我有一个名为" ORDER_HISTORY"其中有一个名为" Status"。" Status"列包含不同商店的不同状态值。
我的要求是我需要为每个商店获取不同的状态值(count)并在相应的列中显示它们。例如:如果我有53(order_cancelled)和57(order_completed)作为两个状态,并且商店号码20我有两个订单状态为57(ieorder_completed)和一个订单状态为53(order_cancelled),然后我需要以下面的格式显示结果(计数):
================================================
Store_ID || order_cancelled || order_completed||
------------------------------------------------
20 || 1 || 2 ||
================================================
如何编写执行上述工作的查询?
请帮忙。谢谢!
答案 0 :(得分:1)
试试这个
SELECT a.Store_ID,b.total as order_completed,c.total as order_completed FROM ORDER_HISTORY a
LEFT OUTER join (select Store_ID,count(*) as total from ORDER_HISTORY where Status=57 group by Store_ID) b on a.Store_ID=b.Store_ID
LEFT OUTER join (select Store_ID,count(*) as total from ORDER_HISTORY where Status=53 group by Store_ID) c on a.Store_ID=c.Store_ID
group by a.Store_ID
答案 1 :(得分:0)
试试这个
SELECT Store_ID, (SELECT COUNT(Status)FROM order_history WHERE状态= 53)AS order_cancelled, (选择COUNT(Status)FROM order_history WHERE Status = 57)order_completed 从 clover_source.order_history 按Store_ID分组;
答案 2 :(得分:0)
试试这个:
SELECT dbo.ORDER_HISTORY.Store_ID,ORDER_HISTORY_Cancelled.order_cancelled,ORDER_HISTORY_completed.order_completed FROM dbo.ORDER_HISTORY LEFT OUTER JOIN (SELECT Store_ID,COUNT(Status)AS order_cancelled FROM dbo.ORDER_HISTORY AS TotalCancelled WHERE(状态= N'53') GROUP BY Store_ID)AS ORDER_HISTORY_Cancelled ON dbo.ORDER_HISTORY.Store_ID = ORDER_HISTORY_Cancelled.Store_ID LEFT OUTER JOIN (SELECT Store_ID,COUNT(Status)AS order_completed 来自dbo.ORDER_HISTORY AS TotalComleted WHERE(状态= N'57') GROUP BY Store_ID)AS ORDER_HISTORY_completed ON dbo.ORDER_HISTORY.Store_ID = ORDER_HISTORY_completed.Store_ID GROUP BY dbo.ORDER_HISTORY.Store_ID,ORDER_HISTORY_Cancelled.order_cancelled,ORDER_HISTORY_completed.order_completed