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不管是用月份年份還是天數(shù)相減叫胖,最后得到的都是天數(shù)。


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她奥, 多想一下說不定有更方便的方法臭家;有除法一定要加ifnullFI隆!6ち蕖蹄殃;日期如果是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的時候吩谦,有沒有重復(fù)項,如果一時判斷不清楚膝藕,就先加上式廷。


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,要注意一些芭挽。

最后編輯于
?著作權(quán)歸作者所有,轉(zhuǎn)載或內(nèi)容合作請聯(lián)系作者
  • 序言:七十年代末滑废,一起剝皮案震驚了整個濱河市,隨后出現(xiàn)的幾起案子袜爪,更是在濱河造成了極大的恐慌蠕趁,老刑警劉巖,帶你破解...
    沈念sama閱讀 211,561評論 6 492
  • 序言:濱河連續(xù)發(fā)生了三起死亡事件辛馆,死亡現(xiàn)場離奇詭異俺陋,居然都是意外死亡,警方通過查閱死者的電腦和手機昙篙,發(fā)現(xiàn)死者居然都...
    沈念sama閱讀 90,218評論 3 385
  • 文/潘曉璐 我一進店門腊状,熙熙樓的掌柜王于貴愁眉苦臉地迎上來,“玉大人苔可,你說我怎么就攤上這事缴挖。” “怎么了硕蛹?”我有些...
    開封第一講書人閱讀 157,162評論 0 348
  • 文/不壞的土叔 我叫張陵醇疼,是天一觀的道長硕并。 經(jīng)常有香客問我法焰,道長,這世上最難降的妖魔是什么倔毙? 我笑而不...
    開封第一講書人閱讀 56,470評論 1 283
  • 正文 為了忘掉前任埃仪,我火速辦了婚禮,結(jié)果婚禮上陕赃,老公的妹妹穿的比我還像新娘卵蛉。我一直安慰自己颁股,他們只是感情好,可當我...
    茶點故事閱讀 65,550評論 6 385
  • 文/花漫 我一把揭開白布傻丝。 她就那樣靜靜地躺著甘有,像睡著了一般。 火紅的嫁衣襯著肌膚如雪葡缰。 梳的紋絲不亂的頭發(fā)上亏掀,一...
    開封第一講書人閱讀 49,806評論 1 290
  • 那天,我揣著相機與錄音泛释,去河邊找鬼滤愕。 笑死,一個胖子當著我的面吹牛怜校,可吹牛的內(nèi)容都是我干的间影。 我是一名探鬼主播,決...
    沈念sama閱讀 38,951評論 3 407
  • 文/蒼蘭香墨 我猛地睜開眼茄茁,長吁一口氣:“原來是場噩夢啊……” “哼魂贬!你這毒婦竟也來了?” 一聲冷哼從身側(cè)響起胰丁,我...
    開封第一講書人閱讀 37,712評論 0 266
  • 序言:老撾萬榮一對情侶失蹤随橘,失蹤者是張志新(化名)和其女友劉穎,沒想到半個月后锦庸,有當?shù)厝嗽跇淞掷锇l(fā)現(xiàn)了一具尸體机蔗,經(jīng)...
    沈念sama閱讀 44,166評論 1 303
  • 正文 獨居荒郊野嶺守林人離奇死亡,尸身上長有42處帶血的膿包…… 初始之章·張勛 以下內(nèi)容為張勛視角 年9月15日...
    茶點故事閱讀 36,510評論 2 327
  • 正文 我和宋清朗相戀三年甘萧,在試婚紗的時候發(fā)現(xiàn)自己被綠了萝嘁。 大學時的朋友給我發(fā)了我未婚夫和他白月光在一起吃飯的照片。...
    茶點故事閱讀 38,643評論 1 340
  • 序言:一個原本活蹦亂跳的男人離奇死亡扬卷,死狀恐怖牙言,靈堂內(nèi)的尸體忽然破棺而出,到底是詐尸還是另有隱情怪得,我是刑警寧澤咱枉,帶...
    沈念sama閱讀 34,306評論 4 330
  • 正文 年R本政府宣布,位于F島的核電站徒恋,受9級特大地震影響蚕断,放射性物質(zhì)發(fā)生泄漏。R本人自食惡果不足惜入挣,卻給世界環(huán)境...
    茶點故事閱讀 39,930評論 3 313
  • 文/蒙蒙 一亿乳、第九天 我趴在偏房一處隱蔽的房頂上張望。 院中可真熱鬧,春花似錦葛假、人聲如沸障陶。這莊子的主人今日做“春日...
    開封第一講書人閱讀 30,745評論 0 21
  • 文/蒼蘭香墨 我抬頭看了看天上的太陽抱究。三九已至,卻和暖如春带斑,著一層夾襖步出監(jiān)牢的瞬間媳维,已是汗流浹背。 一陣腳步聲響...
    開封第一講書人閱讀 31,983評論 1 266
  • 我被黑心中介騙來泰國打工遏暴, 沒想到剛下飛機就差點兒被人妖公主榨干…… 1. 我叫王不留侄刽,地道東北人。 一個月前我還...
    沈念sama閱讀 46,351評論 2 360
  • 正文 我出身青樓朋凉,卻偏偏與公主長得像州丹,于是被迫代替她去往敵國和親。 傳聞我的和親對象是個殘疾皇子杂彭,可洞房花燭夜當晚...
    茶點故事閱讀 43,509評論 2 348