我想整合这个查询......
select substr(prtmst.typcod, 1, 3) Article_Type,
case when substr(prtmst.typcod, 1, 3) like 'A%' then 'Accessories'
else
case when substr(prtmst.typcod, 1, 3) = 'L02' then 'Spirit'
else
case when substr(prtmst.typcod, 1, 3) = 'L03' then 'Wine'
else
case when substr(prtmst.typcod, 1, 3) = 'L04' then 'Beer & Soft Drinks'
else
case when substr(prtmst.typcod, 1, 3) = 'L05' then 'Confectionary'
else
case when substr(prtmst.typcod, 1, 3) = 'L06' then 'Food'
else
case when substr(prtmst.typcod, 1, 3) like 'P%' then 'Parfum/Cosmetics'
else
case when substr(prtmst.typcod, 1, 3) like 'T%' then 'Tester'
else
case when substr(prtmst.typcod, 1, 3) like 'X%' then 'GWP'
else
case when substr(prtmst.typcod, 1, 3) like 'Z%' then 'Procurement'
else ''
end
end
end
end
end
end
end
end
end
end as "Translation",
case when datediff(day, to_char(invdtl.fifdte, 'MM/DD/YYYY'), sysdate) < 90 then sum(invdtl.untqty)
else ''
end as "Less than 90",
case when datediff(day, to_char(invdtl.fifdte, 'MM/DD/YYYY'), sysdate) between 91
and 120 then sum(invdtl.untqty)
else ''
end as "91-120",
case when datediff(day, to_char(invdtl.fifdte, 'MM/DD/YYYY'), sysdate) between 121
and 180 then sum(invdtl.untqty)
else ''
end as "121-180",
case when datediff(day, to_char(invdtl.fifdte, 'MM/DD/YYYY'), sysdate) between 181
and 360 then sum(invdtl.untqty)
else ''
end as "181-360",
case when datediff(day, to_char(invdtl.fifdte, 'MM/DD/YYYY'), sysdate) between 361
and 500 then sum(invdtl.untqty)
else ''
end as "361-500",
case when datediff(day, to_char(invdtl.fifdte, 'MM/DD/YYYY'), sysdate) between 501
and 900 then sum(invdtl.untqty)
else ''
end as "501-900",
case when datediff(day, to_char(invdtl.fifdte, 'MM/DD/YYYY'), sysdate) > 900 then sum(invdtl.untqty)
else ''
end as "900+"
from prtmst,
invdtl
where invdtl.prtnum = prtmst.prtnum
and prtmst.prt_client_id = 'HUS'
and prtmst.wh_id_tmpl = 'MFTZ'
and prtmst.typcod is not null
and invdtl.prt_client_id = 'HUS'
and invdtl.ship_line_id is null
and invdtl.wrkref is null
and invdtl.lst_arecod not in ('ADJS', 'CADJ', 'SADJ')
group by substr(prtmst.typcod, 1, 3),
to_char(invdtl.fifdte, 'MM/DD/YYYY')
order by substr(prtmst.typcod, 1, 3) asc
我遇到的问题是结果显示如下:(413行)
article_type translation less than 90 91-120 121-180 181-360 361-500 501-900 900+
A71 Accessories 481
L02 Spirit 1296
L02 Spirit 6
L02 Spirit 96
L02 Spirit 60
L02 Spirit 2100
L02 Spirit 1014
L02 Spirit 252
L02 Spirit 318
L02 Spirit 36
L02 Spirit 192
L02 Spirit 1848
L02 Spirit 2124
L02 Spirit 1550
L02 Spirit 7547
L02 Spirit 4206
L03 Wine 96
L03 Wine 417
L03 Wine 258
L03 Wine 492
L03 Wine 348
L03 Wine 448
L03 Wine 552
L03 Wine 60
L04 Beer & Soft Drinks 1416
L05 Confectionary 19
L05 Confectionary 45
L05 Confectionary 108
L05 Confectionary 546
L05 Confectionary 1112
我希望结果如下所示:(11行)
translation less than 90 91-120 121-180 181-360 361-500 501-900 900+
A71 481
L02 19147 36 3462
L03 1452 448 771
L04 1416
L05 3666 2153 3630 2691 387 1689
P81 11460 7056 13581 19070 27626 12141
P83 525 344 253
T85 880 802 2888 3811 4732 2539
T90 20 786 36
X 5082 6
Z10 250 130 549
请帮助我....基本上,查询不会在语句分组时因为它们具有sum函数....因此,显示要在同一列中合并的多个结果...
答案 0 :(得分:2)
您应该将sum()函数应用于整个case表达式,如果在派生表中应用datediff和substr函数,则可以避免大量重复。
因此,稍微调整一下您的原始查询后,我们会得到您应该执行此操作的查询:
String str = new String (request.getParameter("string").getBytes ("iso-8859-1"), "UTF-8");
答案 1 :(得分:0)
你想把你的条件存入你的总和。改变这个:
case when datediff(day, to_char(invdtl.fifdte, 'MM/DD/YYYY'), sysdate) < 90
then sum(invdtl.untqty)
else ''
end as "Less than 90"
为:
sum(
case when datediff(day, to_char(invdtl.fifdte, 'MM/DD/YYYY'), sysdate) < 90
then invdtl.untqty
else 0
end)
as "Less than 90"
答案 2 :(得分:0)
您需要SUM
超过CASE
(并清理您的“翻译”案例:
SELECT SUBSTR(prtmst.typcod, 1, 3) Article_Type,
CASE WHEN SUBSTR(prtmst.typcod, 1, 3) LIKE 'A%' THEN 'Accessories'
WHEN SUBSTR(prtmst.typcod, 1, 3) = 'L02' THEN 'Spirit'
WHEN SUBSTR(prtmst.typcod, 1, 3) = 'L03' THEN 'Wine'
WHEN SUBSTR(prtmst.typcod, 1, 3) = 'L04' THEN 'Beer & Soft Drinks'
WHEN SUBSTR(prtmst.typcod, 1, 3) = 'L05' THEN 'Confectionary'
WHEN SUBSTR(prtmst.typcod, 1, 3) = 'L06' THEN 'Food'
WHEN SUBSTR(prtmst.typcod, 1, 3) LIKE 'P%' THEN 'Parfum/Cosmetics'
WHEN SUBSTR(prtmst.typcod, 1, 3) LIKE 'T%' THEN 'Tester'
WHEN SUBSTR(prtmst.typcod, 1, 3) LIKE 'X%' THEN 'GWP'
WHEN SUBSTR(prtmst.typcod, 1, 3) LIKE 'Z%' THEN 'Procurement'
ELSE ''
END AS "Translation",
SUM(CASE WHEN datediff(DAY, TO_CHAR(invdtl.fifdte, 'MM/DD/YYYY'), sysdate) < 90 THEN invdtl.untqty ELSE 0 END) AS "Less than 90",
SUM(CASE WHEN datediff(DAY, TO_CHAR(invdtl.fifdte, 'MM/DD/YYYY'), sysdate) BETWEEN 91 AND 120 THEN invdtl.untqty ELSE 0 END) AS "91-120",
SUM(CASE WHEN datediff(DAY, TO_CHAR(invdtl.fifdte, 'MM/DD/YYYY'), sysdate) BETWEEN 121 AND 180 THEN invdtl.untqty ELSE 0 END) AS "121-180",
SUM(CASE WHEN datediff(DAY, TO_CHAR(invdtl.fifdte, 'MM/DD/YYYY'), sysdate) BETWEEN 181 AND 360 THEN invdtl.untqty ELSE 0 END) AS "181-360",
SUM(CASE WHEN datediff(DAY, TO_CHAR(invdtl.fifdte, 'MM/DD/YYYY'), sysdate) BETWEEN 361 AND 500 THEN invdtl.untqty ELSE 0 END) AS "361-500",
SUM(CASE WHEN datediff(DAY, TO_CHAR(invdtl.fifdte, 'MM/DD/YYYY'), sysdate) BETWEEN 501 AND 900 THEN invdtl.untqty ELSE 0 END) AS "501-900",
SUM(CASE WHEN datediff(DAY, TO_CHAR(invdtl.fifdte, 'MM/DD/YYYY'), sysdate) > 900 THEN invdtl.untqty ELSE 0 END) AS "900+"
FROM prtmst,
invdtl
WHERE invdtl.prtnum = prtmst.prtnum
AND prtmst.prt_client_id = 'HUS'
AND prtmst.wh_id_tmpl = 'MFTZ'
AND prtmst.typcod IS NOT NULL
AND invdtl.prt_client_id = 'HUS'
AND invdtl.ship_line_id IS NULL
AND invdtl.wrkref IS NULL
AND invdtl.lst_arecod NOT IN ('ADJS', 'CADJ', 'SADJ')
GROUP BY SUBSTR(prtmst.typcod, 1, 3),
TO_CHAR(invdtl.fifdte, 'MM/DD/YYYY')
ORDER BY SUBSTR(prtmst.typcod, 1, 3) ASC