如何实现这个输出?

时间:2019-04-11 12:25:05

标签: sql sql-server tsql group-by aggregate-functions

我有一张表,上面有这样的数据:

async function loadPdf(){
  const response = await fetch('existing.pdf');
  const buffer = await response.arrayBuffer();
  const existingPdfDocBytes = new Uint8Array(buffer);
  const pdfDoc = PDFLib.PDFDocumentFactory.load(existingPdfDocBytes);
  return pdfDoc;
}

,例外的输出是显示带有客户编号,每个客户的txns总数,成功Txns,失败Txns的字段。请注意:

  • txnStatus 1和2分别表示“成功”和“失败”。
  • 在某些情况下,例如最后三行,客户编号可能为空或NULL

这是我尝试过的方法,但是没有得到例外的结果

create table test (transferID int, customerNumber varchar(10), txnstatus int);
insert into test
values
    (1,  1001, 1),
    (2,  1001, 2),
    (3,  1001, 1),
    (4,  1002, 2),
    (5,  1002, 1),
    (6,  1002, 2),
    (7,  1002, 1),
    (8,  1002, 1),
    (9,  1003, 2),
    (10, 1003, 1),
    (11, 1003, 1),
    (12, 1003, 1),
    (13, 1003, 1),
    (14, '  ', 1),
    (15, '  ', 2),
    (16, NULL, 2);

我希望输出为:

select customerNumber,
       count(*) over (partition by 1) as TotalTxns,
       case when txnstatus = 1 then count(txnstatus) else 0 end as successFulTrxn,
       case when txnstatus = 2 then count(txnstatus) else 0 end as failedFulTrxn
from test
group by customerNumber, txnstatus

4 个答案:

答案 0 :(得分:3)

您的逻辑有些正确,您只需要将CASE表达式放在COUNT内,而不必反过来:

SELECT customerNumber
     , COUNT(*) AS TotalTxns
     , COUNT(CASE WHEN txnstatus = 1 THEN 1 END) AS SuccessFulTxns
     , COUNT(CASE WHEN txnstatus = 2 THEN 1 END) AS FailedTxns
FROM test
GROUP BY customerNumber

请注意,没有空INT之类的东西。转换为INT时,空字符串/空格变为0。

答案 1 :(得分:2)

我插入0而不是空白,因为customerNumber是int。如果想要与预期结果相同的顺序,则如果最后需要0和NULL,则可以使用条件orderby。

  SELECT customerNumber
   , COUNT(*) AS TotalTxns
   , COUNT(CASE WHEN txnstatus = 1 THEN 1 END) AS successFulTrxn
   , COUNT(CASE WHEN txnstatus = 2 THEN 1 END) AS failedFulTrxn
FROM test
GROUP BY customerNumber
ORDER BY CASE WHEN customerNumber IS NULL THEN 100000 
          WHEN customerNumber = 0 THEN 99999
           ELSE customerNumber END  

答案 2 :(得分:1)

有条件的聚合是实现此目标的方法:

select customerNumber, count(transferID) as TotalTxns,
       sum(case when txnstatus = 1 then 1 else 0 end) as successFulTrxn,
       sum(case when txnstatus = 2 then 1 else 0 end) as failedFulTrxn
from test t
group by customerNumber; 

答案 3 :(得分:0)

如果您必须对txnstatus进行分组,显然您不能将该字段包括在分组列表中。

此外,您不能在案件内部进行计数,而不必在案件外部进行计数,也不必对状态进行计数,因为如果状态为“ 2”,则结果显然是不正确的。

我将按照以下方式处理问题。

    select customerNumber, count(*) as TotalTxns,
sum(case when txnstatus = 1 then  1 else 0 end)  as successFulTrxn,
sum(case when txnstatus = 2 then  1  else 0 end) as failedFulTrxn
from test 
group by customerNumber

它给我您想要的结果。