无法解决SQL查询 [英] Not able to solve SQL query
问题描述
我有一张如下所示的表格。
日期产品>
2017年2月1日P1
2017年2月1日P2
2017年2月2日P2
2017年2月2日P3
请在这两天内向我推荐一下这个产品的详细信息。
谢谢,
Chandra
我的尝试:
我尝试过与联合相关的子查询
select * from dbo.Product_Table t
其中产品<> (选择*来自dbo.Product_Table
,其中date = t.date)
您可以通过下面的查询尝试该组。 />
它将显示所有带有单个日期的产品。
SELECT 产品
FROM dbo.Product_Table
GROUP BY 产品
HAVING COUNT( DISTINCT < span class =code-keyword>日期)= 1
注意:如果有两行具有相同的日期,则还将返回该产品。
如果您不想在同一天显示包含两条记录的产品,请使用下面的查询:
SELECT 产品
FROM dbo.Product_Table
GROUP BY 产品
HAVING COUNT(产品)= 1 跨度>
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)
You could try the group by query below.
It will show all products with a single Date.
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:
SELECT Product FROM dbo.Product_Table GROUP BY Product HAVING COUNT(Product) = 1
这篇关于无法解决SQL查询的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!