Click here to Skip to main content
15,896,154 members
Please Sign up or sign in to vote.
0.00/5 (No votes)
See more:
Hi,

I have a table like mentioned below.

Date Product
1st Feb 2017 P1
1st Feb 2017 P2
2nd Feb 2017 P2
2nd Feb 2017 P3

Please suggest me the query which will give the details of the product which are not common (or not sold) in both the days.

Thanks,
Chandra

What I have tried:

I tried with co-related sub query

select * from dbo.Product_Table t
where product <> (select * from dbo.Product_Table
where date = t.date)
Posted
Updated 6-Mar-17 18:42pm

1 solution

You could try the group by query below.
It will show all products with a single Date.

SQL
SELECT Product 
FROM dbo.Product_Table 
GROUP BY Product 
HAVING COUNT(DISTINCT Date) = 1


Note: If there are two rows with the same date, the product will also be returned.
Use the query below if you do not want to show a Product with two records on the same day:
SQL
SELECT Product 
FROM dbo.Product_Table 
GROUP BY Product 
HAVING COUNT(Product) = 1
 
Share this answer
 
v2
Comments
Karthik_Mahalingam 7-Mar-17 0:44am    
enclose the code block in
 <pre> 
tag

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