作者licheer (Keep The Faith)
看板Database
標題[SQL ] 每個Field都要做SubQuery
時間Fri Nov 9 01:53:28 2018
資料庫名稱: MS-SQL
資料庫版本: VBA
內容/問題描述: 想要讀右邊的樣子
Name Location Number Name 001 002 003
A 001 50 A 50 10 20
A 002 10 B 30 10 0
A 003 20 ==>
B 001 30
B 002 10
Select
Name,
Number=(Select i.Number From Table as i where i.Locate=001 and i.Name=j.Name),
Number=(Select i.Number From Table as i where i.Locate=002 and i.Name=j.Name),
Number=(Select i.Number From Table as i where i.Locate=004 and i.Name=j.Name)
From Table as j
上面這樣寫會得到錯誤"子查詢傳回不只1 個值"
請問要怎麼改呢?
謝謝
--
※ 發信站: 批踢踢實業坊(ptt.cc), 來自: 1.168.43.224
※ 文章網址: https://www.ptt.cc/bbs/Database/M.1541699610.A.2DF.html
推 jj2236789: 如果 a,b 只要各一列可以用 sum over partition by 11/10 16:08
推 jj2236789: 另一種如果 001、002 、003 是固定的常數而且不多可以 11/10 16:11
→ jj2236789: 考慮用 pivot 11/10 16:11