在SQL中获取多个计数

huangapple go评论98阅读模式
英文:

getting multiple counts in SQL

问题

我希望我的 SQL 查询返回所有活动和过去案例的计数。我尝试了以下查询:

  1. SELECT
  2. sum(case when need_By > sysdate and status not in (30,40,50) then 1 else 0 end) AS active,
  3. sum(case when need_by < sysdate and status not in (30,40,50) then 1 else 0 end) AS past
  4. FROM discrepancy
  5. GROUP BY id;

这给我以下输出:

  1. ACTIVE PAST
  2. 0 1
  3. 1 0
  4. 0 0
  5. 0 1
  6. 0 1

我期望的是:

  1. ACTIVE PAST
  2. 1 3

我应该如何更改查询以获取活动和过去记录的计数。此查询将在 Hibernate 中执行,因此希望确保 createNativeQuery 能够正常工作。

英文:

I want my sql query to return me count of all active and past cases. I tried a query like this,

  1. SELECT
  2. sum(case when need_By &gt; sysdate and status not in (30,40,50) then 1 else 0 end) AS active,
  3. sum(case when need_by &lt; sysdate and status not in (30,40,50) then 1 else 0 end) AS past
  4. FROM discrepancy
  5. GROUP BY id;

This gives me output as follows:

  1. ACTIVE PAST
  2. 0 1
  3. 1 0
  4. 0 0
  5. 0 1
  6. 0 1

What i am expecting is ,

  1. ACTIVE PAST
  2. 1 3

How should I change the query to get the count of active and past records. Also this query will be executed in hibernate and so want to make sure the createNativeQuery works .

答案1

得分: 1

移除GROUP BY

  1. SELECT sum(CASE WHEN need_By > sysdate AND status NOT IN (30, 40, 50) THEN 1 ELSE 0 END) AS active,
  2. sum(CASE WHEN need_by < sysdate AND status NOT IN (30, 40, 50) THEN 1 ELSE 0 END) AS past
  3. FROM discrepancy;

MySQL也有一个方便的简写表示,可以直接计算布尔表达式:

  1. SELECT SUM(need_By > sysdate AND status NOT IN (30, 40, 50)) AS active,
  2. SUM(need_by < sysdate AND status NOT IN (30, 40, 50)) AS past
  3. FROM discrepancy;

最后,你可以将NOT IN移到WHERE子句中,这可能会提高性能:

  1. SELECT SUM(need_By > sysdate) AS active,
  2. SUM(need_by < sysdate) AS past
  3. FROM discrepancy
  4. WHERE status NOT IN (30, 40, 50);

在Oracle中,最后一种写法如下:

  1. SELECT SUM(CASE WHEN need_By > sysdate THEN 1 ELSE 0 END) AS active,
  2. SUM(CASE WHEN need_by < sysdate THEN 1 ELSE 0 END) AS past
  3. FROM discrepancy
  4. WHERE status NOT IN (30, 40, 50);
英文:

Remove the GROUP BY:

  1. SELECT sum(case when need_By &gt; sysdate and status not in (30,40,50) then 1 else 0 end) AS active,
  2. sum(case when need_by &lt; sysdate and status not in (30,40,50) then 1 else 0 end) AS past
  3. FROM discrepancy;

MySQL also has a convenient short-hand notation so you can count the boolean expressions directly:

  1. SELECT SUM(need_By &gt; sysdate and status not in (30,40,50)) AS active,
  2. SUM(need_by &lt; sysdate and status not in (30,40,50)) AS past
  3. FROM discrepancy;

And finally, you can move the NOT IN to a WHERE clause which could improve performance:

  1. SELECT SUM(need_By &gt; sysdate) AS active,
  2. SUM(need_by &lt; sysdate) AS past
  3. FROM discrepancy
  4. WHERE status NOT IN (30, 40, 50);

EDIT:

In Oracle, the last would be:

  1. SELECT SUM(CASE WHEN need_By &gt; sysdate THEN 1 ELSE 0 END) AS active,
  2. SUM(CASE WHEN need_by &lt; sysdate THEN 1 ELSE 0 END) AS past
  3. FROM discrepancy
  4. WHERE status NOT IN (30, 40, 50);

huangapple
  • 本文由 发表于 2020年8月5日 03:22:13
  • 转载请务必保留本文链接:https://go.coder-hub.com/63253759.html
匿名

发表评论

匿名网友

:?: :razz: :sad: :evil: :!: :smile: :oops: :grin: :eek: :shock: :???: :cool: :lol: :mad: :twisted: :roll: :wink: :idea: :arrow: :neutral: :cry: :mrgreen:

确定