繁体   English   中英

Group By 组合到 CASE (SQL DB2)

[英]Group By combined to CASE (SQL DB2)

我有一个包含此列的客户数据库(示例):

ID 时间戳 TYP_EVEN 姓名 国家 性别 NBR_儿童 地址 CUSTOMER_LINKED TYP_OF_LINK
044348547 2020-09-08-02.02.21.442908 0004 NXXX 科琳娜 法兰克福机场 2 02 000000000
044379039 2020-07-17-11.17.55.410843 0013 00 11 街 XXXX XXX 000000000
044379039 2020-07-21-16.45.53.485200 0004 KXXX 斯蒂芬妮 法兰克福机场 1 00 000000000
044379039 2020-08-05-02.02.41.403053 0004 KXXX 斯蒂芬妮 法兰克福机场 1 00 000000000
044385911 2019-01-31-02.03.22.455660 0004 SXXX 菲利普 法兰克福机场 1 02 000000000
044385911 2020-09-18-15.46.31.387588 0004 SXXX 菲利普 法兰克福机场 1 02 000000000
044385911 2020-09-30-02.01.45.310449 0004 SXXX 菲利普 法兰克福机场 1 02 000000000
044385911 2020-10-01-00.06.20.341427 0013 00 19 街 XXXX XXXX 000000000
044680501 2021-03-10-02.02.42.979297 0004 HXXX 斯蒂芬妮 法兰克福机场 1 00 000000000
044680501 2021-03-10-02.02.42.980069 0013 00 22 街 XXXX XXXX 000000000
044680501 2021-03-10-02.02.42.981083 0029 00 044680519 已婚
044680519 2021-03-10-02.02.42.966522 0004 HXXX 酒水 法兰克福机场 2 02 000000000
044680519 2021-03-10-02.02.42.970031 0029 00 044680501 已婚

TYP_EVEN : 是关于每个客户 ID 的记录的事件类型,然后OOO4 : 是关于客户信息 (NAME, COUNTRY, GENDER, Number of children), 0013 : 是关于 ADRESS 信息和0029 : 是关于链接 beetwen 客户

我想按 ID 列出所有信息。 我这样进行:

    SELECT
T1.ID,
CASE WHEN T1.TYP_EVEN = '0004' THEN T1.NAME END AS NAME,
CASE WHEN T1.TYP_EVEN = '0004' THEN T1.COUNTRY END AS COUNTRY,
CASE WHEN T1.TYP_EVEN = '0004' THEN T1.GENDER END AS GENDER,
CASE WHEN T1.TYP_EVEN = '0004' THEN T1.NBR_CHILDREN END AS NBR_CHILDREN,

CASE WHEN T1.TYP_EVEN = '0013' THEN T1.ADRESS END AS ADRESS,

CASE WHEN T1.TYP_EVEN = '0029' THEN T1.CUSTOMER_LINKED END AS CUSTOMER_LINKED,
CASE WHEN T1.TYP_EVEN = '0029' THEN T1.TYP_OF_LINK END AS TYP_OF_LINK
,T1.TIMESTAMP

FROM DB.CUSTOMERS T1

GROUP BY GROUP BY T1.ID,T1.TIMESTAMP, T1.TYP_EVEN,T1.NAME,T1.COUNTRY,T1.GENDER,T1.NBR_CHILDREN,T1.ADRESS
,T1.CUSTOMER_LINKED, T1.TYP_OF_LINK 
ORDER BY 1

结果应该是:

ID 姓名 国家 性别 NBR_儿童 地址 CUSTOMER_LINKED TYP_OF_LINK
044348547 NXXX 科琳娜 法兰克福机场 2 02 11 街 XXXX XXX 000000000
044379039 KXXX 斯蒂芬妮 法兰克福机场 1 00 000000000
044385911 SXXX 菲利普 法兰克福机场 1 02 19 街 XXXX XXXX 000000000
044680501 HXXX 斯蒂芬妮 法兰克福机场 1 00 22 街 XXXX XXXX 044680519 已婚
044680519 HXXX 酒水 法兰克福机场 2 02 044680501 已婚

更多信息:如果一个 ID 有 2 行具有相同的 TYP_EVEN,则通过 TIMESTAMP 选择后者。

有人可以解决这个 SQL 声明吗?

这是一种做你想做的事的方法

with customers (id, timestamp, typ_even, name, country, gender, nbr_children, adress, customer_linked, typ_of_link) as (
  values
  (044348547, timestamp('2020-09-08-02.02.21.442908'), 0004, 'NXXX CORINNE', 'FRA', 2, 02, NULL, 000000000, NULL),
  (044379039, '2020-07-17-11.17.55.410843', 0013, NULL, NULL, NULL, 00, '11 RUE XXXX XXX', 000000000, NULL),
  (044379039, '2020-07-21-16.45.53.485200', 0004, 'KXXX STEPHANE', 'FRA', 1, 00, NULL, 000000000, NULL),
  (044379039, '2020-08-05-02.02.41.403053', 0004, 'KXXX STEPHANE', 'FRA', 1, 00, NULL, 000000000, NULL),
  (044385911, '2019-01-31-02.03.22.455660', 0004, 'SXXX PHILIPPE', 'FRA', 1, 02, NULL, 000000000, NULL),
  (044385911, '2020-09-18-15.46.31.387588', 0004, 'SXXX PHILIPPE', 'FRA', 1, 02, NULL, 000000000, NULL),
  (044385911, '2020-09-30-02.01.45.310449', 0004, 'SXXX PHILIPPE', 'FRA', 1, 02, NULL, 000000000, NULL),
  (044385911, '2020-10-01-00.06.20.341427', 0013, NULL, NULL, NULL, 00, '19 RUE XXXX XXXX', 000000000, NULL),
  (044680501, '2021-03-10-02.02.42.979297', 0004, 'HXXX STEPHANE', 'FRA', 1, 00, NULL, 000000000, NULL),
  (044680501, '2021-03-10-02.02.42.980069', 0013, NULL, NULL, NULL, 00, '22 RUE XXXX XXXX', 000000000, NULL),
  (044680501, '2021-03-10-02.02.42.981083', 0029, NULL, NULL, NULL, 00, NULL, 044680519, 'MARIED'),
  (044680519, '2021-03-10-02.02.42.966522', 0004, 'HXXX LIDWINE', 'FRA', 2, 02, NULL, 000000000, NULL),
  (044680519, '2021-03-10-02.02.42.970031', 0029, NULL, NULL, NULL, 00, NULL, 044680501, 'MARIED')
)
select ID,NAME,COUNTRY,GENDER,NBR_CHILDREN,ADRESS,CUSTOMER_LINKED,TYP_OF_LINK from (
  select
    id,
    last_value(name, 'IGNORE NULLS') over(partition by id order by timestamp) name,
    last_value(country, 'IGNORE NULLS') over(partition by id order by timestamp) country,
    last_value(gender, 'IGNORE NULLS') over(partition by id order by timestamp) gender,
    last_value(nbr_children, 'IGNORE NULLS') over(partition by id order by timestamp) nbr_children,
    last_value(adress, 'IGNORE NULLS') over(partition by id order by timestamp ) adress,
    last_value(customer_linked, 'IGNORE NULLS') over(partition by id order by timestamp ) customer_linked,
    last_value(typ_of_link, 'IGNORE NULLS') over(partition by id order by timestamp ) typ_of_link,
    rownumber() over(partition by id order by timestamp desc) seq
  from customers
) where seq = 1
order by id

db<>fiddle ,Corinne NXXX 没有地址

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM