【问题标题】:SQL Server : filtered rows to columnSQL Server:过滤行到列
【发布时间】:2021-08-02 07:41:57
【问题描述】:

我得到低于表 A 使用

select sn, processName, result
from xxxx
inner join xxxx on xxxx
inner join xxxx on xxxx
where processDate between xxx  xxx

表 A

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

如何转换表 A 或直接获取如下表?

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 子句和 pivot 对其进行转换。但它必须运行多次(3 次)查询,这需要很长时间(因为我的记录大约是 350,000,000)

Power BI,我正在加载 3 次原生查询表。

  1. “depthMeas”过滤表
  2. 'widthMeas' 过滤表
  3. “权重”过滤表。

然后将它们合并到一个表中。这需要大量的原生查询过程。

提前致谢

【问题讨论】:

标签: sql sql-server tsql filtered


【解决方案1】:

您可以使用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

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 2013-08-04
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2023-02-08
    相关资源
    最近更新 更多