[SQL ] 每个Field都要做SubQuery

楼主: licheer (Keep The Faith)   2018-11-09 01:53:28
数据库名称: 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 个值"
请问要怎么改呢?
谢谢
作者: jj2236789 (Absinthe)   2018-11-10 16:08:00
如果 a,b 只要各一列可以用 sum over partition by另一种如果 001、002 、003 是固定的常数而且不多可以考虑用 pivot

Links booklink

Contact Us: admin [ a t ] ucptt.com