我希望能够使用客户的生日获得下面每个年龄组的计数。客户还必须满足某些条件。我需要使用2个表。
18 to 25
26 to 35
36 to 45
46 to 55
56 to 65
65 and over
使用下表和列:
BRTH_DT - birthday
MSTR_CUST - customer table where BRTH_DT is
MSTR_ACT - account table where BR_CD is
BR_CD - code , I want to include all customer with code like '%00%'
CUST_ODT - customer open date from customer table, I want to include dates between '2017-11-01' and '2017-11-30'
我尝试了一些语法,但它没有用。我正在使用Hadoop。
查询:
SELECT (
CASE
WHEN EXTRACT (YEAR FROM (select sysdate from dual)) - EXTRACT (YEAR FROM BRTH_DT) BETWEEN 18 AND 25
THEN '18-25'
ELSE
CASE
WHEN EXTRACT (YEAR FROM (select sysdate from dual)) - EXTRACT (YEAR FROM BRTH_DT) BETWEEN 26 AND 35
THEN '26-35'
ELSE
CASE
WHEN EXTRACT (YEAR FROM (select sysdate from dual)) - EXTRACT (YEAR FROM BRTH_DT) BETWEEN 36 AND 45
THEN '36-45'
ELSE
CASE
WHEN EXTRACT (YEAR FROM (select sysdate from dual)) - EXTRACT (YEAR FROM BRTH_DT) BETWEEN 46 AND 55
THEN '46-55'
ELSE
CASE
WHEN EXTRACT (YEAR FROM (select sysdate from dual)) - EXTRACT (YEAR FROM BRTH_DT) BETWEEN 56 AND 65
THEN '56-65'
ELSE
CASE
WHEN EXTRACT (YEAR FROM (select sysdate from dual)) - EXTRACT (YEAR FROM BRTH_DT) > 65
THEN '65andOver'
END) from MSTR_ACCT_A JOIN MSTR_CUST B
where B.CUST_ODT between '2017-11-01' and '2017-11-30' and A.BR_CD like '%00%';
答案 0 :(得分:0)
下面提供的查询应该与您的目标相同。我在语句时简化了你的案例。我不确定嵌套case语句是否是语法错误,但它过于复杂化了。我还为将要创建的列添加了一个名称。
SELECT
CASE
WHEN EXTRACT (YEAR FROM (select sysdate from dual)) - EXTRACT (YEAR FROM BRTH_DT) BETWEEN 18 AND 25
THEN '18-25'
WHEN EXTRACT (YEAR FROM (select sysdate from dual)) - EXTRACT (YEAR FROM BRTH_DT) BETWEEN 26 AND 35
THEN '26-35'
WHEN EXTRACT (YEAR FROM (select sysdate from dual)) - EXTRACT (YEAR FROM BRTH_DT) BETWEEN 36 AND 45
THEN '36-45'
WHEN EXTRACT (YEAR FROM (select sysdate from dual)) - EXTRACT (YEAR FROM BRTH_DT) BETWEEN 46 AND 55
THEN '46-55'
WHEN EXTRACT (YEAR FROM (select sysdate from dual)) - EXTRACT (YEAR FROM BRTH_DT) BETWEEN 56 AND 65
THEN '56-65'
WHEN EXTRACT (YEAR FROM (select sysdate from dual)) - EXTRACT (YEAR FROM BRTH_DT) > 65
THEN '65andOver'
END AS Age_Range
FROM MSTR_ACCT_A JOIN MSTR_CUST B
WHERE B.CUST_ODT BETWEEN '2017-11-01' AND '2017-11-30' AND A.BR_CD LIKE '%00%';