我有一张表格,我已将其截图并上传到此处:
我需要创建一个sql查询来计算具有以下披萨数量的订单数量:1,2,3,4,5,6及以上6.
我已经尝试过一些SQL语句,但还没找到合适的语句。
我目前的尝试是
<asp:SqlDataSource ID="SqlDataSource1" runat="server"
ConnectionString="<%$ ConnectionStrings:ConnectPizza %>"
SelectCommand="Select '1' AS Caption, Count(quantity) AS Count FROM orders WHERE quantity = 1
UNION ALL
Select '2' AS Caption, Count(quantity) AS Count FROM orders WHERE quantity = 2
UNION ALL
Select '3' AS Caption, Count(quantity) AS Count FROM orders WHERE quantity = 3
UNION ALL
Select '4' AS Caption, Count(quantity) AS Count FROM orders WHERE quantity = 4
UNION ALL
Select '5' AS Caption, Count(quantity) AS Count FROM orders WHERE quantity = 5
UNION ALL
Select '6' AS Caption, Count(quantity) AS Count FROM orders WHERE quantity = 6
UNION ALL
Select 'Above 7' AS Caption, Count(quantity) AS Count FROM orders WHERE quantity > 7">
</asp:SqlDataSource>
但它没有成功。
我也想创建一个图表,但即使我的sql语句成功,我也不确定我为X和Y值成员输入的内容。
任何帮助都将不胜感激。
答案 0 :(得分:0)
这将按您希望的方式分配您的数量,然后将数量计数分组1-7。我没有在组中使用该案例,因为在许多数据库中不允许这样做。
Select o.quantity AS Caption, Count(o.quantity) AS Count
FROM (SELECT CASE WHEN quantity < 7 THEN Quantity ELSE 7 END) quantity from orders) o
group by o.quantity
答案 1 :(得分:0)
这应该会让你接近,但会将所有7个以上的数量标记为“7”
select case when quantity < 7 then quantity else 7 end,
count(*)
from orders
group by case when quantity < 7 then quantity else 7 end
要使7+标签正确,需要确切知道您正在使用的SQL变体