日韩性视频-久久久蜜桃-www中文字幕-在线中文字幕av-亚洲欧美一区二区三区四区-撸久久-香蕉视频一区-久久无码精品丰满人妻-国产高潮av-激情福利社-日韩av网址大全-国产精品久久999-日本五十路在线-性欧美在线-久久99精品波多结衣一区-男女午夜免费视频-黑人极品ⅴideos精品欧美棵-人人妻人人澡人人爽精品欧美一区-日韩一区在线看-欧美a级在线免费观看

歡迎訪問 生活随笔!

生活随笔

當前位置: 首頁 > 运维知识 > 数据库 >内容正文

数据库

LeetCode MySQL 1127. 用户购买平台 *

發(fā)布時間:2024/7/5 数据库 24 豆豆
生活随笔 收集整理的這篇文章主要介紹了 LeetCode MySQL 1127. 用户购买平台 * 小編覺得挺不錯的,現(xiàn)在分享給大家,幫大家做個參考.

文章目錄

    • 1. 題目
    • 2. 解題

1. 題目

支出表: Spending

+-------------+---------+ | Column Name | Type | +-------------+---------+ | user_id | int | | spend_date | date | | platform | enum | | amount | int | +-------------+---------+ 這張表記錄了用戶在一個在線購物網(wǎng)站的支出歷史, 該在線購物平臺同時擁有桌面端('desktop')和手機端('mobile')的應用程序。 這張表的主鍵是 (user_id, spend_date, platform)。 平臺列 platform 是一種 ENUM ,類型為('desktop', 'mobile')。

寫一段 SQL 來查找每天 使用手機端用戶、 使用桌面端用戶、 同時 使用桌面端和手機端的用戶人數(shù)和總支出金額。

查詢結(jié)果格式如下例所示:

Spending table: +---------+------------+----------+--------+ | user_id | spend_date | platform | amount | +---------+------------+----------+--------+ | 1 | 2019-07-01 | mobile | 100 | | 1 | 2019-07-01 | desktop | 100 | | 2 | 2019-07-01 | mobile | 100 | | 2 | 2019-07-02 | mobile | 100 | | 3 | 2019-07-01 | desktop | 100 | | 3 | 2019-07-02 | desktop | 100 | +---------+------------+----------+--------+Result table: +------------+----------+--------------+-------------+ | spend_date | platform | total_amount | total_users | +------------+----------+--------------+-------------+ | 2019-07-01 | desktop | 100 | 1 | | 2019-07-01 | mobile | 100 | 1 | | 2019-07-01 | both | 200 | 1 | | 2019-07-02 | desktop | 100 | 1 | | 2019-07-02 | mobile | 100 | 1 | | 2019-07-02 | both | 0 | 0 | +------------+----------+--------------+-------------+ 2019-07-01, 用戶1 同時 使用桌面端和手機端購買, 用戶2 僅 使用了手機端購買, 而用戶3 僅 使用了桌面端購買。在 2019-07-02, 用戶2 僅 使用了手機端購買, 用戶3 僅 使用了桌面端購買, 且沒有用戶 同時 使用桌面端和手機端購買。

來源:力扣(LeetCode)
鏈接:https://leetcode-cn.com/problems/user-purchase-platform
著作權(quán)歸領(lǐng)扣網(wǎng)絡所有。商業(yè)轉(zhuǎn)載請聯(lián)系官方授權(quán),非商業(yè)轉(zhuǎn)載請注明出處。

2. 解題

  • 先造出表的各種組合
select distinct spend_date, "desktop" platform from Spending union select distinct spend_date, "mobile" platform from Spending union select distinct spend_date, "both" platform from Spending {"headers": ["spend_date", "platform"], "values": [ ["2019-07-01", "desktop"], ["2019-07-02", "desktop"], ["2019-07-01", "mobile"], ["2019-07-02", "mobile"], ["2019-07-01", "both"], ["2019-07-02", "both"]]}
  • 計算每天,某類屬下的總金額、人數(shù)
select spend_date, if(count(distinct platform)=1, platform, 'both') plat,sum(amount) total_am,count(distinct user_id) total_u # 1 total_u 這么寫也對,就1個人 from Spending group by spend_date, user_id {"headers": ["spend_date", "plat", "total_am", "total_u"], "values": [ ["2019-07-01", "both", 200, 1], ["2019-07-01", "mobile", 100, 1], ["2019-07-01", "desktop", 100, 1], ["2019-07-02", "mobile", 100, 1], ["2019-07-02", "desktop", 100, 1]]}
  • 上面2表連接
select p.spend_date, p.platform, t.total_am, t.total_u from (select distinct spend_date, "desktop" platform from Spendingunionselect distinct spend_date, "mobile" platform from Spendingunionselect distinct spend_date, "both" platform from Spending ) p left join (select spend_date, if(count(distinct platform)=1, platform, 'both') plat,sum(amount) total_am,count(distinct user_id) total_ufrom Spendinggroup by spend_date, user_id ) t on p.platform = t.plat and p.spend_date = t.spend_date {"headers": ["spend_date", "platform", "total_am", "total_u"],"values": [ ["2019-07-01", "desktop", 100, 1], ["2019-07-02", "desktop", 100, 1], ["2019-07-01", "mobile", 100, 1], ["2019-07-02", "mobile", 100, 1], ["2019-07-01", "both", 200, 1], ["2019-07-02", "both", null, null]]}
  • 對連接后的表,求和
# Write your MySQL query statement below selectspend_date, platform,ifnull(sum(total_am),0) total_amount,ifnull(sum(total_u),0) total_users from (select p.spend_date, p.platform, t.total_am, t.total_ufrom(select distinct spend_date, "desktop" platform from Spendingunionselect distinct spend_date, "mobile" platform from Spendingunionselect distinct spend_date, "both" platform from Spending) pleft join(select spend_date, if(count(distinct platform)=1, platform, 'both') plat,sum(amount) total_am,count(distinct user_id) total_ufrom Spendinggroup by spend_date, user_id) ton p.platform = t.plat and p.spend_date = t.spend_date ) temp group by spend_date, platform

我的CSDN博客地址 https://michael.blog.csdn.net/

長按或掃碼關(guān)注我的公眾號(Michael阿明),一起加油、一起學習進步!

總結(jié)

以上是生活随笔為你收集整理的LeetCode MySQL 1127. 用户购买平台 *的全部內(nèi)容,希望文章能夠幫你解決所遇到的問題。

如果覺得生活随笔網(wǎng)站內(nèi)容還不錯,歡迎將生活随笔推薦給好友。