[英]Oracle SQL Developer Query to return the count of 2 values from same column
I am trying to count the number of two types of transactions in a column and to return a value of 0 if there are none. 我试图计算一列中两种交易的数量,如果没有则返回0值。 The table is Transaction, the columns I want returned are the account number and a column for the count each of the two transactions listed.
该表是“交易”,我要返回的列是帐号,并且列出了两个交易中每一个的计数列。 here's what I have, but it doesn't run:
这是我所拥有的,但无法运行:
SELECT
ACCTNBR,
COUNT(CASE when RTXNTYPCD='XDEP'then 1 else 0) AS Deposits,
COUNT(CASE when RTXNTYPCD='PWTH'then 1 else 0) AS Debits
FROM
TRANSACTION
WHERE
((POSTDATE BETWEEN TO_DATE('05-01-2014','MM-DD-YYYY')) AND TO_DATE('05-31-2014','MM-DD-YYYY'))
and ACCTNBR in ( 406, 1206, 1347, 4556, 6668, 9845)
GROUP BY
ACCTNBR
The count
function counts any value that isn't null
. count
函数对任何不为null
值进行计数。 Since both 0
and 1
are not null
, both your count
s will return the total number of rows in your table. 由于
0
和1
都不为null
,所以两个count
都将返回表中的总行数。 Instead, you should use sum
: 相反,您应该使用
sum
:
SELECT
ACCTNBR,
SUM(CASE when RTXNTYPCD='XDEP' then 1 else 0 END) AS Deposits,
SUM(CASE when RTXNTYPCD='PWTH' then 1 else 0 END) AS Debits
FROM
TRANSACTION
WHERE
((POSTDATE BETWEEN TO_DATE('05-01-2014','MM-DD-YYYY')) AND
TO_DATE('05-31-2014','MM-DD-YYYY'))
and ACCTNBR in ( 406, 1206, 1347, 4556, 6668, 9845)
GROUP BY
ACCTNBR
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.