declare @dateto as datetime
set @dateto='8/1/11'
SELECT ti.userkey, SUM(l.Price * l.OrderQty) AS SumOfOrders, count (distinct ti.orderid) AS Ordercount
FROM SOLine AS l WITH (nolock) LEFT OUTER JOIN
SOImported AS ti ON ti.OrderId = l.OrderId LEFT OUTER JOIN
Sale AS o ON o.SaleId = ti.Inventory_Id
Where ti.createddate < @DateTo
GROUP BY ti.userkey, o.WootSiteId
需要此查询才能将订单计数如下所示:
Group userkeycount sumoforders
1 trans
2-30
31-60
61-90
91-120
121-150
151-180
181-210
211-240
241-270
271-300
301-330
331-360
360 or More
答案 0 :(得分:1)
declare @dateto as datetime
set @dateto='8/1/11'
SELECT
CASE WHEN Ordercount >= 360 THEN '360 or More'
WHEN Ordercount = 1 THEN '1 trans'
ELSE CAST (30 * FLOOR(Ordercount / 30) AS VARCHAR) + '-' + CAST (30 * CEILING (Ordercount / 30) AS VARCHAR)
END grp,
COUNT(*) userkeycount,
SUM(SumOfOrders) SumOfOrders
FROM (
SELECT ti.userkey,
SUM(l.Price * l.OrderQty) AS SumOfOrders,
count (distinct ti.orderid) AS Ordercount
FROM SOLine AS l WITH (nolock) LEFT OUTER JOIN
SOImported AS ti ON ti.OrderId = l.OrderId LEFT OUTER JOIN
Sale AS o ON o.SaleId = ti.Inventory_Id
Where ti.createddate < @DateTo
GROUP BY ti.userkey
) rs
GROUP BY CASE WHEN Ordercount >= 360 THEN '360 or More'
WHEN Ordercount = 1 THEN '1 trans'
ELSE CAST (30 * FLOOR(Ordercount / 30) AS VARCHAR) + '-' + CAST (30 * CEILING (Ordercount / 30) AS VARCHAR)
END
答案 1 :(得分:0)
我在Scrum Meister的查询中做了一些更改......下面你可以看到结果。
create table #T (ID int identity(0,1) primary key, Ordercount int)
insert #T (Ordercount) values (1)
insert #T (Ordercount) values (2)
insert #T (Ordercount) values (5)
insert #T (Ordercount) values (30)
insert #T (Ordercount) values (37)
insert #T (Ordercount) values (60)
insert #T (Ordercount) values (61)
insert #T (Ordercount) values (360)
insert #T (Ordercount) values (361)
select Ordercount,
CASE WHEN Ordercount > 360 THEN '360 or More'
WHEN Ordercount = 1 THEN '1 trans'
WHEN Ordercount <= 30 THEN '2-30'
ELSE
CAST (30*FLOOR(case when Ordercount%30 = 0 then Ordercount-1 else Ordercount end/30)+1 AS VARCHAR)
+ '-' +
CAST (30*FLOOR(case when Ordercount%30 = 0 then Ordercount-1 else Ordercount end/30)+30 AS VARCHAR)
END
from #T