I am fiddling on SQL fiddle with group by clause and finding the percentage based on the temp table "A" result.
Here is the fiddle.
http://sqlfiddle.com/#!9/faf2f/6959
I did come across that there are different ways of achieving this like by using union all but my question is why this query brings back only one row instead of two rows.
- List item
Data:
TotalCost
230
200
100
1254
Query:
SELECT Category, Cnt , Cnt/sum(Cnt) as percent from (
SELECT
Case When TotalCost < 301 Then '0-300'
Else ' > 300' End as Category,
count(*) as cnt
FROM Cars
group by Category
) A
;
Expected Result:
Category Cnt percent
0-300 3 75
> 300 1 25
Actual Result:
Category Cnt percent
> 300 1 25