在SQL Server查询中计算多行

时间:2009-06-29 05:24:32

标签: sql sql-server count

我有一个名为Online_Transaction的表,我希望显示特定月份中所有类型的事务以及每种类型(已完成,已拒绝,待批准)的数量。直到现在我才有这个问题,但我反复得到任何想法吗?

SELECT DISTINCT
    TRANSACTION_TYPE_ID ,
    YEAR(CREATED_ON) AS YEAR ,
    MONTH(CREATED_ON) AS MONTH ,
    ( SELECT
          Count(TRANSACTION_TYPE_ID)
      FROM
          ONLINE_TRANSACTION
      WHERE
          ONLINE_TRANSACTION.STATUS_ID = 'COMPLETED' AND MONTH(CREATED_ON) = '2' ) AS COMPLETED ,
    ( SELECT
          Count(TRANSACTION_TYPE_ID)
      FROM
          ONLINE_TRANSACTION
      WHERE
          ONLINE_TRANSACTION.STATUS_ID = 'DECLINED' AND MONTH(CREATED_ON) = '2' ) AS DECLINED ,
    ( SELECT
          Count(TRANSACTION_TYPE_ID)
      FROM
          ONLINE_TRANSACTION
      WHERE
          ONLINE_TRANSACTION.STATUS_ID = 'FAILED' AND MONTH(CREATED_ON) = '2' ) AS FAILED ,
    ( SELECT
          Count(TRANSACTION_TYPE_ID)
      FROM
          ONLINE_TRANSACTION
      WHERE
          ONLINE_TRANSACTION.STATUS_ID = 'PENDING_AUTH' AND MONTH(CREATED_ON) = '2' ) AS PENDING_AUTH
--(SELECT Count(*) from )
FROM
    ONLINE_TRANSACTION
WHERE
    MONTH(CREATED_ON) = '2'
GROUP BY
    TRANSACTION_TYPE_ID ,
    ONLINE_TRANSACTION.CREATED_ON    

我得到了这些结果:

TRANSACTION_TYPE_ID                  YEAR        MONTH       COMPLETED   DECLINED    FAILED      
------------------------------------ ----------- ----------- ----------- ----------- -------
INSURANCE--TYPE                       2009        2           9712        177         0           
CHEQUEBOOK-TYPE                       2009        2           9712        177         0           
CHEQUE-STOP-YPE                       2009        2           9712        177         0           
PAYMENT-TRANS-TYPE               2009        2           9712        177         0           
DOMESTIC-TRANSFER-TYPE                2009        2           9712        177         0
PAYMENT-TRANS-TYPE                2009        2           9712        177         0           
INTRA-ACCOUNT-TRANS-TYPE              2009        2           9712        177         0           
INTRA-BANK-TRANS-TYPE                 2009        2           9712        177         0           
STANDING-ORDER-TYPE                   2009        2           9712        177         0           
STATEMENT-REORDERING TYPE             2009        2           9712        177         0           
PAYMENTS-TRANS-TYPE             2009        2           9712        177         0           

如您所见,结果重复出现,表中应该是不同的值 有什么想法吗?


尝试了下面的答案,这就是我得到的:  这种类型的结果集 - 它为不同行中的每种交易类型显示不同的结果,所以如果说交易类型是内部转移,我有10个完成,2个被拒绝,它将显示完成一行而被拒绝在另一行?如何在每个交易类型的一行中显示它

TRANSACTION_TYPE_ID        YEAR MONTH REJECTED COMPLETED POSTED

ALPHA-INSURANCE-TRANS-TYPE  2009 2       0 12 0 
CHEQUEBOOK-ORDER-TRANS-TYPE 2009 2      0 0 0 
CHEQUEBOOK-ORDER-TRANS-TYPE 2009 2      0 52 0 
CHEQUEBOOK-ORDER-TRANS-TYPE 2009 2      2 0 0 
CHEQUE-STOP-TRANS-TYPE      2009 2      0 3 0
 PAYMENT-TRANS-TYPE         2009 2     0 361 0 
