我有2個表cardinfo和paytb。在我的發言中,我通過CONT_ID加入了他們。select case statement add information
select (case when age_years >= 18 and age_years < 30 then '18-29'
when age_years < 50 then '30-49'
when age_years < 70 then '50-69'
when age_years < 100 then '70-100'
end) as age_range,
count(DISTINCT c.CONT_ID) as num,
CASE WHEN GENDER = '1' THEN 1 ELSE 0 END/COUNT(DISTINCT c.CONT_ID),
SUM(ACAUREQ_AUREQ_TX_DT_TTLAMT)/COUNT(*) 'TOTAL-Amount Avg'
from cardinfo c
left join paytb t
on c.CONT_ID = t.CONT_ID
group by (case when age_years >= 18 and age_years < 30 then '18-29'
when age_years < 50 then '30-49'
when age_years < 70 then '50-69'
when age_years < 100 then '70-100'
end)
order by min(age_years);
現在,這示出了第一列2:
AGE_RANGE NUM GENDER
---------+---------+----------------
18-29 828 50%
30-49 2510 ??
50-69 2014 ??
70-100 649
我要添加性別參數(0或1)從表中。然而,這是不工作:
CASE WHEN GENDER = '1' THEN 1 ELSE 0 END/COUNT(DISTINCT c.CONT_ID)
SQLCODE = -122
而且我tryed找到交易的平均:
SUM(ACAUREQ_AUREQ_TX_DT_TTLAMT)/COUNT(*) 'TOTAL-Amount Avg'
不幸的是這會給我一個DECFLOAT錯誤。
總和看起來是這樣的:
SUM(ACAUREQ_AUREQ_TX_DT_TTLAMT)
-----+---------+---------+---------+
+1.232071426000000000000000E+0007
+3.062581021000000000000000E+0007
+2.399951792000000000000000E+0007
+8.228919170000000000000000E+0006
這工作了,我將有爲此添加更多列。像最常見的ACAUREQ_AUREQ_ENV_M_CMONNM - (共同商家名稱)一樣。那看起來怎麼樣? – bastel
@bastel。 。 。我不確定你想要什麼,但是「SUM(CASE ...)」和「AVG(CASE ...)」是一個常見的結構。 –