我的要求是仅按TERMS_ID
分组,但仍能看到许多TERMS_ID
个数字。在这里,我希望根据TERMS_ID
对特定日期完成的所有记录进行分组。
寻找一些意见。
SELECT FL.TERMS_ID,FR.CCARD_ACPT_NAME_LOC,
((FR.BOX_ITEM_VALUE0 * FR.BOX_NO_ITEMS_DISP0) +
(FR.BOX_ITEM_VALUE1 * FR.BOX_NO_ITEMS_DISP1) +
(FR.BOX_ITEM_VALUE2 * FR.BOX_NO_ITEMS_DISP2) +
(FR.BOX_ITEM_VALUE3 * FR.BOX_NO_ITEMS_DISP3) +
(FR.BOX_ITEM_VALUE4 * FR.BOX_NO_ITEMS_DISP4) +
(FR.BOX_ITEM_VALUE5 * FR.BOX_NO_ITEMS_DISP5) +
(FR.BOX_ITEM_VALUE6 * FR.BOX_NO_ITEMS_DISP6) +
(FR.BOX_ITEM_VALUE7 * FR.BOX_NO_ITEMS_DISP7)) AS "CASH DISB",
FR.BOX_ITEM_VALUE0 AS "DENOM 1" ,FR.BOX_ITEM_VALUE0 * FR.BOX_NO_ITEMS_DISP0 AS "AMT DISBURSED1",
FR.BOX_ITEM_VALUE1 AS "DENOM 2" ,FR.BOX_ITEM_VALUE1 * FR.BOX_NO_ITEMS_DISP1 AS "AMT DISBURSED2",
FR.BOX_ITEM_VALUE2 AS "DENOM 3" ,FR.BOX_ITEM_VALUE2 * FR.BOX_NO_ITEMS_DISP2 AS "AMT DISBURSED3",
FR.BOX_ITEM_VALUE3 AS "DENOM 4" ,FR.BOX_ITEM_VALUE3 * FR.BOX_NO_ITEMS_DISP3 AS "AMT DISBURSED4",
FR.BOX_ITEM_VALUE4 AS "DENOM 5" ,FR.BOX_ITEM_VALUE4 * FR.BOX_NO_ITEMS_DISP4 AS "AMT DISBURSED5",
FR.BOX_ITEM_VALUE5 AS "DENOM 6" ,FR.BOX_ITEM_VALUE5 * FR.BOX_NO_ITEMS_DISP5 AS "AMT DISBURSED6",
FR.BOX_ITEM_VALUE6 AS "DENOM 7" ,FR.BOX_ITEM_VALUE6 * FR.BOX_NO_ITEMS_DISP6 AS "AMT DISBURSED7",
FR.BOX_ITEM_VALUE7 AS "DENOM 8" ,FR.BOX_ITEM_VALUE7 * FR.BOX_NO_ITEMS_DISP7 AS "AMT DISBURSED8"
FROM SSPNGT.LOG_LIST201706 AS FL
JOIN SSPNGT.LOG_RECORD201706 AS FR ON FL.TSTAMP_TRANS=FR.TSTAMP_TRANS
WHERE FR.TSTAMP_TRANS LIKE'20170622%'
GROUP BY FL.TERMS_ID,FR.CCARD_ACPT_NAME_LOC,
FR.BOX_ITEM_VALUE0,FR.BOX_ITEM_VALUE1,FR.BOX_ITEM_VALUE2,FR.BOX_ITEM_VALUE3,FR.BOX_ITEM_VALUE4,FR.BOX_ITEM_VALUE5,FR.BOX_ITEM_VALUE6,FR.BOX_ITEM_VALUE7,
FR.BOX_NO_ITEMS_DISP0,FR.BOX_NO_ITEMS_DISP1,FR.BOX_NO_ITEMS_DISP2,FR.BOX_NO_ITEMS_DISP3,FR.BOX_NO_ITEMS_DISP4,FR.BOX_NO_ITEMS_DISP5,FR.BOX_NO_ITEMS_DISP6,FR.BOX_NO_ITEMS_DISP7;
结果:
TERMS_ID CCARD_ACPT_NAME_LOC CASH DISB --------
------------------------------------------------
TERM101 BANGALORE-1 150 --------
TERM101 BANGALORE-1 170 --------
TERM101 BANGALORE-1 500 --------
TERM200 CHENNAI-1 300 --------
TERM200 CHENNAI-1 200 --------
TERM301 CHENNAI-21 600 --------
TERM301 CHENNAI-21 200 --------
TERM301 CHENNAI-21 100 --------
我正在寻找以下结果:
TERMS_ID CCARD_ACPT_NAME_LOC CASH DISB --------
------------------------------------------------
TERM101 BANGALORE-1 820 --------
TERM200 CHENNAI-1 500 --------
TERM301 CHENNAI-21 900 --------
答案 0 :(得分:0)
你可以使用sql-server的SUM()函数
SELECT FL.TERMS_ID,FR.CCARD_ACPT_NAME_LOC,
SUM
(
((FR.BOX_ITEM_VALUE0 * FR.BOX_NO_ITEMS_DISP0) +
(FR.BOX_ITEM_VALUE1 * FR.BOX_NO_ITEMS_DISP1) +
(FR.BOX_ITEM_VALUE2 * FR.BOX_NO_ITEMS_DISP2) +
(FR.BOX_ITEM_VALUE3 * FR.BOX_NO_ITEMS_DISP3) +
(FR.BOX_ITEM_VALUE4 * FR.BOX_NO_ITEMS_DISP4) +
(FR.BOX_ITEM_VALUE5 * FR.BOX_NO_ITEMS_DISP5) +
(FR.BOX_ITEM_VALUE6 * FR.BOX_NO_ITEMS_DISP6) +
(FR.BOX_ITEM_VALUE7 * FR.BOX_NO_ITEMS_DISP7))
)
AS "CASH DISB",
FR.BOX_ITEM_VALUE0 AS "DENOM 1" ,FR.BOX_ITEM_VALUE0 * FR.BOX_NO_ITEMS_DISP0 AS "AMT DISBURSED1",
FR.BOX_ITEM_VALUE1 AS "DENOM 2" ,FR.BOX_ITEM_VALUE1 * FR.BOX_NO_ITEMS_DISP1 AS "AMT DISBURSED2",
FR.BOX_ITEM_VALUE2 AS "DENOM 3" ,FR.BOX_ITEM_VALUE2 * FR.BOX_NO_ITEMS_DISP2 AS "AMT DISBURSED3",
FR.BOX_ITEM_VALUE3 AS "DENOM 4" ,FR.BOX_ITEM_VALUE3 * FR.BOX_NO_ITEMS_DISP3 AS "AMT DISBURSED4",
FR.BOX_ITEM_VALUE4 AS "DENOM 5" ,FR.BOX_ITEM_VALUE4 * FR.BOX_NO_ITEMS_DISP4 AS "AMT DISBURSED5",
FR.BOX_ITEM_VALUE5 AS "DENOM 6" ,FR.BOX_ITEM_VALUE5 * FR.BOX_NO_ITEMS_DISP5 AS "AMT DISBURSED6",
FR.BOX_ITEM_VALUE6 AS "DENOM 7" ,FR.BOX_ITEM_VALUE6 * FR.BOX_NO_ITEMS_DISP6 AS "AMT DISBURSED7",
FR.BOX_ITEM_VALUE7 AS "DENOM 8" ,FR.BOX_ITEM_VALUE7 * FR.BOX_NO_ITEMS_DISP7 AS "AMT DISBURSED8"
FROM SSPNGT.LOG_LIST201706 AS FL
JOIN SSPNGT.LOG_RECORD201706 AS FR ON FL.TSTAMP_TRANS=FR.TSTAMP_TRANS
WHERE FR.TSTAMP_TRANS LIKE'20170622%'
GROUP BY FL.TERMS_ID,FR.CCARD_ACPT_NAME_LOC,
FR.BOX_ITEM_VALUE0,FR.BOX_ITEM_VALUE1,FR.BOX_ITEM_VALUE2,FR.BOX_ITEM_VALUE3,FR.BOX_ITEM_VALUE4,FR.BOX_ITEM_VALUE5,FR.BOX_ITEM_VALUE6,FR.BOX_ITEM_VALUE7,
FR.BOX_NO_ITEMS_DISP0,FR.BOX_NO_ITEMS_DISP1,FR.BOX_NO_ITEMS_DISP2,FR.BOX_NO_ITEMS_DISP3,FR.BOX_NO_ITEMS_DISP4,FR.BOX_NO_ITEMS_DISP5,FR.BOX_NO_ITEMS_DISP6,FR.BOX_NO_ITEMS_DISP7;
答案 1 :(得分:0)
知道它运行正常,我不会弄乱(并打破)你的原始查询。所以我会保留它作为您的数据源,并将其结果按terms_id分组。
with MY_DATA as
(
SELECT FL.TERMS_ID,FR.CCARD_ACPT_NAME_LOC,
((FR.BOX_ITEM_VALUE0 * FR.BOX_NO_ITEMS_DISP0) +
(FR.BOX_ITEM_VALUE1 * FR.BOX_NO_ITEMS_DISP1) +
(FR.BOX_ITEM_VALUE2 * FR.BOX_NO_ITEMS_DISP2) +
(FR.BOX_ITEM_VALUE3 * FR.BOX_NO_ITEMS_DISP3) +
(FR.BOX_ITEM_VALUE4 * FR.BOX_NO_ITEMS_DISP4) +
(FR.BOX_ITEM_VALUE5 * FR.BOX_NO_ITEMS_DISP5) +
(FR.BOX_ITEM_VALUE6 * FR.BOX_NO_ITEMS_DISP6) +
(FR.BOX_ITEM_VALUE7 * FR.BOX_NO_ITEMS_DISP7)) AS "CASH DISB",
FR.BOX_ITEM_VALUE0 AS "DENOM 1" ,FR.BOX_ITEM_VALUE0 * FR.BOX_NO_ITEMS_DISP0 AS "AMT DISBURSED1",
FR.BOX_ITEM_VALUE1 AS "DENOM 2" ,FR.BOX_ITEM_VALUE1 * FR.BOX_NO_ITEMS_DISP1 AS "AMT DISBURSED2",
FR.BOX_ITEM_VALUE2 AS "DENOM 3" ,FR.BOX_ITEM_VALUE2 * FR.BOX_NO_ITEMS_DISP2 AS "AMT DISBURSED3",
FR.BOX_ITEM_VALUE3 AS "DENOM 4" ,FR.BOX_ITEM_VALUE3 * FR.BOX_NO_ITEMS_DISP3 AS "AMT DISBURSED4",
FR.BOX_ITEM_VALUE4 AS "DENOM 5" ,FR.BOX_ITEM_VALUE4 * FR.BOX_NO_ITEMS_DISP4 AS "AMT DISBURSED5",
FR.BOX_ITEM_VALUE5 AS "DENOM 6" ,FR.BOX_ITEM_VALUE5 * FR.BOX_NO_ITEMS_DISP5 AS "AMT DISBURSED6",
FR.BOX_ITEM_VALUE6 AS "DENOM 7" ,FR.BOX_ITEM_VALUE6 * FR.BOX_NO_ITEMS_DISP6 AS "AMT DISBURSED7",
FR.BOX_ITEM_VALUE7 AS "DENOM 8" ,FR.BOX_ITEM_VALUE7 * FR.BOX_NO_ITEMS_DISP7 AS "AMT DISBURSED8"
FROM SSPNGT.LOG_LIST201706 AS FL
JOIN SSPNGT.LOG_RECORD201706 AS FR ON FL.TSTAMP_TRANS=FR.TSTAMP_TRANS
WHERE FR.TSTAMP_TRANS LIKE'20170622%'
GROUP BY FL.TERMS_ID,FR.CCARD_ACPT_NAME_LOC,
FR.BOX_ITEM_VALUE0,FR.BOX_ITEM_VALUE1,FR.BOX_ITEM_VALUE2,FR.BOX_ITEM_VALUE3,FR.BOX_ITEM_VALUE4,FR.BOX_ITEM_VALUE5,FR.BOX_ITEM_VALUE6,FR.BOX_ITEM_VALUE7,
FR.BOX_NO_ITEMS_DISP0,FR.BOX_NO_ITEMS_DISP1,FR.BOX_NO_ITEMS_DISP2,FR.BOX_NO_ITEMS_DISP3,FR.BOX_NO_ITEMS_DISP4,FR.BOX_NO_ITEMS_DISP5,FR.BOX_NO_ITEMS_DISP6,FR.BOX_NO_ITEMS_DISP7
)
select TERMS_ID, CCARD_ACPT_NAME_LOC, sum(CASH DISB) as CASH_DIB
from MY_DATA
group by TERMS_ID, CCARD_ACPT_NAME_LOC
根据您发布的结果,这必须返回您需要的结果。