Click here to Skip to main content
15,895,142 members
Please Sign up or sign in to vote.
0.00/5 (No votes)
See more:
C#
I have a two tables A and B

In table A I have two coloumns date and ID. I would like to get IDs from table A for all dates except two dates "20140115" and "20140425". 
Some ID from table A can appear multiple times, therefore I have to use distinct.
After I get all IDs from table A, then I have to use the extract IDs from table A to get the data from table B.Because the same IDs exist in both tables.
I know that I have to use both distinct and join. 

I need help with combination of both distinct and join to get what I have explained above.
I need something like this. 


SELECT A.ID, B.ID
FROM (select distinct ID from A where date<>20140115 and date<>20140125) AS A
JOIN (select ID) AS B
ON A.ID = B.ID;

Thanks
Posted

1 solution

If I understand you correctly you want to get a distinct list of ID from Table A and then retrieve the records from Table B that contain the IDs.

In this case you can try the in statement.
eg

SQL
Select * from tableB where ID in (select distinct ID from TableA where date not in ('15-Jan-2015','25-Jan-2015'))
 
Share this answer
 
v2
Comments
merh 22-Dec-15 6:40am    
Thanks a lot!

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)



CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900