我必须进行查询才能显示gridview的数据。我计算每个门票类别的门票数量,但是如果该类别的门票数量少于3,我需要将其放入"其他"类别。
Select
count(ticket.id) As 'Tickets',
if(count(ticket.id) <= 3, "Other",ticketCategoryName) As 'Ticket Category'
FROM ds_tickets
GROUP BY 'Ticket Category
而不是像这样:
+----------------+-------------+
| TicketCategory | TicketCount |
+----------------+-------------+
| Category1 | 2 |
| Category2 | 3 |
| Category3 | 4 |
| Category4 | 5 |
+----------------+-------------+
我希望它像:
+----------------+-------------+
| TicketCategory | TicketCount |
+----------------+-------------+
| Other | 5 |
| Category3 | 4 |
| Category4 | 5 |
+----------------+-------------+
答案 0 :(得分:0)
尝试这样的事情
SELECT CASE WHEN CNT <= 3 THEN 'Other' ELSE TicketCategory END 'Ticket Category' ,
SUM(CNT)
FROM(
SELECT TicketCategory, COUNT(*) as CNT
FROM Table
GROUP BY TicketCategory
)M
GROUP BY CASE WHEN CNT <= 3 THEN 'Other' ELSE TicketCategory END