Insert-Select for single result of multiple rows in same table

bajicdusko

I do not know if title of this question makes any sense, but please allow me to explain my problem.

I have table like this.

DocTag table

DocId | TagId
   10 | 8
   10 | 45
   11 | 2
   11 | 15
   12 | 9
   12 | 32
   13 | 8
   13 | 15

Tag table

TagId | TagName
    8 | HomePage
    2 | Private
   45 | IssuerNameOne
   15 | IssuerNameTwo
   32 | IssuerNameThree
    9 | TagThatNeedsToBeSkipped
 3000 | NewTag

DocTag table contains FK's from Document and Tag tables. Now i have to select those documents that have tags with ids 8 or 2 and one of other ids (eg: 45, 32, 15), and when i find that Document in this table i have to insert [DocId | 3000], where 3000 is id of new tag.

In other words i have to select documents that belongs HomePage or Private and one of mentioned issuers and assign new tag to that document.

I have millions of documents and hundreds of tags, and 72 different issuers, so i suppose i have to execute query 72 times for every issuer.

For "IssuerNameOne" result of selection query should be:

DocId
   10       

because of 8 and 45 TagIds.

For "IssuerNameTwo" result of selection query should be:

DocId
   11
   13

because of 2, 8 and 15 TagIds.

After insert execution, DocTag should looks like this:

DocId | TagId
   10 | 8
   10 | 45
   11 | 2
   11 | 15
   12 | 9
   12 | 32
   13 | 8
   13 | 15
   10 | 3000
   11 | 3000
   13 | 3000
T I
--INSERT INTO DocTag  (DocId,TagId)
SELECT DISTINCT DocId, 3000
FROM DocTag  t1 
WHERE TagId IN(8,2)
-- Check the DocId also has a TagId `IN(45,32,15)`
AND EXISTS (SELECT 1 FROM DocTag  t2 
            WHERE t2.DocId=t1.DocId AND t2.TagId IN(45,32,15))
-- Check the new tag mapping doesn't already exists
AND NOT EXISTS(SELECT 1 FROM DocTag  t3 
               WHERE t3.DocId=t1.DocId AND t3.TagId=3000)

Fiddle

Collected from the Internet

Please contact [email protected] to delete if infringement.

edited at
0

Comments

0 comments
Login to comment

Related

From Dev

SQL: Select rows in a table by filtering multiple columns from the same table by a 3 column select result

From Dev

How to insert three new rows for every result of a SELECT query into the same table

From Dev

Insert multiple rows from select into another table

From Dev

Convert the multiple rows Select statement result into a single row string result

From Dev

Insert multiple rows from a single row inside an insert into select query

From Dev

Select multiple rows in single result using SQL Server 2008

From Dev

Insert multiple rows with single a query from one table into another in Oracle

From Dev

SQL insert multiple rows from single field in another table

From Dev

Joining multiple queries on a table and get a single select result

From Dev

select multiple column from one table and insert into another as rows

From Dev

php pdo select multiple rows and insert to other table with LIMIT

From Dev

How to select multiple rows of mysql table as a single row

From Dev

Reusing single HTML select / drop down list for multiple table rows?

From Dev

How to select a single row where multiple rows exist from a table

From Dev

How to select a single row where multiple rows exist from a table

From Dev

How to insert a single row in the parent table and then multiple rows in the child table in single SQL in PostgreSQL?

From Dev

How to insert a single row in the parent table and then multiple rows in the child table in single SQL in PostgreSQL?

From Dev

MySQL select and update multiple rows from same table

From Dev

MySQL select grouping multiple rows from same table

From Dev

Have a select query with multiple where clauses return multiple result rows in one result table?

From Dev

Have a select query with multiple where clauses return multiple result rows in one result table?

From Dev

How to insert multiple data of same name from single form in multiple row of same column of table

From Dev

How to insert multiple data of same name from single form in multiple row of same column of table

From Dev

INSERT multiple rows with SELECT and an array

From Dev

Postgresql Insert select with multiple rows

From Dev

Multiple SELECT on the same table

From Dev

Insert multiple rows in table with a join

From Dev

Updating multiple rows of single table

From Dev

mysql insert with select query to insert multiple rows

Related Related

  1. 1

    SQL: Select rows in a table by filtering multiple columns from the same table by a 3 column select result

  2. 2

    How to insert three new rows for every result of a SELECT query into the same table

  3. 3

    Insert multiple rows from select into another table

  4. 4

    Convert the multiple rows Select statement result into a single row string result

  5. 5

    Insert multiple rows from a single row inside an insert into select query

  6. 6

    Select multiple rows in single result using SQL Server 2008

  7. 7

    Insert multiple rows with single a query from one table into another in Oracle

  8. 8

    SQL insert multiple rows from single field in another table

  9. 9

    Joining multiple queries on a table and get a single select result

  10. 10

    select multiple column from one table and insert into another as rows

  11. 11

    php pdo select multiple rows and insert to other table with LIMIT

  12. 12

    How to select multiple rows of mysql table as a single row

  13. 13

    Reusing single HTML select / drop down list for multiple table rows?

  14. 14

    How to select a single row where multiple rows exist from a table

  15. 15

    How to select a single row where multiple rows exist from a table

  16. 16

    How to insert a single row in the parent table and then multiple rows in the child table in single SQL in PostgreSQL?

  17. 17

    How to insert a single row in the parent table and then multiple rows in the child table in single SQL in PostgreSQL?

  18. 18

    MySQL select and update multiple rows from same table

  19. 19

    MySQL select grouping multiple rows from same table

  20. 20

    Have a select query with multiple where clauses return multiple result rows in one result table?

  21. 21

    Have a select query with multiple where clauses return multiple result rows in one result table?

  22. 22

    How to insert multiple data of same name from single form in multiple row of same column of table

  23. 23

    How to insert multiple data of same name from single form in multiple row of same column of table

  24. 24

    INSERT multiple rows with SELECT and an array

  25. 25

    Postgresql Insert select with multiple rows

  26. 26

    Multiple SELECT on the same table

  27. 27

    Insert multiple rows in table with a join

  28. 28

    Updating multiple rows of single table

  29. 29

    mysql insert with select query to insert multiple rows

HotTag

Archive