102關務三等 資訊處理 資料庫應用SQL查詢 - 考試

By Todd Johnson
at 2013-06-04T00:16
at 2013-06-04T00:16
Table of Contents
題目如下:
四、(二)Select Count(*) As SKU_Count, Sum(Price) As TotalRev, SKU From
Order_Item Group By SKU Having Sum(Price)=50;
Order_Item
┌──────┬─────┬────┬───┬───────┐
│OrderNumber │ SKU │Quantity│Price │ExtendedPrice │
├──────┼─────┼────┼───┼───────┤
│ 1000 │ 201000 │ 1 │ 300 │ 300 │
├──────┼─────┼────┼───┼───────┤
│ 1000 │ 202000 │ 1 │ 130 │ 130 │
├──────┼─────┼────┼───┼───────┤
│ 2000 │ 101100 │ 4 │ 50 │ 200 │
├──────┼─────┼────┼───┼───────┤
│ 2000 │ 101200 │ 2 │ 50 │ 100 │
└──────┴─────┴────┴───┴───────┘
想法:
Group By SKU,應該會分成4群(筆),但後面的Having Sum(Price)就看不懂了
印象中Having語法是針對Group By後的結果再下條件
但Group By後的4筆,每一筆都有各自的單一Price,
所以不知道Sum(Price)是要跟誰加總(或是執行之後的結果是?)
還請版上高手幫忙,謝謝!
==========================================
謝謝ARCHERDEVIL的提點,我的答案如下圖,請問正確嗎?謝謝!
┌──────┬─────┬────┐
│SKU_Count │ TotalRev │ SKU │
├──────┼─────┼────┤
│ 2 │ 100 │ 101100 │
├──────┼─────┼────┤
│ 2 │ 100 │ 101200 │
└──────┴─────┴────┘
--
四、(二)Select Count(*) As SKU_Count, Sum(Price) As TotalRev, SKU From
Order_Item Group By SKU Having Sum(Price)=50;
Order_Item
┌──────┬─────┬────┬───┬───────┐
│OrderNumber │ SKU │Quantity│Price │ExtendedPrice │
├──────┼─────┼────┼───┼───────┤
│ 1000 │ 201000 │ 1 │ 300 │ 300 │
├──────┼─────┼────┼───┼───────┤
│ 1000 │ 202000 │ 1 │ 130 │ 130 │
├──────┼─────┼────┼───┼───────┤
│ 2000 │ 101100 │ 4 │ 50 │ 200 │
├──────┼─────┼────┼───┼───────┤
│ 2000 │ 101200 │ 2 │ 50 │ 100 │
└──────┴─────┴────┴───┴───────┘
想法:
Group By SKU,應該會分成4群(筆),但後面的Having Sum(Price)就看不懂了
印象中Having語法是針對Group By後的結果再下條件
但Group By後的4筆,每一筆都有各自的單一Price,
所以不知道Sum(Price)是要跟誰加總(或是執行之後的結果是?)
還請版上高手幫忙,謝謝!
==========================================
謝謝ARCHERDEVIL的提點,我的答案如下圖,請問正確嗎?謝謝!
┌──────┬─────┬────┐
│SKU_Count │ TotalRev │ SKU │
├──────┼─────┼────┤
│ 2 │ 100 │ 101100 │
├──────┼─────┼────┤
│ 2 │ 100 │ 101200 │
└──────┴─────┴────┘
--
Tags:
考試
All Comments

By Olive
at 2013-06-07T10:16
at 2013-06-07T10:16

By Frederic
at 2013-06-09T23:02
at 2013-06-09T23:02

By Madame
at 2013-06-14T02:52
at 2013-06-14T02:52
Related Posts
精算逃兵隨便聊聊

By Brianna
at 2013-06-04T00:09
at 2013-06-04T00:09
原住民特考 五等交通行政 考科?

By Ophelia
at 2013-06-03T23:50
at 2013-06-03T23:50
公職人員財產申報細則

By Hedy
at 2013-06-03T23:22
at 2013-06-03T23:22
經濟學 AD 曲線

By Edward Lewis
at 2013-06-03T23:19
at 2013-06-03T23:19
95原民特四

By Anthony
at 2013-06-03T22:59
at 2013-06-03T22:59