繁体   English   中英

计算SQL中分组行的数量

[英]Calculate number of grouped rows in SQL

我有2个表格,请求和响应

要求:

RequestId  UserId  InsertDate
1           1       5/4/2013
2           2       6/4/2012
.           .          .
.           .          .

对策:

Responseid  Requestid(FK)  ResponseCode  
    1           1              A   
    2           1              V
    3           1              M   
    4           2              A
    5           2              S   
    6           2              D
    .           .              .
    .           .              .

如果收到响应代码A和D(例如在我的示例中ID为2的请求),则该请求被视为“通过”。 我想编写一个SQL查询,该查询将返回3件事:

  • “通过”请求的数量
  • 那些通过的请求的RequestID
  • 那些未通过的请求的RequestID

我写了一些东西,但我不喜欢它,我想有更好的方法。 我的查询是:

SELECT COUNT(*) 
FROM
(
  SELECT count(*) as c, req.RequestID
  FROM Responses res inner join Requests req 
  on req.RequestID = res.RequestID
  where 
      res.ResponseCode = 'A' or
      res.ResponseCode = 'D' 

  group by req.RequestID
)cc

where c = 2

提前致谢。

要返回同时包含响应代码A和D的请求ID,一种方法是使用HAVINGCOUNT

SELECT res.requestId
FROM Responses res inner join Requests req 
  on req.RequestID = res.requestid
WHERE res.ResponseCode IN ('A','D')
GROUP BY res.requestId
HAVING COUNT(DISTINCT res.ResponseCode) = 2

SQL小提琴演示

我想不出另一种根本不同的方法(即没有子查询或CTE,这是等效的)。 但是,我将分别显式测试“ A”和“ D”值:

SELECT COUNT(*) as c, req.RequestID
FROM Responses res inner join
     Requests req 
     on req.RequestID = res.EquifaxIDCompareRequestID
where res.ResponseCode in ('A', 'D')
group by EquifaxIDCompareRequestID
having SUM(case when res.ResponseCode = 'D' then 1 else 0 end) > 0 and
       SUM(case when res.ResponseCode = 'A' then 1 else 0 end)

having子句与您的外部where子句相似,除了要检查结果集中是否包含“ A”和“ D”。

还有另一种使用联接或子查询的方法。 像这样:

select EquifaxIDCompareRequestID
from Requests req
where EquifaxIDCompareRequestID in (select RequestID from responses where ResponseCode = 'A') and
      EquifaxIDCompareRequestID in (select RequestID from responses where ResponseCode = 'D') 

通过

SELECT r1.RequestID
FROM Responses R1
JOIN Responses R2
  on R2.RequestID = R1.RequestID
 and R1.ResponseCode = 'A' 
 and R2.ResponseCode = 'D'
compute count(r1.RequestID)

(对计算语法不是肯定的)

没有通过

SELECT distinct (r1.RequestID)
FROM Responses R1
FULL OUTTER JOIN Responses R2
  on R2.RequestID = R1.RequestID
 and R1.ResponseCode = 'A' 
 and R2.ResponseCode = 'D'
WHERE R2.RequestID is null 
   OR R1.RequestID is null

通过

SELECT r1.RequestID
FROM  Responses R1
WHERE R1.ResponseCode = 'A' 
INTERSECT 
SELECT r1.RequestID
FROM  Responses R1
WHERE R1.ResponseCode = 'D'

没有通过

SELECT r1.RequestID
FROM  Responses R1
EXCEPT
SELECT r1.RequestID
FROM  Responses R1
WHERE R1.ResponseCode = 'D'
   OR R1.ResponseCode = 'A' 

下面为每个requestId给出了是否通过以及请求所在组中的总数(通过或失败)(即,如果通过,则为通过的请求总数;如果失败,则为总数)失败的请求数)。

with myCTE as
(
    select rq.requestId, case when rA.responseCode='A' and rD.responseCode='D' then 1 else 0 end as passed
    from dbo.Requests as rq
    outer apply
    (
        select top (1) re.responseCode
        from dbo.Responses as re
        where re.requestId=rq.requestId
            and re.responseCode='A'
    ) as rA
    outer apply
    (
        select top (1) re.responseCode
        from dbo.Responses as re
        where re.requestId=rq.requestId
            and re.responseCode='D'
    ) as rD
)
select *, count(*) over (partition by m.passed) as totalNumInGroup
from myCTE as m

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM