SELECT cmoup,it_ye,SUM(aunt) FROM (
SELECT
cmroup,
CASE
WHEN SD_UPRICE = 0 THEN
'buer'
WHEN SUBSTRING(ST_DATE,1,2) = '988889' THEN
'cacel'
WHEN SD_DELE >=SD_REQDATE THEN
'ovdue'
ELSE
'other'
END AS itetype,
OS_TY*SPRICE*S_RATE AS amount
FROM
`biz_rp_oo`
) AS tab1
GROUP BY cm_grp,ittype

通过case when 或者Sum if 语法糖把行变成列
SELECT
cm_gr AS itemo,
SUM(IF(SD_UPRICE = 0 ,OS_Q,0)) AS itemfer,
SUM(IF(SD_DELIVERY_DATE >=SD_REQUEST_DATE,OS_Q,0)) AS itr_due,
SUM(IF(SUBSTRING(SD_R,1,2) != '50' && SUBSTRING(SD_REQUEST_DATE,1,2) AS it
FROM biz_rso
GROUP BY cmoup
ORDER BY cmup
