SQL 服务器:过滤行到列
SQL Server : filtered rows to column
我低于Table A 使用
select sn, processName, result
from xxxx
inner join xxxx on xxxx
inner join xxxx on xxxx
where processDate between xxx xxx
Table一个
sn
processName
result
a01
depthMeas
0.1
a01
widthMeas
2
a01
weight
11.6
b02
depthMeas
0.2
b02
widthMeas
2.1
b02
weight
11.3
a24
depthMeas
0.15
a24
widthMeas
2.2
a24
weight
11.5
我怎样才能运行sform Table A 或直接得到 table 如下所示?
sn
depthMeas
widthMeas
weight
a01
0.1
2.0
11.6
b02
0.2
2.1
11.3
a24
0.15
2.2
11.5
可能我可以 运行使用 where 子句和主元来构造它。但它必须是 运行 多次(3 次)查询,这将花费很长时间(因为我的记录大约是 350,000,000)
Power BI,我正在加载 3 次本机查询 table。
- 'depthMeas' 已过滤 table
- 'widthMeas' 已过滤 table
- 'weight' 过滤 table.
然后将它们合并为一个table。这在本机查询过程中花费了大量时间。
提前致谢
您可以使用 GROUP BY
,如:
with
q as (
-- your query here
)
select
sn,
max(case when processname = 'depthMeas' then result end) as depthmeas,
max(case when processname = 'widthMeas' then result end) as widthmeas,
max(case when processname = 'weight' then result end) as weight
from q
group by sn
我低于Table A 使用
select sn, processName, result
from xxxx
inner join xxxx on xxxx
inner join xxxx on xxxx
where processDate between xxx xxx
Table一个
sn | processName | result |
---|---|---|
a01 | depthMeas | 0.1 |
a01 | widthMeas | 2 |
a01 | weight | 11.6 |
b02 | depthMeas | 0.2 |
b02 | widthMeas | 2.1 |
b02 | weight | 11.3 |
a24 | depthMeas | 0.15 |
a24 | widthMeas | 2.2 |
a24 | weight | 11.5 |
我怎样才能运行sform Table A 或直接得到 table 如下所示?
sn | depthMeas | widthMeas | weight |
---|---|---|---|
a01 | 0.1 | 2.0 | 11.6 |
b02 | 0.2 | 2.1 | 11.3 |
a24 | 0.15 | 2.2 | 11.5 |
可能我可以 运行使用 where 子句和主元来构造它。但它必须是 运行 多次(3 次)查询,这将花费很长时间(因为我的记录大约是 350,000,000)
Power BI,我正在加载 3 次本机查询 table。
- 'depthMeas' 已过滤 table
- 'widthMeas' 已过滤 table
- 'weight' 过滤 table.
然后将它们合并为一个table。这在本机查询过程中花费了大量时间。
提前致谢
您可以使用 GROUP BY
,如:
with
q as (
-- your query here
)
select
sn,
max(case when processname = 'depthMeas' then result end) as depthmeas,
max(case when processname = 'widthMeas' then result end) as widthmeas,
max(case when processname = 'weight' then result end) as weight
from q
group by sn