计算另一个计数值,但前提是它足够高

发布于 2024-11-28 02:09:29 字数 657 浏览 1 评论 0原文

我正在尝试提取 2 个数字。第一个是 1 个月内总共有多少医生(dr 表)有超过 10 个答案(答案表),无论日期如何,总共有 75 个答案。另一个数字是相同的,但是在过去 3 个月内而不是 1 个月内。

我使用下面的这个答案来提出这个查询

SELECT D.name, 
    count(DISTINCT case when A.created > DATE_SUB(NOW(), INTERVAL 1 MONTH) then A.id end) as '1 month', 
    count(DISTINCT case when A.created > DATE_SUB(NOW(), INTERVAL 3 MONTH) then A.id end) as '1 quarter', 
    count(DISTINCT A.id) as total
FROM dr D
JOIN answer A ON A.dr_id=D.id AND A.status=3
GROUP BY D.id

:为我提供了所需的原始信息,但我不知道如何通过将它们与 10 和 75 个答案要求进行比较来计算给出的计数。

I'm trying to pull 2 numbers. One is a total of how many doctors (dr table) have more than 10 answers (answers table) from within 1 month and 75 answers total regardless of the date. The other number is the same thing but for within the last 3 months instead of 1 month.

I used this answer answer below to come up with this query:

SELECT D.name, 
    count(DISTINCT case when A.created > DATE_SUB(NOW(), INTERVAL 1 MONTH) then A.id end) as '1 month', 
    count(DISTINCT case when A.created > DATE_SUB(NOW(), INTERVAL 3 MONTH) then A.id end) as '1 quarter', 
    count(DISTINCT A.id) as total
FROM dr D
JOIN answer A ON A.dr_id=D.id AND A.status=3
GROUP BY D.id

This gives me the raw information I need, but I don't know how to count the counts given by comparing them to the 10 and 75 answers requirements.

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(2

淡莣 2024-12-05 02:09:29

我想是这样的:

SELECT
  COUNT(CASE WHEN total >= 75 AND `1month`   > 10 THEN name END) AS `10+ per month count`,
  COUNT(CASE WHEN total >= 75 AND `1quarter` > 10 THEN name END) AS `10+ per quarter count`
FROM (
  SELECT D.name, 
    count(DISTINCT case when A.created > DATE_SUB(NOW(), INTERVAL 1 MONTH) then A.id end) as `1month`, 
    count(DISTINCT case when A.created > DATE_SUB(NOW(), INTERVAL 3 MONTH) then A.id end) as `1quarter`, 
    count(DISTINCT A.id) as total
  FROM dr D
    JOIN answer A ON A.dr_id=D.id AND A.status=3
  GROUP BY D.id
) s

Something like this, I think:

SELECT
  COUNT(CASE WHEN total >= 75 AND `1month`   > 10 THEN name END) AS `10+ per month count`,
  COUNT(CASE WHEN total >= 75 AND `1quarter` > 10 THEN name END) AS `10+ per quarter count`
FROM (
  SELECT D.name, 
    count(DISTINCT case when A.created > DATE_SUB(NOW(), INTERVAL 1 MONTH) then A.id end) as `1month`, 
    count(DISTINCT case when A.created > DATE_SUB(NOW(), INTERVAL 3 MONTH) then A.id end) as `1quarter`, 
    count(DISTINCT A.id) as total
  FROM dr D
    JOIN answer A ON A.dr_id=D.id AND A.status=3
  GROUP BY D.id
) s
眼眸里的快感 2024-12-05 02:09:29

您可能需要稍微玩一下这个查询,但它应该会为您提供您正在寻找的内容。基本上将您的查询用作派生表,并使用 group by/having 进一步总结它。

;with DrCounts as (
    SELECT D.id, 
      count(DISTINCT case when A.created > DATE_SUB(NOW(), INTERVAL 1 MONTH) then A.id end) as '1month', 
      count(DISTINCT case when A.created > DATE_SUB(NOW(), INTERVAL 3 MONTH) then A.id end) as '1quarter', 
      count(DISTINCT A.id) as total
    FROM dr D
    JOIN answer A ON A.dr_id=D.id AND A.status=3
    GROUP BY D.id)

select count(distinct D.id) as Dr1075
from  DrCounts D
group by D.Id
having D.total >= 75 and D.1month >= 10
union
select count(distinct D.id) as Dr1075
from  DrCounts D
group by D.Id
having D.total >= 75 and D.1quarter >= 10

You might need to play w/ this query a bit, but it should give you what you're looking for. Basically take your query and use it as a derived table and summarize it further using group by/having.

;with DrCounts as (
    SELECT D.id, 
      count(DISTINCT case when A.created > DATE_SUB(NOW(), INTERVAL 1 MONTH) then A.id end) as '1month', 
      count(DISTINCT case when A.created > DATE_SUB(NOW(), INTERVAL 3 MONTH) then A.id end) as '1quarter', 
      count(DISTINCT A.id) as total
    FROM dr D
    JOIN answer A ON A.dr_id=D.id AND A.status=3
    GROUP BY D.id)

select count(distinct D.id) as Dr1075
from  DrCounts D
group by D.Id
having D.total >= 75 and D.1month >= 10
union
select count(distinct D.id) as Dr1075
from  DrCounts D
group by D.Id
having D.total >= 75 and D.1quarter >= 10
~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文