PAYMENT-TRANS-TYPE          2009 2     1 0 0 
DOMESTIC-TRANSFER-TRANS-TYPE 2009 2    0 0 0 
DOMESTIC-TRANSFER-TRANS-TYPE 2009 2   0 541 0 
DOMESTIC-TRANSFER-TRANS-TYPE 2009 2    6 0 0

查询如下所示:

SELECT DISTINCT
    TRANSACTION_TYPE_ID ,
    YEAR(CREATED_ON) AS YEAR ,
    MONTH(CREATED_ON) AS MONTH ,
    SUM(CASE ONLINE_TRANSACTION.STATUS_ID
          WHEN 'STATUS-TRANS-REJECTED ' THEN 1
          ELSE 0
        END) AS REJECTED ,
    SUM(CASE ONLINE_TRANSACTION.STATUS_ID
          WHEN 'STATUS-TRANS-COMPLETED ' THEN 1
          ELSE 0
        END) AS COMPLETED ,
    SUM(CASE ONLINE_TRANSACTION.STATUS_ID
          WHEN 'STATUS-TRANS-DECLINDED ' THEN 1
          ELSE 0
        END) AS DECLINED ,
    SUM(CASE ONLINE_TRANSACTION.STATUS_ID
          WHEN 'STATUS-TRANS-FAILED' THEN 1
          ELSE 0
        END) AS FAILED ,
    SUM(CASE ONLINE_TRANSACTION.STATUS_ID
          WHEN 'STATUS-TRANS-PENDING-AUTH ' THEN 1
          ELSE 0
        END) AS PENDING_AUTH ,
    SUM(CASE ONLINE_TRANSACTION.STATUS_ID
          WHEN 'STATUS-TRANS-PENDING-POST ' THEN 1
          ELSE 0
        END) AS PENDING_POST ,
    SUM(CASE ONLINE_TRANSACTION.STATUS_ID
          WHEN 'STATUS-TRANS-PENDING' THEN 1
          ELSE 0
        END) AS PENDING ,
    SUM(CASE ONLINE_TRANSACTION.STATUS_ID
          WHEN 'ALPHA-STATUS-TRANS-POSTED' THEN 1
          ELSE 0
        END) AS POSTED
FROM
    ONLINE_TRANSACTION
WHERE
    MONTH(CREATED_ON) = '2'
GROUP BY
    TRANSACTION_TYPE_ID ,
    YEAR(CREATED_ON) ,
    MONTH(CREATED_ON) ,
    STATUS_ID

5 个答案:

答案 0 :(得分:2)

我尝试了other answers只是为了我自己的启发,并认为我会分享结果,以防它帮助其他人。数据集很小,但显示了原则。

sqlite> SELECT * FROM transactions;
id          type_id      status_id
----------  ----------   ----------
1           insurance    completed
2           insurance    declined
3           cheque-stop  completed
4           cheque-stop  completed

sqlite> SELECT
   ...>     type_id,
   ...>     SUM(status_id == 'completed') AS completed,
   ...>     SUM(status_id == 'declined') AS declined
   ...> FROM transactions
   ...> GROUP BY type_id;
type_id      completed   declined
-----------  ----------  ----------
cheque-stop  2           0
insurance    1           1

答案 1 :(得分:0)

您的子查询中需要另一个WHERE子句链接TRANSACTION_TYPE_IDs。

SELECT DISTINCT 
  TRANSACTION_TYPE_ID, YEAR(CREATED_ON)AS YEAR, MONTH(CREATED_ON)AS MONTH ,
(SELECT Count(TRANSACTION_TYPE_ID)  FROM ONLINE_TRANSACTION 
 WHERE ONLINE_TRANSACTION.STATUS_ID ='COMPLETED'AND MONTH(CREATED_ON)='2'
    AND TRANSACTION_TYPE_ID=ot.TRANSACTION_TYPE_ID) AS COMPLETED,
