Skip to content
Advertisement

SQL: Query the same column 3 times with 3 different where clauses

Trying to show a table with 3 columns that are prices that need to be displayed. the columns are differentiated by ‘price_type’ and there are 3 different price types.

Its probably something obvious im missing but something like:

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

I’m not sure what the actual syntax would be, but any help is appreciated.

Advertisement

Answer

With conditional aggregation:

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
User contributions licensed under: CC BY-SA
9 People found this is helpful
Advertisement