I'm working SQLServer 2019 and I have orders table. Orders table has columns orderid, userid, country, site, count, price. I need help write query. You can see below with details.
Question: Show me that how much user ordered from 2010 to present once, twice, etc in 5 stripe ([1], [2], [3], [4-10], [10-100]) based on country
Example Result:
Country 1 2 3 4-10 10-100
---------------------------------------------------------
US 0 0 3 4 5
GB 10 10 8 50 60
NL 20 20 20 100 30
....
My query: I used pivot table from 1 to 3 and I have correct result. But, I couldn't write the ranges ten to four and ten to one hundred in the pivot table.
I ran query for below;
select * from (
SELECT Country,
count(*) as total,
count as totalpay
FROM [CRM].[dbo].[Orders]
where date like '%2010%'
group by Country,count
) countrytotalcnt
pivot
(
sum(total) for totalpay in ([1],[2],[3],[4-10],[10-100])
)countrytotal;
I have error for below;
Msg 8114, Level 16, State 1, Line 24
Error converting data type nvarchar to int.
Msg 473, Level 16, State 1, Line 24
The incorrect value "4-10" is supplied in the PIVOT operator.
Completion time: 2021-10-13T13:55:47.1067875+03:00