I use a SQL Server 2008 database.
I have two tables with columns like these:
Table A:
request_id|order_id|e-mail 100 |2567 |[email protected] 100 |4784 |[email protected] 450 |2578 |[email protected] 450 |8432 |[email protected] 600 |9032 |[email protected] 600 |9033 |[email protected] Table B has also id and order_no columns and many others columns:
Table B:
request_id|order_id|e-mail 100 |2563 |[email protected] 300 |4784 |[email protected] 600 |9032 |[email protected] 650 |2578 |[email protected] 850 |8432 |[email protected] As you can see, a given request_id in table A can occur more than once (see 100 & 450 records)
I need to find all records from table A, which are not present in table B by order_id, but have equal request_id column values.
For above example I expect something like this:
Output:
request_id|order_id|e-mail 100 |2567 |[email protected] 100 |4784 |[email protected] 450 |2578 |[email protected] 450 |8432 |[email protected] 600 |9033 |[email protected] As you can see above records from table A are not present in table B. This criteria is only satisfied with record where order_id=600
I created the sketch of T-SQL query:
select D.request_id, D.order_id from table A AS D where D.request_id = 600 and D.order_id not in (select M.order_id from table B AS M where M.request_id = 600) Unfortunately I don't have idea how to transform my query for all request_id. The first think is to use while loop over all request_id from table A, but it seems not smart in SQL world.
Thank you for any help