【问题标题】:SQL Select to obtain more column with different conditionsSQL Select 获取更多不同条件的列
【发布时间】:2020-05-22 10:57:27
【问题描述】:

您好,我无法创建显示与不同 WHERE 条件相关的不同列的查询。这是我的用例: 数据库报告在多台机器上测试的 UNITS 上执行的测试测量。

我需要为每个 DISTINCT TEST 生成一份报告,其中包含每个 UNIT 的 TEST 值的 AVERAGE 值。 (我可以硬编码 MACHINE 编号,以便简化查询)。

  • 测试 A:单元 1 的平均测量值,单元 2 的平均测量值,单元 N 的平均测量值
  • 测试 B:单元 1 的平均测量值,单元 2 的平均测量值,单元 N 的平均测量值
  • 测试 C:单元 1 的平均测量值,单元 2 的平均测量值,单元 N 的平均测量值
  • ...

【问题讨论】:

    标签: sql multiple-columns


    【解决方案1】:

    我想你想要条件聚合:

    select testname,
           avg(case when unit = 11 then measure end) as unit_11,
           avg(case when unit = 22 then measure end) as unit_22,
           avg(case when unit = 33 then measure end) as unit_33,
           avg(case when unit = 44 then measure end) as unit_44
    from t
    group by testname;
    

    【讨论】:

    • 非常感谢。这正是我需要的:)
    猜你喜欢
    • 2013-01-03
    • 2021-09-10
    • 2015-12-08
    • 2019-08-08
    • 1970-01-01
    • 1970-01-01
    • 2012-02-18
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多