我有一个Sql Server查询:
SELECT AC.Author_ID, A.Author_Name, AC.Paper_ID, AC.CoAuthor_ID, AP.Venue_ID, AC.Year
FROM AuthorCoAuthor AC
JOIN AuthorPaper AP ON AP.Author_ID = AC.Author_ID AND
AP.Paper_ID = AC.Paper_ID
JOIN Author A ON A.Author_ID = AC.Author_ID
ORDER BY AC.Author_ID, AC.Year, AC.Paper_ID, AC.CoAuthor_ID, AP.Venue_ID
返回近221317行。示例输出为:
Author_ID | Author_Name | Paper_ID | CoAuthor_ID | Venue_ID | Year
------------------------------------------------------------------
677 | Nuno Vasc | 812229 | 901706 | 64309 | 2005
677 | Nuno Vasc | 812486 | 901706 | 65182 | 2005
677 | Nuno Vasc | 818273 | 901706 | 185787 | 2007
1359 | Peng Sui | 818373 | 457348 | 18534 | 2005
1359 | Peng Sui | 868273 | 542321 | 184222 | 2006
... | ... | ... | ... | ... | ...
我希望查询只返回10个不同的Author_ID
,而我尝试添加
WHERE COUNT(DISTINCT(AC.Author_ID)) <= 10
这个WHERE
子句进入同一个查询,但我仍然得到相同数量的行,即通常查询返回的所有数据。
为什么我的WHERE
条款在这里不起作用?
答案 0 :(得分:1)
要获得10个不同的Author_ID
,您可以将 GROUP BY 与 TOP 10 一起使用。然后将结果(此处为10行)与其他表一起在select list
中显示所需的列。
你可以试试这个:
SELECT AC.Author_ID, A.Author_Name, AC.Paper_ID, AC.CoAuthor_ID, AP.Venue_ID, AC.Year
FROM
(
SELECT TOP (10) AC.Author_ID
FROM AuthorCoAuthor AC
GROUP BY AC.Author_ID
ORDER BY AC.Author_ID
) DS
JOIN AuthorCoAuthor AC ON AC.Author_ID = DS.Author_ID
JOIN AuthorPaper AP ON AP.Author_ID = AC.Author_ID AND
AP.Paper_ID = AC.Paper_ID
JOIN Author A ON A.Author_ID = AC.Author_ID
ORDER BY AC.Author_ID, AC.Year, AC.Paper_ID, AC.CoAuthor_ID, AP.Venue_ID
<强>更新强>
根据之前的答案,如果10 Distinct Author
和AuthorPaper
和Author
同时,您可以获得少于10 Distinct Author
的答案。 10 Distinct Author
表没有其中一个十大连续作者。出于这个原因,您需要在获得AuthorCoAuthor
时检查这些情况,以便当{且仅10 Distinct Author
至少10 Distinct Author
时,它始终为您提供SELECT AC.Author_ID, A.Author_Name, AC.Paper_ID, AC.CoAuthor_ID, AP.Venue_ID, AC.Year
FROM
(
SELECT TOP (10) AC.Author_ID
FROM AuthorCoAuthor AC
JOIN AuthorPaper AP
ON AP.Author_ID = AC.Author_ID
AND AP.Paper_ID = AC.Paper_ID
JOIN Author A ON A.Author_ID = AC.Author_ID
GROUP BY AC.Author_ID
ORDER BY AC.Author_ID
) DS
JOIN AuthorCoAuthor AC ON AC.Author_ID = DS.Author_ID
JOIN AuthorPaper AP
ON AP.Author_ID = AC.Author_ID
AND AP.Paper_ID = AC.Paper_ID
JOIN Author A ON A.Author_ID = AC.Author_ID
ORDER BY AC.Author_ID, AC.Year, AC.Paper_ID, AC.CoAuthor_ID, AP.Venue_ID
。所以,为了确保你总是得到log
,你可以使用这个:
env_logger