Select records in one table where a field in this table is included in another table - sql-server

Basically I have two tables. Table1 has millions of rows. Table2 has very few rows.
Table1 has field1 which is a product ID (not unique). Table2 has field2 which is just a list of productID's that need to be included from Table1 in the select statement.

SELECT Table1.*
FROM Table1
Join Table2 ON Table2.field2 = table1.field1

Related

SQL Server Merge statement causing error

I have 2 tables Table1 and Table2 that have the following identical schema:
Table1 (id int, lastModifiedDate datetime)
Table2 (id int, lastModifiedDate datetime)
Table1 is the source table, Table2 is the target table.
I need in only one query to execute the following scenario:
if Table1.id = Table2.id and Table1.lastModifiedDate = Table2.lastModifiedDate then don't do anything.
elseif Table1.id = Table2.id and Table1.lastModifiedDate <> Table2.lastModifiedDate then we have to delete from Table2 all row with this id and then we have to insert into it the matching row
I need also to insert into Table2 id values present in Table1 and not present in Table2
To summarize I need to do something like the following query (which has an incorrect syntax):
MERGE INTO Table2 AS target
USING (SELECT id, lastModifiedDate FROM Table1) AS source ON Source.id = target.id
WHEN NOT MATCHED BY TARGET
THEN INSERT(id,lastModifiedDate) VALUES(source.id,source.lastModifiedDate)
WHEN MATCHED AND Source.lastModifiedDate <> target.lastModifiedDate
THEN Delete
WHEN MATCHED AND Source.lastModifiedDate <> target.lastModifiedDate
THEN
INSERT(idlastModifiedDate)
VALUES(source.id, source.lastModifiedDate)
OUTPUT $action, inserted.*, deleted.*;
The generated error is that we can not have an insert in the when matched case.
Can anyone has an idea how can we do this?
here is an example of scenarios:
scenario 1:
table 1 an table2 contain the following row:
Table1 (1,2011-10-05 14:55:00.403)
Table2 is empty
after execution, since the Table1.id is not present in Table2, we should insert this row into Table2
So Table2 should contain (1,2011-10-05 14:55:00.403)
Scenario 2:
Table1 (1,2011-10-05 14:55:00.403),(2,2011-10-05 14:55:00.403)
Table2 (1,2011-10-05 14:55:00.403)
after execution, because the first row already exist in Table2, we don't touch it, however the 2nd row don't exist yet so we have to insert it .
So Table2 should contain (1,2011-10-05 14:55:00.403),(2,2011-10-05 14:55:00.403)
Scenario 2:
Table1 (1,2011-10-05 14:55:00.403),(2,2011-10-05 00:00:00.403)
after execution, because the 2nd row has the same id but another lastModifiedDate, we have to delete from table2 the row having this id and then insert the 2nd row of table1
So after execution, Table2 should contain (1,2011-10-05 14:55:00.403),(2,2011-10-05 00:00:00.403)
Thanks in advance
No, you cannot have an INSERT in matched (https://msdn.microsoft.com/en-us/library/bb510625.aspx) But are you sure your disallowed INSERT shouldn't just be an UPDATE? Also seeing that you try to insert two columns into one column? (source.id, source.lastModifiedDate)==> idlastModifiedDate and the two matched conditions are the same?
So more like:
MERGE INTO Table2 AS target
USING (SELECT id, lastModifiedDate FROM Table1) AS source ON Source.id = target.id
WHEN NOT MATCHED BY TARGET
THEN INSERT(id,lastModifiedDate) VALUES(source.id,source.lastModifiedDate)
WHEN MATCHED THEN
UPDATE set lastModifiedDate = Source.lastModifiedDate

With TSQL, how do I create a record in Table2 using GUID from existing record in Table1

Table1 contains Work Orders and Table2 contains Installers. I would like to insert a record into Table2 for Work Order 3906 in Table1 using unique identifier for that Work Order such that Table1GUID = Table2GUID . I'll worry about populating the new record later (all columns in Table2 can be blank for now except GUID which must be identical to corresponding GUID from Table1). Records for Table1 and Table2 are one to one.
This is NOT working: (0 row(s) affected)
INSERT INTO Table2 (Table2GUID)
SELECT Table1GUID FROM Table1
WHERE Table1.WOnumber = '3906'
This doesn't work either:
INSERT INTO Table2 (Table2GUID)
SELECT Table1GUID FROM Table1
INNER JOIN Table2 ON Table2GUID = Table1GUID
WHERE Table1.WOnumber = '3906'

How to represent OR table using JOIN

I have a query having 4 tables Table1, Table2, Table3 and Table4.
Table1 is master table having IDs
Table2 is child table with FK relationship with Table1 and has a column called Tag.
Table3 is child table with FK relationship with Table1 and has a column called Code
Table4 is child table with FK relationship with Table1 and has column called Code2
Now I want a query to represent follow logic
Select Table1.ID
From Table1 ...
WHERE Table2.Tag IN( 1,2,3,4)
AND ( Table3.Code In (456,789) OR Table4.CODE2 in (123,897) )
I know I can have join between Table1 and Table2 get the ids for which Tag value is in (1,2,3,4). But I am not sure how to join the Table3 and Table4 to achieve the requirement.
You just keep adding joins to the child table based on the condition that the Foreign Key column in each child table equals the key of Table1
You're query should look like this
Select Table1.ID
From Table1
JOIN Table2 ON Table2.FK_ID = Table1.ID
JOIN Table3 ON Table3.FK_ID = Table1.ID
JOIN Table4 ON Table4.FK_ID = Table1.ID
WHERE Table2.Tag IN( 1,2,3,4)
AND ( Table3.Code In (456,789) OR Table4.CODE2 in (123,897) )
But with FK_ID replaced with the appropriate Foreign Key field

How to use BETWEEN in a reference table

I was just wondering on how create a query that in such a way it will check if the column is in between in a reference table.
such as
SELECT *
FROM Table1 WHERE Column1 BETWEEN ( SELECT Column1 , Column2 FROM TABLE2 )
I just don't know how to implement it in a correct way.
Thank you.
If you can have overlapping ranges in Table2, and all you want are (unique) Table1 records that are in any range in Table2, then this query will do it.
SELECT *
FROM Table1
WHERE EXISTS (
SELECT *
FROM Table2
Where Table1.Column1 BETWEEN Table2.Column1 and Table2.Column2)
You can also solve this using JOINs, if the ranges in Table2 are not overlapping, otherwise you will need to use either DISTINCT or ROW_NUMBER() to pare them down to unique Table1 records.
Try This....
SELECT *
FROM Table1 as t1
INNER JOIN Table2 t2 ON t1.Column1 BETWEEN t2.Column1 AND t2.Column2
this works
SELECT * FROM table1 as t1,table2 as t2
WHERE t1.Column1 BETWEEN t2.Column1 AND t2.Column2.

SQL Server many-to-many query

I have two tables, let's call them table1 and table2. They both have a column called ID1 and ID2 which are respective PK for each of two tables.
I have also another table, called table3 which contains ID1 and ID2, establishing a many to many relation between these two tables.
What I need is to get all the records from table2 that are not related with records in table1.
Ex.
Table 1 contains a record with ID = 1
Table 2 contains two records with ID 1, 2
Table 3 contains one record with values 1 - 1
I need a query that will give me as result 2.
Can anyone suggest me a way to proceed?
Thanks
SELECT t2.ID2
FROM table2 t2
WHERE NOT EXISTS(SELECT NULL
FROM table3 t3
WHERE t3.ID2 = t2.ID2);
You could also use a LEFT JOIN:
SELECT t2.ID2
FROM table2 t2
LEFT JOIN table3 t3
ON t2.ID2 = t3.ID2
WHERE t3.ID2 IS NULL;

Resources