我写了一个查询以获得我需要的结果。现在我需要从该结果中根据其他一些列的分组从正值中减去负值。
我的查询:
Select ISNULL(SUM(QTYH),0) as QTY,
ISNULL(SUM(AMTH),0)as AMT,
PART_NBR, A_NBR, DATE
From (
Select PART_NBR,
R.A_NBR,
Case
when PO_DEBIT_CREDIT = 'H'
then - QTY else QTY
end as QTYH,
Case
when PO_DEBIT_CREDIT='H'
then -AMT else AMT
end as AMTH,
DATEPART(yyyy,R.DATE) as DATE,
PO_DEBIT_CREDIT
FROM Receipts as R,Master as M,Vendors as t
WHERE R.A_NBR = 'A-10'
AND CAST(DATEPART(yyyy,R.DATE)as varchar) >= '2013'
AND CAST(DATEPART(yyyy,R.DATE)as varchar) <= '2013'
AND R.A_NBR = M.A_NBR
GROUP BY PART_NBR,
R.A_NBR,
DATEPART(yyyy,R.DATE), PO_DEBIT_CREDIT,QTY,AMT) as tb1
Group by PART_NBR, A_NBR, DATE, PO_DEBIT_CREDIT
结果:
QTY AMT PART_NBR A_NBR Date
=====================================
-1 -11208.58 101 A-10 2013
8 89668.64 101 A-10 2013
3 46362.42 102 A-10 2013
我希望根据最后3列PART_NBR, A_NBR, Date
将行分组在一起,并根据该分组将前2列的值加在一起。
期望的输出:
QTY AMT PART_NBR A_NBR Date
7 78460.06 101 A-10 2013
3 46362.42 102 A-10 2013
答案 0 :(得分:0)
这可能是一个解决方案
Select ISNULL(SUM(QTYH),0) as QTY, ISNULL(SUM(AMTH),0)as AMT, PART_NBR, A_NBR, DATE From (
Select PART_NBR, R.A_NBR, Case when PO_DEBIT_CREDIT = 'H' then - QTY else QTY end as QTYH, Case when PO_DEBIT_CREDIT='H' then -AMT else AMT end as AMTH, DATEPART(yyyy,R.DATE) as DATE, PO_DEBIT_CREDIT
FROM Receipts as R,Master as M,Vendors as t
WHERE R.A_NBR = 'A-10' AND CAST(DATEPART(yyyy,R.DATE)as varchar) >= '2013' AND CAST(DATEPART(yyyy,R.DATE)as varchar) <= '2013' AND R.A_NBR = M.A_NBR
GROUP BY PART_NBR, R.A_NBR, DATEPART(yyyy,R.DATE), PO_DEBIT_CREDIT,QTY,AMT) as tb1
Group by PART_NBR, A_NBR, DATE, PO_DEBIT_CREDIT
Having ISNULL(SUM(QTYH),0) >= 0
答案 1 :(得分:0)
将查询包装在子查询中并对其执行一些分组和聚合:
SELECT SUM(QTY) AS QTY, SUM(AMT) AS AMT, PART_NBR, A_NBR, [Date]
FROM
(SELECT.... YOUR EXISTING QUERY HERE)
GROUP BY PART_NBR, A_NBR, [Date]
这将按最后3列分组:PART_NBR, A_NBR, [Date]
并在每组的前2列中添加值,从而添加正值或负值。
这是一个从查询输出中获取结果的示例,包含在子查询中:
CREATE TABLE #YourSubquery
([QTY] int, [AMT] DECIMAL(10,2), [PART_NBR] int, [A_NBR] varchar(4), [Date] int)
;
INSERT INTO #YourSubquery
([QTY], [AMT], [PART_NBR], [A_NBR], [Date])
VALUES
(-1, -11208.58, 101, 'A-10', 2013),
(8, 89668.64, 101, 'A-10', 2013),
(3, 46362.42, 102, 'A-10', 2013)
;
SELECT SUM(QTY) AS QTY, SUM(AMT) AS AMT, PART_NBR, A_NBR, [Date]
FROM
(SELECT * FROM #YourSubquery) t
GROUP BY PART_NBR, A_NBR, [Date]
DROP TABLE #YourSubquery
<强>输出:强>
QTY AMT PART_NBR A_NBR Date
========================================
7 78460.06 101 A-10 2013
3 46362.42 102 A-10 2013
警告:这是基于您的AMT
列是数字数据类型的假设,因为您的输出显示$
符号。
答案 2 :(得分:0)
在外部选择中,您不需要使用PO_DEBIT_CREDIT进行分组。 外部组应该是
"Group by PART_NBR, A_NBR, DATE"
代替"Group by PART_NBR, A_NBR, DATE, PO_DEBIT_CREDIT"
因此修改后的查询是:
Select ISNULL(SUM(QTYH),0) as QTY, ISNULL(SUM(AMTH),0) as AMT, PART_NBR, A_NBR, DATE
From (
Select PART_NBR, R.A_NBR, Case when PO_DEBIT_CREDIT = 'H' then - QTY else QTY end as QTYH, Case when PO_DEBIT_CREDIT='H' then -AMT else AMT end as AMTH, DATEPART(yyyy,R.DATE) as DATE, PO_DEBIT_CREDIT
FROM Receipts as R,Master as M,Vendors as t
WHERE R.A_NBR = 'A-10' AND CAST(DATEPART(yyyy,R.DATE)as varchar) >= '2013' AND CAST(DATEPART(yyyy,R.DATE)as varchar) <= '2013' AND R.A_NBR = M.A_NBR
GROUP BY PART_NBR, R.A_NBR, DATEPART(yyyy,R.DATE), PO_DEBIT_CREDIT,QTY,AMT) as tb1
Group by PART_NBR, A_NBR, DATE