iT邦幫忙

2

SQL要怎麼下(合併)

sql
XYZ 2019-12-01 16:08:411889 瀏覽

請問下圖一的資料要如何變成圖二?
其中
圖一的typeall欄位中內有關鍵字book在圖二的結果就是book的加總筆數數量,
圖一的typeall欄位中內有關鍵字pen在圖二的結果就是pen的加總筆數數量,
圖一的typeall欄位中內有關鍵字ball在圖二的結果就是ball的加總筆數數量
https://ithelp.ithome.com.tw/upload/images/20191201/20108157AvkCdexjzE.jpg


https://ithelp.ithome.com.tw/upload/images/20191201/20108157M16WTsueKP.jpg

slime iT邦大師 1 級 ‧ 2019-12-01 17:05:08 檢舉
建議先考慮一下 三階正規化 啦....

1. 原則上一張訂單只會歸屬於一間供應商或客戶, 訂單編號 a01 一定是給 公司100 , 那拆成
訂單頭: 訂單編號(a01, b01), 日期, 公司編號(100, 200).
訂單明細: 訂單編號, 訂單內序號, 商品編號, 商品數量.
公司基本資料表: 公司編號, 公司名稱
商品基本資料表: 商品編號, 商品名稱, 商品類別編號
商品類別編號表: 商品類別編號, 商品類別說明

2. 這樣搜尋時就是用 商品類別 處理, 結案.
小魚 iT邦大師 1 級 ‧ 2019-12-02 07:22:49 檢舉
他是想讓我們練習一下SQL啦
圖片
  直播研討會
圖片
{{ item.channelVendor }} {{ item.webinarstarted }} |
{{ formatDate(item.duration) }}
直播中
12
暐翰
iT邦大師 1 級 ‧ 2019-12-01 16:51:47
最佳解答

你可以使用count + windows function + case when like判斷
以下邏輯 : 以orderno,com為分組,當組內資料typeall包含特定字串,count數+1

select orderno,com
    ,count(case when typeall like '%book%' then 1 end) book
    ,count(case when typeall like '%pen%' then 1 end) pen
    ,count(case when typeall like '%ball%' then 1 end) ball
from 你的表格
group by  orderno,com
XYZ iT邦新手 4 級 ‧ 2019-12-01 20:21:28 檢舉

謝謝你^^,解決我的問題,原來要用 then 1

1
阿展展展
iT邦好手 1 級 ‧ 2019-12-01 16:20:46
group by
sum

.....
你有嘗試自己寫看看嗎?

XYZ iT邦新手 4 級 ‧ 2019-12-01 16:29:47 檢舉

它不數值不能用sum

阿0.0 用count ....

1
小魚
iT邦大師 1 級 ‧ 2019-12-01 16:47:56

請先把你目前寫的語法貼出來,
再說明一下遇到的困難在哪裡.

我要發表回答

立即登入回答