Re: [SQL ] 每個Field都要做SubQuery

作者: cutekid (可愛小孩子)   2018-11-09 14:32:55
select Name,
isnull([001],0) as [001],
isnull([002],0) as [002],
isnull([003],0) as [003]
from table
pivot (
sum(Number) for Location in ([001],[002],[003])
) as pvt
※ 引述《licheer (Keep The Faith)》之銘言:
: 資料庫名稱: 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 個值"
: 請問要怎麼改呢?
: 謝謝

Links booklink

Contact Us: admin [ a t ] ucptt.com