I have a SQL table with [Items]
and their associated [Cost]
, and I am looking to create a histogram showing cost distribution. This is my (working) query used to create bins:
SELECT
[$0-$25]=COUNT(CASE WHEN [Cost]>=0 AND [Cost]<25 THEN 1 END),
[$25-$50]=COUNT(CASE WHEN [Cost]>=25 AND [Cost]<50 THEN 1 END)
--etc.
FROM
table_name
The cost range can vary wildly for different items in the table - in other words a highest cost cannot be hard-coded. What's the best way to start at $0
and create additional $25
columns as needed? I think the solution involves using MAX([Cost])
and PIVOT
together, but I'm not sure how to go about it.