作者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://webptt.com/cn.aspx?n=bbs/Database/M.1541699610.A.2DF.html
1F:推 jj2236789: 如果 a,b 只要各一列可以用 sum over partition by 11/10 16:08
2F:推 jj2236789: 另一种如果 001、002 、003 是固定的常数而且不多可以 11/10 16:11
3F:→ jj2236789: 考虑用 pivot 11/10 16:11