【问题标题】:SQL: Query the same column 3 times with 3 different where clausesSQL:使用 3 个不同的 where 子句查询同一列 3 次
【发布时间】:2020-02-14 17:09:24
【问题描述】:

试图显示一个包含 3 列的表格,这些列是需要显示的价格。这些列由“price_type”区分,并且有 3 种不同的价格类型。

这可能是我缺少的一些明显的东西,但类似于:

Select price as 'current', price as '10min', price as '30min'
from table
where Price_Type(current) = 'current' AND Price_Type(10min) = '10min' AND 
Price_Type(30min) = '30min'
Order by date desc

我不确定实际的语法是什么,但感谢任何帮助。

【问题讨论】:

  • 欢迎来到 Stack Overflow。获取Tour 并阅读How To Ask。阅读此well structured question,然后阅读edit 您的问题,其中包含示例数据、表结构和所需结果,以帮助我们为您提供帮助。

标签: sql sql-server tsql group-by conditional-aggregation


【解决方案1】:

使用条件聚合:

select date,
  max(case when Price_Type = 'current' then price end) as [current],
  max(case when Price_Type = '10min' then price end) as [10min],
  max(case when Price_Type = '30min' then price end) as [30min]
from table
group by date
order by date desc

【讨论】:

  • 天哪,你是救生员。非常感谢!!
猜你喜欢
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 2021-10-20
  • 1970-01-01
  • 2017-06-09
  • 1970-01-01
  • 1970-01-01
相关资源
最近更新 更多