好吧,所以我有这个查询,几乎拉了我在数据库中得到的所有信息
Select
p.ProjectName,
s.siteid,
c.PaperID,
m.MemberFirstName,
m.MemberLastName,
m.MemberDOB,
m.MemberID
From Sites s, Papers c, Members m, Projects p
Where s.SiteID=c.SiteID
and c.MemberID=m.MemberID
and s.ProjectID=p.ProjectID
order by ProjectName, MemberFirstName, MemberLastName
但是,我只是想查看我的查询,只显示会员进入多个网站时的多个实例
目前我的结果看起来像下面的输出 - 所以在这种特殊情况下,我想保留John Doe的结果,因为他出现在多个站点中(他的MemberID证明他是一样的John Doe并不是一个不同的John Doe,但我不想保留Jane Doe的结果,因为她只在一个网站上出现一次。
ProjectName | SiteID | PaperId | MemberFirstName | MemberLastName | MemberDOB | MemberID
-------------------------------------------------------------------------------------------
Project 1 | 123456 | 123 | Jane | Doe | 1991-01-01 | 12345
-------------------------------------------------------------------------------------------
Project 1 | 789012 | 321 | John | Doe | 1991-02-02 | 54321
-------------------------------------------------------------------------------------------
Project 1 | 345678 | 456 | John | Doe | 1991-02-02 | 54321
答案 0 :(得分:0)
您想要的是GROUP BY
和HAVING
。请注意,您无法选择相同的数据,因为每人会有多个SiteID
。
这样的事情:
Select
m.MemberFirstName,
m.MemberLastName,
m.MemberDOB,
m.MemberID
From Sites s, Papers c, Members m, Projects p
Where s.SiteID = c.SiteID
and c.MemberID = m.MemberID
and s.ProjectID = p.ProjectID
order by ProjectName, MemberFirstName, MemberLastName
GROUP BY
m.MemberFirstName,
m.MemberLastName,
m.MemberDOB,
m.MemberID
HAVING COUNT(*) > 1
答案 1 :(得分:0)
或者,如果SiteID很重要,您可以执行以下操作:
SELECT
ProjectName,
siteid,
PaperID,
MemberFirstName,
MemberLastName,
MemberDOB,
MemberID
FROM (
Select
p.ProjectName,
s.siteid,
c.PaperID,
m.MemberFirstName,
m.MemberLastName,
m.MemberDOB,
m.MemberID,
COUNT(1) OVER ( PARTITION BY m.MemberID ) AS CNT
From Sites s, Papers c, Members m, Projects p
Where s.SiteID=c.SiteID
and c.MemberID=m.MemberID
and s.ProjectID=p.ProjectID
) A
WHERE CNT > 1
order by ProjectName, MemberFirstName, MemberLastName