如何在sql中将类似的情况放在一行中,将不同的情况放在不同的行中?

问题描述 投票:0回答:0

我有两张学生表,表1是个人信息,表2是他在学校借书次数的附加信息。

表一:

* doc_part1: id part 1
* doc_part2: id part 2
* doc_part2: id part 3
doc_all: join id1+id2+id3
profile: student profile
++ date_update_login: date when student updated login
date: date when the student entered the school
doc_part1.doc_part2;doc_parte2;doc_all;perfil;date_update_login;date
987654321;5672;12;987654321567212;profile1;2020-01-01;2020-01-01 (Case 1)
987654321;5672;12;987654321567212;profile1;2020-06-28;2020-01-01 (Case 2)
987654321;5672;12;987654321567212;profile2;2020-12-07;2020-01-01 (Case 2)
987654321;5672;12;987654321567212;profile1;2021-04-22;2020-01-01 (Case 1)
987654321;5672;12;987654321567212;profile1;2021-11-04;2020-01-01 (Case 1)

表二:

* doc_part1: id part 1
* doc_part2: id part 2
* doc_part2: id part 3
doc_all: join id1+id2+id3
++ borrow_book: date when the student borrowed a school book
doc_part1;doc_part2;doc_parte2;doc_all;borrow_book
987654321;5672;12;987654321567212;2020-06-28; (Case 2)
987654321;5672;12;987654321567212;2020-12-07; (Case 2)
987654321;5672;12;987654321567212;2022-01-05; (Case 3)

输出:

doc_part1.doc_part2;doc_parte2;doc_all;perfil;date_update_login;borrow_book;date
987654321;5672;12;987654321567212;profile1;2020-01-01;null;2020-01-01 (Case 1)
987654321;5672;12;987654321567212;profile1;2020-06-28;2020-06-28;2020-01-01 (Case 2)
987654321;5672;12;987654321567212;profile2;2020-12-07;2020-12-07;2020-01-01 (Case 2)
987654321;5672;12;987654321567212;null;null;2020-12-07;2020-01-01 (Case 3)

有了这两张表,我需要把3个case带进决赛桌:

Case 1: Cases where the student just updated the login without borrowing the school book
Case 2: Cases where the student updated the login and borrowed the school book (on the same date)
Case 3: Cases where the student borrowed the school book and did not update the login

表主键标有:标有

*
可以用来交叉的日期:标有
++

如何在 SQL 中执行此操作?

在上面的示例中,我想帮助使用提到的 2 个表获得 3 个输出。我已经求助于 GPT 聊天,但我仍然遇到困难。

sql hive logic etl
© www.soinside.com 2019 - 2024. All rights reserved.