2020-08-21 easy 7-11

7题属于delete题型,很少用到,先跳过。

8. Rising Temperature

id is the primary key for this table.

This table contains information about the temperature in a certain day.

Table: Weather

+---------------+---------+

| Column Name  | Type    |

+---------------+---------+

| id            | int    |

| recordDate    | date    |

| temperature  | int    |

+---------------+---------+

Write an SQL query to find all dates' id with higher temperature compared to its previous dates (yesterday). Return the result table in any order.

SELECT b.id

FROM weather AS a

INNER JOIN weather AS b

ON DATEDIFF(b.recorddate, a.recorddate) = 1

WHERE b.temperature > a.temperature

;


这道题不难,但是datediff有几个需要注意的地方,datediff很容易写成date_diff,这是不对的。还有就是datdiff(x,y)= z,是x-y=z,不是y-x=z。还有就是datediff不管是用月份年份还是天数相减,最后得到的都是天数。


9. User Activity for the Past 30 Days II

Table: Activity

+---------------+---------+

| Column Name  | Type    |

+---------------+---------+

| user_id      | int    |

| session_id    | int    |

| activity_date | date    |

| activity_type | enum    |

+---------------+---------+

There is no primary key for this table, it may have duplicate rows. The activity_type column is an ENUM of type ('open_session', 'end_session', 'scroll_down', 'send_message'). The table shows the user activities for a social media website. Note that each session belongs to exactly one user.

The query result format is in the following example:

Activity table:

+---------+------------+---------------+---------------+

| user_id | session_id | activity_date | activity_type |

+---------+------------+---------------+---------------+

| 1      | 1          | 2019-07-20    | open_session  |

| 1      | 1          | 2019-07-20    | scroll_down  |

| 1      | 1          | 2019-07-20    | end_session  |

| 2      | 4          | 2019-07-20    | open_session  |

| 2      | 4          | 2019-07-21    | send_message  |

| 2      | 4          | 2019-07-21    | end_session  |

| 3      | 2          | 2019-07-21    | open_session  |

| 3      | 2          | 2019-07-21    | send_message  |

| 3      | 2          | 2019-07-21    | end_session  |

| 3      | 5          | 2019-07-21    | open_session  |

| 3      | 5          | 2019-07-21    | scroll_down  |

| 3      | 5          | 2019-07-21    | end_session  |

| 4      | 3          | 2019-06-25    | open_session  |

| 4      | 3          | 2019-06-25    | end_session  |

+---------+------------+---------------+---------------+

Write an SQL query to find the average number of sessions per user for a period of 30 days ending 2019-07-27inclusively, rounded to 2 decimal places. The sessions we want to count for a user are those with at least one activity in that time period.

SELECT ROUND(IFNNULL(SUM(a.num)/COUNT(a.user_id), 0), 2) AS average_sessions_per_user

FROM

