我有一张看起来像这样的表
ID SchoolName SchoolURL updflag orderflag
1 Brighton School Brighton.edu 0 2
2 Andover school Andover.edu 0 2
3 Fremont school Fremont.edu 0 2
4 Tulsa Public school Tulsa.edu 0 1
5 Crescent school Crescent.edu 0 1
55 NH school NH.edu 0 2
61 PennState Univ pennstate.edu 0 1
2 john Adams public School Andover.edu 0 2
2 Estabrook school Andover.edu 0 2
我想只打印没有重复的记录,即不应打印ID = 2
。我想首先选择orderflag = 2
的记录,然后显示orderflag = 1
。我还希望一次只显示10
条记录。如果我更新记录,updflag
会更新为1
。
SELECT TOP 10 r.orderflag,r.[id],
r.schoolurl,
updatedflag
FROM [dbo].[schools] AS r
INNER JOIN
(SELECT id
FROM [dbo].[schools]
GROUP BY id
HAVING COUNT(*) = 1) AS k
ON k.id = r.id
WHERE updflag = 0
GROUP BY r.orderflag,r.id, School URL, updflag order by r.orderflag desc;
我有数百万条记录,我希望先处理orderflag = 2
,然后再处理orderflag = 1
。我怎么能这样做?
由于 [R
答案 0 :(得分:0)
一种可能的方法是简单地排除COUNT > 1
存在ID
的那些:
SELECT ... FROM schools
WHERE ID NOT IN (
SELECT ID FROM schools
GROUP BY ID
HAVING COUNT(ID) > 1
)
接下来,要处理订单标记,您可以执行以下操作:
SELECT ... FROM schools
WHERE orderflag = 2 AND
ID NOT IN (
SELECT ID FROM schools
WHERE orderflag = 2
GROUP BY ID
HAVING COUNT(ID) > 1
)
UNION
SELECT ... FROM schools
WHERE orderflag = 1 AND
ID NOT IN (
SELECT ID FROM schools
WHERE orderflag = 1
GROUP BY ID
HAVING COUNT(ID) > 1
)