使用生日统计年龄组

ryevplcw  于 2021-06-01  发布在  Hadoop
关注(0)|答案(1)|浏览(343)

我希望能够得到计数对每个年龄组以下使用客户的生日。客户还必须满足某些条件。我需要用两张table。

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%';
pgccezyw

pgccezyw1#

下面提供的查询应该与您的查询的目的相同。我简化了你的陈述。我不确定嵌套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%';

相关问题