(SELECT user_id, COUNT(DISTINCT session_id) AS num

FROM activity

WHERE activity_date BETWEEN ADDDATE('2019-07-27, INTERVAL -29 DAY) AND '2019-07-27'

GROUP BY user_id) AS a

;

SELECT ROUND(IFNULL(COUNT(DISTINCT session_id)/COUNT(DISTINCT user_id), 0), 2)

FROM activity

WHERE activity_date BETWEEN ADDDATE('2019-07-27', INTERVAL -29 DAY) AND '2019-07-27'

;

这道题有几个点需要注意:不要一看见per user就直接想用group by, 多想一下说不定有更方便的方法;有除法一定要加ifnull!!!;日期如果是inclusively的话,需要注意两个日期端点之间差了多少天。


10. Consecutive Available Seats

Several friends at a cinema ticket office would like to reserve consecutive available seats.

Can you help to query all the consecutive available seats order by the seat_id using the following cinema table?

| seat_id | free |

|---------|------|

| 1      | 1    |

| 2      | 0    |

| 3      | 1    |

| 4      | 1    |

| 5      | 1    |

Note:

The seat_id is an auto increment int, and free is bool ('1' means free, and '0' means occupied.).

Consecutive available seats are more than 2(inclusive) seats consecutively available.

SELECT DISTINCT a.seat_id

FROM cinema AS a, cinema AS b

WHERE ABS(a.seat_id-b.seat_id) = 1 AND a.free = 1 AND b.free = 1

ORDER BY seat_id

;

注意ABS使用的时候和DATEDIFF不一样,ABS中间是减号;还有就是这道题必须加distinct,注意join了以后再select的时候,有没有重复项,如果一时判断不清楚,就先加上。


11. Average Selling Price

Table: Prices

+---------------+---------+

| Column Name  | Type    |

+---------------+---------+

| product_id    | int    |

| start_date    | date    |

| end_date      | date    |

| price        | int    |

+---------------+---------+

(product_id, start_date, end_date) is the primary key for this table.

Each row of this table indicates the price of the product_id in the period from start_date to end_date.

For each product_id there will be no two overlapping periods. That means there will be no two intersecting periods for the same product_id.


Table: UnitsSold

+---------------+---------+

| Column Name  | Type    |

+---------------+---------+

| product_id    | int    |

| purchase_date | date    |

| units        | int    |

+---------------+---------+

There is no primary key for this table, it may contain duplicates.

Each row of this table indicates the date, units and product_id of each product sold.


Write an SQL query to find the average selling price for each product.

average_price should berounded to 2 decimal places.

The query result format is in the following example:

Prices table:

+------------+------------+------------+--------+

| product_id | start_date | end_date  | price  |

+------------+------------+------------+--------+

| 1          | 2019-02-17 | 2019-02-28 | 5      |

| 1          | 2019-03-01 | 2019-03-22 | 20    |

| 2          | 2019-02-01 | 2019-02-20 | 15    |

| 2          | 2019-02-21 | 2019-03-31 | 30    |

+------------+------------+------------+--------+

UnitsSold table:

+------------+---------------+-------+

| product_id | purchase_date | units |

+------------+---------------+-------+

| 1          | 2019-02-25    | 100  |

| 1          | 2019-03-01    | 15    |

| 2          | 2019-02-10    | 200  |

| 2          | 2019-03-22    | 30    |

+------------+---------------+-------+

SELECT p.product_id, 

            ROUND(IFNULL(SUM(u.units*p.price)/SUM(u.units), 0), 2)

FROM unitssold AS u

INNER JOIN prices AS p

ON u.purchase_date BETWEEN p.start_date AND end_date AND u.product_id = p.product_id

GROUP BY p.product_id

;

这道题在Join的时候在匹配date的时候很容易忘记去匹配product_id,要注意一些。

最后编辑于
©著作权归作者所有,转载或内容合作请联系作者
  • 序言:七十年代末,一起剥皮案震惊了整个滨河市,随后出现的几起案子,更是在滨河造成了极大的恐慌,老刑警刘岩,带你破解...
    沈念sama阅读 194,761评论 5 460
  • 序言:滨河连续发生了三起死亡事件,死亡现场离奇诡异,居然都是意外死亡,警方通过查阅死者的电脑和手机,发现死者居然都...
    沈念sama阅读 81,953评论 2 371
  • 文/潘晓璐 我一进店门,熙熙楼的掌柜王于贵愁眉苦脸地迎上来,“玉大人,你说我怎么就摊上这事。” “怎么了?”我有些...
    开封第一讲书人阅读 141,998评论 0 320
  • 文/不坏的土叔 我叫张陵,是天一观的道长。 经常有香客问我,道长,这世上最难降的妖魔是什么? 我笑而不...
    开封第一讲书人阅读 52,248评论 1 263
  • 正文 为了忘掉前任,我火速办了婚礼,结果婚礼上,老公的妹妹穿的比我还像新娘。我一直安慰自己,他们只是感情好,可当我...
    茶点故事阅读 61,130评论 4 356
  • 文/花漫 我一把揭开白布。 她就那样静静地躺着,像睡着了一般。 火红的嫁衣衬着肌肤如雪。 梳的纹丝不乱的头发上,一...
    开封第一讲书人阅读 46,145评论 1 272
  • 那天,我揣着相机与录音,去河边找鬼。 笑死,一个胖子当着我的面吹牛,可吹牛的内容都是我干的。 我是一名探鬼主播,决...
    沈念sama阅读 36,550评论 3 381
  • 文/苍兰香墨 我猛地睁开眼,长吁一口气:“原来是场噩梦啊……” “哼!你这毒妇竟也来了?” 一声冷哼从身侧响起,我...
    开封第一讲书人阅读 35,236评论 0 253
  • 序言:老挝万荣一对情侣失踪,失踪者是张志新(化名)和其女友刘颖,没想到半个月后,有当地人在树林里发现了一具尸体,经...
    沈念sama阅读 39,510评论 1 291
  • 正文 独居荒郊野岭守林人离奇死亡,尸身上长有42处带血的脓包…… 初始之章·张勋 以下内容为张勋视角 年9月15日...
    茶点故事阅读 34,601评论 2 310
  • 正文 我和宋清朗相恋三年,在试婚纱的时候发现自己被绿了。 大学时的朋友给我发了我未婚夫和他白月光在一起吃饭的照片。...
    茶点故事阅读 36,376评论 1 326
  • 序言:一个原本活蹦乱跳的男人离奇死亡,死状恐怖,灵堂内的尸体忽然破棺而出,到底是诈尸还是另有隐情,我是刑警宁泽,带...
    沈念sama阅读 32,247评论 3 313
  • 正文 年R本政府宣布,位于F岛的核电站,受9级特大地震影响,放射性物质发生泄漏。R本人自食恶果不足惜,却给世界环境...
    茶点故事阅读 37,613评论 3 299
  • 文/蒙蒙 一、第九天 我趴在偏房一处隐蔽的房顶上张望。 院中可真热闹,春花似锦、人声如沸。这庄子的主人今日做“春日...
    开封第一讲书人阅读 28,911评论 0 17
  • 文/苍兰香墨 我抬头看了看天上的太阳。三九已至,却和暖如春,着一层夹袄步出监牢的瞬间,已是汗流浃背。 一阵脚步声响...
    开封第一讲书人阅读 30,191评论 1 250
  • 我被黑心中介骗来泰国打工, 没想到刚下飞机就差点儿被人妖公主榨干…… 1. 我叫王不留,地道东北人。 一个月前我还...
    沈念sama阅读 41,532评论 2 342
  • 正文 我出身青楼,却偏偏与公主长得像,于是被迫代替她去往敌国和亲。 传闻我的和亲对象是个残疾皇子,可洞房花烛夜当晚...
    茶点故事阅读 40,739评论 2 335