1127. User Purchase Platform

Table: Spending

+-------------+---------+
| Column Name | Type |
+-------------+---------+
| user_id | int |
| spend_date | date |
| platform | enum |
| amount | int |
+-------------+---------+
The table logs the spendings history of users that make purchases from an online shopping website which has a desktop and a mobile application.
(user_id, spend_date, platform) is the primary key of this table.
The platform column is an ENUM type of ('desktop', 'mobile').
Write an SQL query to find the total number of users and the total amount spent using mobile only, desktop only and both mobile and desktop together for each date.

The query result format is in the following example:

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 |
+------------+----------+--------------+-------------+
On 2019-07-01, user 1 purchased using both desktop and mobile, user 2 purchased using mobile only and user 3 purchased using desktop only.
On 2019-07-02, user 2 purchased using mobile only, user 3 purchased using desktop only and no one purchased using both platforms.

Source: stay button (LeetCode)
link: https: //leetcode-cn.com/problems/user-purchase-platform
copyrighted by deduction from all networks. Commercial reprint please contact the authorized official, non-commercial reprint please indicate the source.

 

 

I began to think for a long time, to prepare respectively check only desktop, only mobile, and both found check only desktop of this is too complicated,
later changed the idea, first according to spend_date, user_id the only desktop, only mobile, and and both of all check out,
then put the results calculated in accordance with the amount and the number of packets spend_date

SELECT spend_date, Platform, SUM (AMOUNT) AS TOTAL_AMOUNT, COUNT (user_id) total_users
from
(SELECT spend_date, user_id,
(Case COUNT (DISTINCT Platform)
When. 1 the then Platform
When 2 the then 'both'
End
) AS Platform, SUM (AMOUNT) AMOUNT AS
from Spending
Group by spend_date, user_id
) AS temp2
Group by spend_date, Platform
but this was out only certain spend_date some user_id
, the subject of the request is not present the results of which should be placed, but the result set to zero it. I found on the Internet
such an approach

DISTINCT SELECT (spend_date), p.platform
from Spending,
(SELECT 'Desktop' Platform Union AS
SELECT 'Mobile' Platform Union AS
SELECT 'both' Platform AS
) AS P
so enumerated put out the result of the first two columns of the check .
Then merge the results of these two steps on it.
Complete sql as follows:

select temp1.spend_date, temp1.platform,
ifnull(temp3.total_amount, 0) total_amount,
ifnull(temp3.total_users,0) total_users
from
(select distinct(spend_date), p.platform
from Spending,
(select 'desktop' as platform union
select 'mobile' as platform union
select 'both' as platform
) as p
) as temp1
left join
(select spend_date,platform, sum(amount) as total_amount, count(user_id) total_users
from
(select spend_date, user_id,
(case count(distinct platform)
when 1 then platform
when 2 then 'both'
end
) as platform, sum(amount) as amount
from Spending
group by spend_date, user_id
) as temp2
group by spend_date, platform
) as temp3
on temp1.platform = temp3.platform and temp1.spend_date = temp3.spend_date

Author: couchpotato613
link: https: //leetcode-cn.com/problems/user-purchase-platform/solution/zhe-ti-hao-nan-by-couchpotato613/
Source: stay button (LeetCode)
copyright reserved by the authors. Commercial reprint please contact the author authorized, non-commercial reprint please indicate the source.

Guess you like

Origin www.cnblogs.com/leeeee/p/11902030.html