SQL練習題_用戶購買收藏記錄合併(拼多多)

拼多多筆試題0805_統計用戶數據

筆試題描述

本題來自2018年8月5日拼多多筆試題sql

  • 給定兩張表buy和fork分別記錄用戶的購買記錄、收藏記錄
  • 返回狀態「已收藏已購買」「已收藏未購買」「未收藏已購買」,以(0,1)表示

表格構建

create table buy(user_id int,item_id int,buy_time DATE);
create table fork(user_id int,item_id int ,fork_time DATE);
insert into buy values(0001,201,'2008-09-04');
insert into buy values(0001,206,'2008-09-04');
insert into buy values(0002,203,'2008-09-04');
insert into buy values(0003,204,'2008-09-04');

insert into fork values(0001,203,'2008-09-04');
insert into fork values(0001,201,'2008-09-04');
insert into fork values(0001,205,'2008-09-04');
insert into fork values(0004,203,'2008-09-04');
insert into fork values(0003,204,'2008-09-04');
insert into fork values(0002,201,'2008-09-04');

表格結果以下:

TABLE buy
3d

TABLE fork
code

數據觀察

  • 表格中能夠發現以下問題

有些商品已購買,未收藏
有些商品未購買,已收藏htm

  • 最後輸出中須要彙總全部用戶&商品

題目分析

1、合併表格

  • 當保證buy表全部數據時,應使用LEFT JOIN
  • 如有數據有購買記錄,無收藏記錄,表格中則會顯示NULL
SELECT *
FROM buy LEFT JOIN fork
ON buy.user_id=fork.user_id AND buy.item_id=fork.item_id;

表格結果以下:


2、CASE表示(0,1)

  • 根據是否爲NULL值,進行邏輯判斷
CASE WHEN fork.fork_time is not null and buy.buy_time is not null THEN 1 ELSE 0 END AS 'fork&buy',
CASE WHEN fork.fork_time is not null and buy.buy_time is null THEN 1 ELSE 0 END AS 'fork&NOT buy',
CASE WHEN fork.fork_time is null and buy.buy_time is not null THEN 1 ELSE 0 END AS 'NOT fork&buy',
CASE WHEN fork.fork_time is null and buy.buy_time is null THEN 1 ELSE 0 END AS 'NOT fork& NOT buy'
  • 固然以buy爲主表,是不可能出現【未收藏已購買】【未收藏未購買】的狀況的。

最後結果及代碼

SELECT buy.user_id,buy.item_id,
    CASE WHEN fork.fork_time is not null and buy.buy_time is not null THEN 1 ELSE 0 END AS 'fork&buy',
    CASE WHEN fork.fork_time is not null and buy.buy_time is null THEN 1 ELSE 0 END AS 'fork&NOT buy',
    CASE WHEN fork.fork_time is null and buy.buy_time is not null THEN 1 ELSE 0 END AS 'NOT fork&buy',
    CASE WHEN fork.fork_time is null and buy.buy_time is null THEN 1 ELSE 0 END AS 'NOT fork& NOT buy'
FROM buy LEFT JOIN fork
ON buy.user_id=fork.user_id and buy.item_id=fork.item_id


3、同理複製FORK表

SELECT fork.user_id,fork.item_id,
    CASE WHEN fork.fork_time is not null and buy.buy_time is not null THEN 1 ELSE 0 END AS 'fork&buy',
    CASE WHEN fork.fork_time is not null and buy.buy_time is null THEN 1 ELSE 0 END AS 'fork&NOT buy',
    CASE WHEN fork.fork_time is null and buy.buy_time is not null THEN 1 ELSE 0 END AS 'NOT fork&buy',
    CASE WHEN fork.fork_time is null and buy.buy_time is null THEN 1 ELSE 0 END AS 'NOT fork& NOT buy'
FROM fork LEFT JOIN buy
ON buy.user_id=fork.user_id and buy.item_id=fork.item_id


題目解答

  • 兩個結果合併後,便可獲得最終結果
  • UNION - 去除重複行合併
SELECT buy.user_id,buy.item_id,
    CASE WHEN fork.fork_time is not null and buy.buy_time is not null THEN 1 ELSE 0 END AS 'fork&buy',
    CASE WHEN fork.fork_time is not null and buy.buy_time is null THEN 1 ELSE 0 END AS 'fork&NOT buy',
    CASE WHEN fork.fork_time is null and buy.buy_time is not null THEN 1 ELSE 0 END AS 'NOT fork&buy',
    CASE WHEN fork.fork_time is null and buy.buy_time is null THEN 1 ELSE 0 END AS 'NOT fork& NOT buy'
FROM buy LEFT JOIN fork
ON buy.user_id=fork.user_id and buy.item_id=fork.item_id
UNION
SELECT fork.user_id,fork.item_id,
    CASE WHEN fork.fork_time is not null and buy.buy_time is not null THEN 1 ELSE 0 END AS 'fork&buy',
    CASE WHEN fork.fork_time is not null and buy.buy_time is null THEN 1 ELSE 0 END AS 'fork&NOT buy',
    CASE WHEN fork.fork_time is null and buy.buy_time is not null THEN 1 ELSE 0 END AS 'NOT fork&buy',
    CASE WHEN fork.fork_time is null and buy.buy_time is null THEN 1 ELSE 0 END AS 'NOT fork& NOT buy'
FROM fork LEFT JOIN buy
ON buy.user_id=fork.user_id and buy.item_id=fork.item_id
ORDER BY user_id,item_id;

【轉載請註明】http://www.javashuo.com/article/p-tbhzqiac-ey.htmlblog

相關文章
相關標籤/搜索