如何有效地使用 CASE 和 HAVING 子句来优化 SQL 服务器查询
How to use CASE and HAVING clauses efficiently to optimize SQL Server Query
我有一个 SQL 查询,即
SELECT
A2P.aid,
COUNT(*) [PaperCount],
[2010] = SUM(CASE WHEN A2P.p_year = 2010 THEN 1 ELSE 0 END),
[2011] = SUM(CASE WHEN A2P.p_year = 2011 THEN 1 ELSE 0 END),
[2012] = SUM(CASE WHEN A2P.p_year = 2012 THEN 1 ELSE 0 END),
[2013] = SUM(CASE WHEN A2P.p_year = 2013 THEN 1 ELSE 0 END),
[2014] = SUM(CASE WHEN A2P.p_year = 2014 THEN 1 ELSE 0 END)
FROM
sub_aminer_author2paper A2P
WHERE
aid IN (SELECT
aid
FROM
sub_aminer_author
WHERE
paper_count >= 20
)
AND A2P.p_year BETWEEN 2010 AND 2014
GROUP BY
A2P.aid
HAVING
COUNT(DISTINCT A2P.pid) >= 2
ORDER BY
A2P.aid
现在,问题是我希望输出中的行在每列中包含 >= 2 的值,即 2010
、2011
、2012
、2013
,和 2014
OR 列中的值 Paper_Count
>= 10 对于输出中的每一行。
此外,我使用的条件为:
HAVING
COUNT(DISTINCT A2P.pid) >= 2
但结果并不如人意。查看输出图像...
显然 0
值也在输出中检索到,这不是我们想要的。
请在这方面提供帮助。
谢谢!
查询您的Select您的条件:
Select A2P.aid, [PaperCount], [2010, [2011], [2012], [2013], [2014]
From (
SELECT
A2P.aid,
COUNT(*) [PaperCount],
[2010] = SUM(CASE WHEN A2P.p_year = 2010 THEN 1 ELSE 0 END),
[2011] = SUM(CASE WHEN A2P.p_year = 2011 THEN 1 ELSE 0 END),
[2012] = SUM(CASE WHEN A2P.p_year = 2012 THEN 1 ELSE 0 END),
[2013] = SUM(CASE WHEN A2P.p_year = 2013 THEN 1 ELSE 0 END),
[2014] = SUM(CASE WHEN A2P.p_year = 2014 THEN 1 ELSE 0 END)
FROM
sub_aminer_author2paper A2P
WHERE
aid IN (SELECT aid FROM sub_aminer_author WHERE paper_count >= 20)
AND A2P.p_year BETWEEN 2010 AND 2014
GROUP BY
A2P.aid
) x
Where [2010] >= 2 and [2011] >= 2 and [2012] >= 2 and [2013] >= 2 and [2014] >= 2;
或者像这样:
Select aid
, [PaperCount] = [2010] + [2011] + [2012] + [2013] + [2014]
, [2010], [2011], [2012], [2013], [2014]
From (
SELECT aid, p_year
FROM @sub_aminer_author2paper A2P
WHERE aid IN (SELECT aid FROM @sub_aminer_author WHERE paper_count >= 20)
AND p_year BETWEEN 2010 AND 2014
GROUP BY A2P.aid, A2P.p_year
)p
Pivot (
count(p_year)
For p_year In ([2010], [2011], [2012], [2013], [2014])
) as piv
Where [2010] >= 2 and [2011] >= 2 and [2012] >= 2 and [2013] >= 2 and [2014] >= 2;
如果有人想看看我是如何实现的,我已经从不同的来源进行了编译并优化了我的查询。请看看这个。
https://junaidtechblog.wordpress.com/2019/09/04/optimize-sql-query-groupby-having/
我有一个 SQL 查询,即
SELECT
A2P.aid,
COUNT(*) [PaperCount],
[2010] = SUM(CASE WHEN A2P.p_year = 2010 THEN 1 ELSE 0 END),
[2011] = SUM(CASE WHEN A2P.p_year = 2011 THEN 1 ELSE 0 END),
[2012] = SUM(CASE WHEN A2P.p_year = 2012 THEN 1 ELSE 0 END),
[2013] = SUM(CASE WHEN A2P.p_year = 2013 THEN 1 ELSE 0 END),
[2014] = SUM(CASE WHEN A2P.p_year = 2014 THEN 1 ELSE 0 END)
FROM
sub_aminer_author2paper A2P
WHERE
aid IN (SELECT
aid
FROM
sub_aminer_author
WHERE
paper_count >= 20
)
AND A2P.p_year BETWEEN 2010 AND 2014
GROUP BY
A2P.aid
HAVING
COUNT(DISTINCT A2P.pid) >= 2
ORDER BY
A2P.aid
现在,问题是我希望输出中的行在每列中包含 >= 2 的值,即 2010
、2011
、2012
、2013
,和 2014
OR 列中的值 Paper_Count
>= 10 对于输出中的每一行。
此外,我使用的条件为:
HAVING
COUNT(DISTINCT A2P.pid) >= 2
但结果并不如人意。查看输出图像...
显然 0
值也在输出中检索到,这不是我们想要的。
请在这方面提供帮助。
谢谢!
查询您的Select您的条件:
Select A2P.aid, [PaperCount], [2010, [2011], [2012], [2013], [2014]
From (
SELECT
A2P.aid,
COUNT(*) [PaperCount],
[2010] = SUM(CASE WHEN A2P.p_year = 2010 THEN 1 ELSE 0 END),
[2011] = SUM(CASE WHEN A2P.p_year = 2011 THEN 1 ELSE 0 END),
[2012] = SUM(CASE WHEN A2P.p_year = 2012 THEN 1 ELSE 0 END),
[2013] = SUM(CASE WHEN A2P.p_year = 2013 THEN 1 ELSE 0 END),
[2014] = SUM(CASE WHEN A2P.p_year = 2014 THEN 1 ELSE 0 END)
FROM
sub_aminer_author2paper A2P
WHERE
aid IN (SELECT aid FROM sub_aminer_author WHERE paper_count >= 20)
AND A2P.p_year BETWEEN 2010 AND 2014
GROUP BY
A2P.aid
) x
Where [2010] >= 2 and [2011] >= 2 and [2012] >= 2 and [2013] >= 2 and [2014] >= 2;
或者像这样:
Select aid
, [PaperCount] = [2010] + [2011] + [2012] + [2013] + [2014]
, [2010], [2011], [2012], [2013], [2014]
From (
SELECT aid, p_year
FROM @sub_aminer_author2paper A2P
WHERE aid IN (SELECT aid FROM @sub_aminer_author WHERE paper_count >= 20)
AND p_year BETWEEN 2010 AND 2014
GROUP BY A2P.aid, A2P.p_year
)p
Pivot (
count(p_year)
For p_year In ([2010], [2011], [2012], [2013], [2014])
) as piv
Where [2010] >= 2 and [2011] >= 2 and [2012] >= 2 and [2013] >= 2 and [2014] >= 2;
如果有人想看看我是如何实现的,我已经从不同的来源进行了编译并优化了我的查询。请看看这个。 https://junaidtechblog.wordpress.com/2019/09/04/optimize-sql-query-groupby-having/