查找组之间的公共数据

时间:2013-02-19 17:40:25

标签: sql sql-server

我有这个数据

DeveloperId   Date             remark
--------------------------------------
1             02/01/2013        Google
1             02/02/2013        MSN
1             02/03/2013        Google
1             02/02/2013        MSN
1             02/01/2013        Yahoo
2             02/01/2013        Google
2             02/02/2013        Yahoo
2             02/03/2013        Google
2             02/01/2013        Google

如何动态查找Google,Msn和yahoo的常用日期?

我想在报告中提出这样的问题。

5 个答案:

答案 0 :(得分:2)

-- Query for dates that have all types of remarks
SELECT
  Date
FROM
  data
GROUP BY 
  Date
HAVING COUNT(DISTINCT remark) = (
  SELECT COUNT(DISTINCT remark)
  FROM data
);

或者,如果那不是要求......

-- Query for dates that have more than one remark.
SELECT
  Date
FROM
  data
GROUP BY 
  Date
HAVING COUNT(DISTINCT remark) > 1;

Sql Fiddle

答案 1 :(得分:1)

试试这个:

with temp as
(
    SELECT Date, Remark
    From MyTable
    GROUP BY Date, Remark
)
SELECT Date
FROM temp
GROUP BY Date
Having COUNT(*) > 1

在这里工作小提琴:http://sqlfiddle.com/#!3/d41d8/9105

答案 2 :(得分:1)

3种不同评论的共同日期:

SELECT t1.DATE
FROM TABLE T1
INNER JOIN TABLE T2
ON t1.DATE = t2.DATE
AND Remark IN ('google', 'yahoo', 'msn')
GROUP BY t1.DATE, t1.REMARK
HAVING COUNT(t1.DATE) = 3;

答案 3 :(得分:0)

Select Date from data
group by remark, Date  

答案 4 :(得分:0)

尝试:

select "Date"
from myTable
group by "Date"
where remark in ('Google','MSN','Yahoo')
having count(distinct remark) = 3