(SELECT Count(TRANSACTION_TYPE_ID) FROM ONLINE_TRANSACTION 
 WHERE ONLINE_TRANSACTION.STATUS_ID ='DECLINED'AND MONTH(CREATED_ON)='2'
    AND TRANSACTION_TYPE_ID=ot.TRANSACTION_TYPE_ID) AS DECLINED,
(SELECT Count(TRANSACTION_TYPE_ID) from ONLINE_TRANSACTION
 WHERE  ONLINE_TRANSACTION.STATUS_ID ='FAILED' AND MONTH(CREATED_ON)='2'
    AND TRANSACTION_TYPE_ID=ot.TRANSACTION_TYPE_ID) AS FAILED,
(SELECT Count(TRANSACTION_TYPE_ID) from ONLINE_TRANSACTION
 WHERE  ONLINE_TRANSACTION.STATUS_ID ='PENDING_AUTH'AND MONTH(CREATED_ON)='2'
    AND TRANSACTION_TYPE_ID=ot.TRANSACTION_TYPE_ID) AS PENDING_AUTH
FROM ONLINE_TRANSACTION ot
WHERE MONTH(CREATED_ON)='2'
GROUP BY TRANSACTION_TYPE_ID,ONLINE_TRANSACTION.CREATED_ON

答案 2 :(得分:0)

嵌套select语句中的where子句不区分不同的事务类型。数字总是总金额。

尝试查看总和和案例。

TRANSACTION_TYPE_ID, YEAR(CREATED_ON)AS YEAR, MONTH(CREATED_ON)AS MONTH ,
  SUM(CASE ONLINE_TRANSACTION.STATUS_ID WHEN 'COMPLETED' THEN 1 ELSE 0 END) AS COMPLETED,
  SUM(CASE ONLINE_TRANSACTION.STATUS_ID WHEN 'DECLINED' THEN 1 ELSE 0 END) AS DECLINED,
  SUM(CASE ONLINE_TRANSACTION.STATUS_ID WHEN 'FAILED' THEN 1 ELSE 0 END) AS FAILED,
  SUM(CASE ONLINE_TRANSACTION.STATUS_ID WHEN 'PENDING_AUTH' THEN 1 ELSE 0 END) AS PENDING_AUTH
FROM ONLINE_TRANSACTION WHERE MONTH(CREATED_ON)='2'
GROUP BY TRANSACTION_TYPE_ID  

答案 3 :(得分:0)

您的主SELECT条款中的括号SELECT会回到ONLINE_TRANSACTION总表中,因此当然他们会在不考虑类型的情况下得到他们的计数,而是整体而言!

如何将SUM更改为STATUS_ID(通过相同的分组,因此不需要额外的语法)IF检查 - 取1表示相等,0表示差异( bool到0/1 int转换在许多SQL方言中是自动的,在最坏的情况下,无论你选择何种方言,你都需要CASE...WHENCAST或{{1}}。< / p>

答案 4 :(得分:0)

SELECT TRANSACTION_TYPE_ID, YEAR(CREATED_ON) AS YEAR, MONTH(CREATED_ON) AS MONTH , 
SUM(CASE WHEN STATUS_ID = 'COMPLETED' THEN 1 ELSE 0 END) AS Completed,
SUM(CASE WHEN STATUS_ID = 'DECLINED' THEN 1 ELSE 0 END) AS Declined,
SUM(CASE WHEN STATUS_ID = 'FAILED' THEN 1 ELSE 0 END) AS Failed,
SUM(CASE WHEN STATUS_ID = 'PENDING_AUTH' THEN 1 ELSE 0 END) AS Pending_Auth
FROM ONLINE_TRANSACTION
WHERE MONTH(CREATED_ON) = 2
GROUP BY TRANSACTION_TYPE_ID, YEAR(CREATED_ON), MONTH(CREATED_ON)

看看这是否有意义。