我有一个Oracle SQL查询,我想用它来计算几个数量列和一些描述性列的总数。总之,我想输出如下:
INDBDebnmbr INInvoicenmbr INOpenAmount
40 10 100
40 14 125
35 20 200
60 21 75
My Querystring:
SELECT TRIM(A.ACCOUNTNUMBER) AS INDBDebnmbr
, TRIM(A.VOUCHER) AS INinvoicenmbr
, A.DATE_ AS INinvoiceDate
, A.DUEDATE AS INinvoiceDueDate
, A.TXT AS INDescription
, A.EXCHANGECODE AS INCurrencyCode
, subq.AMOUNTMST AS INOriginalamount
, subq.SETTLEAMOUNTMST AS INpaidAmount
, subq.OPENAMOUNT AS INOpenAmount
FROM (
SELECT DEBTRANS.VOUCHER AS VOUCHER,
SUM(DEBTRANS.AMOUNTMST) AS AMOUNTMST,
SUM(DEBTRANS.SETTLEAMOUNTMST) AS SETTLEAMOUNTMST,
SUM(DEBTRANS.AMOUNTMST - DEBTRANS.SETTLEAMOUNTMST) AS OPENAMOUNT
FROM XAL_SUPERVISOR.DEBTRANS DEBTRANS
WHERE DEBTRANS.OPEN = 1
AND DEBTRANS.TRANSTYPE <> 9
AND (DEBTRANS.AMOUNTMST - DEBTRANS.SETTLEAMOUNTMST) <> 0
AND DEBTRANS.DATASET = 'FIK'
GROUP BY DEBTRANS.VOUCHER
) subq,
XAL_SUPERVISOR.DEBTRANS A
WHERE subq.VOUCHER = A.VOUCHER
GROUP BY A.VOUCHER,
A.ACCOUNTNUMBER,
A.DATE_,
A.DUEDATE,
A.TXT,
A.EXCHANGECODE,
subq.AMOUNTMST,
subq.SETTLEAMOUNTMST,
subq.OPENAMOUNT
但我确实
INDBDEBNMBR ININVOICENMBR ININVOICEDATE ININVOICEDUEDATE INOPENAMOUNT
10 2022903 12-2-2011 0:00 24-2-2011 0:00 110
35 2022903 11-2-2011 0:00 23-2-2011 0:00 110
20 2022903 17-2-2011 0:00 1-3-2011 0:00 110
10 2022903 14-2-2011 0:00 26-2-2011 0:00 110
29211 2022903 14-2-2011 0:00 26-2-2011 0:00 11
如何通过DEBTRANS.VOUCHER获取描述性列的正确SUMS?
提前致谢,
麦克
答案 0 :(得分:1)
您不必再加入桌面以获取其他列。我猜测问题是你没有重复表的外部实例的过滤条件。
这样做你想要的吗?
SELECT DEBTRANS.VOUCHER AS VOUCHER,
SUM(DEBTRANS.AMOUNTMST) AS AMOUNTMST,
SUM(DEBTRANS.SETTLEAMOUNTMST) AS SETTLEAMOUNTMST,
SUM(DEBTRANS.AMOUNTMST - DEBTRANS.SETTLEAMOUNTMST) AS OPENAMOUNT
TRIM(DEBTRANS.ACCOUNTNUMBER) AS INDBDebnmbr
, DEBTRANS.DATE_ AS INinvoiceDate
, DEBTRANS.DUEDATE AS INinvoiceDueDate
, DEBTRANS.TXT AS INDescription
, DEBTRANS.EXCHANGECODE AS INCurrencyCode
FROM XAL_SUPERVISOR.DEBTRANS DEBTRANS
WHERE DEBTRANS.OPEN = 1
AND DEBTRANS.TRANSTYPE <> 9
AND (DEBTRANS.AMOUNTMST - DEBTRANS.SETTLEAMOUNTMST) <> 0
AND DEBTRANS.DATASET = 'FIK'
GROUP BY DEBTRANS.VOUCHER
,DEBTRANS.ACCOUNTNUMBER
,DEBTRANS.DATE_
,DEBTRANS.DUEDATE
,DEBTRANS.TXT
,DEBTRANS.EXCHANGECODE
如果没有,那么可能这些附加列中的一个或多个没有凭证的唯一值。在这种情况下,您需要决定如何选择要为每个组显示的值。例如,您可以选择最大值,如下所示,但这对您的业务逻辑有意义吗?
SELECT DEBTRANS.VOUCHER AS VOUCHER,
SUM(DEBTRANS.AMOUNTMST) AS AMOUNTMST,
SUM(DEBTRANS.SETTLEAMOUNTMST) AS SETTLEAMOUNTMST,
SUM(DEBTRANS.AMOUNTMST - DEBTRANS.SETTLEAMOUNTMST) AS OPENAMOUNT
MAX(TRIM(DEBTRANS.ACCOUNTNUMBER)) AS INDBDebnmbr
, MAX(DEBTRANS.DATE_) AS INinvoiceDate
, MAX(DEBTRANS.DUEDATE) AS INinvoiceDueDate
, MAX(DEBTRANS.TXT) AS INDescription
, MAX(DEBTRANS.EXCHANGECODE) AS INCurrencyCode
FROM XAL_SUPERVISOR.DEBTRANS DEBTRANS
WHERE DEBTRANS.OPEN = 1
AND DEBTRANS.TRANSTYPE <> 9
AND (DEBTRANS.AMOUNTMST - DEBTRANS.SETTLEAMOUNTMST) <> 0
AND DEBTRANS.DATASET = 'FIK'
GROUP BY DEBTRANS.VOUCHER
答案 1 :(得分:-1)
在子查询subq
中,在WHERE
子句
AND DEBTRANS.VOUCHER = A.VOUCHER
这意味着主查询的凭证必须与子查询凭证编号匹配。