我有这样的数据:
+----+-------------------------+----------+----------+
| ID | DateReceived | Quantity | VendorID |
+----+-------------------------+----------+----------+
| 1 | 2010-08-09 06:53:44.783 | 2 | 1 |
| 2 | 2010-08-01 13:31:26.893 | 1 | 1 |
| 3 | 2010-07-26 07:52:29.403 | 2 | 1 |
| 4 | 2011-03-22 13:31:11.000 | 1 | 2 |
| 5 | 2011-03-22 13:31:11.000 | 1 | 2 |
| 6 | 2011-03-22 11:27:01.000 | 1 | 2 |
| 7 | 2011-03-18 09:04:58.000 | 1 | 1 |
| 8 | 2011-12-17 08:21:29.000 | 1 | 3 |
| 9 | 2012-08-10 10:55:20.000 | 9 | 3 |
| 10 | 2012-08-02 20:18:10.000 | 5 | 1 |
| 11 | 2012-07-12 20:44:36.000 | 3 | 1 |
| 12 | 2012-07-05 20:45:29.000 | 1 | 1 |
| 13 | 2013-03-22 13:31:11.000 | 1 | 2 |
| 14 | 2013-03-22 13:31:11.000 | 1 | 2 |
+----+-------------------------+----------+----------+
我希望按DateReceived
对数据进行排序,并将Quantity
求和。但是,我想将Quantity
归为VendorID
,只要它们相邻,就像下面的示例输出一样。
+----------+----------+
| VendorID | Quantity |
+----------+----------+
| 1 | 5 |
| 2 | 3 |
| 1 | 1 |
| 3 | 10 |
| 1 | 9 |
| 2 | 2 |
+----------+----------+
我目前正在通过加载所有行并在我的应用程序代码中浏览它来完成此操作。这是我想要消除的软件瓶颈。
什么是MS Sql Server查询以生成所需的输出?
PS。有关更好标题的任何建议吗?
答案 0 :(得分:4)
在 SQL Server 2005 + 中,您可以执行以下操作:
with cte as (
select
VendorID, Quantity,
row_number() over(partition by VendorID order by DateReceived) as rn1,
row_number() over(order by DateReceived) as rn2
from Table1
)
select
VendorID, sum(Quantity) as Quantity
from cte
group by VendorID, rn2 - rn1
order by min(rn2)
<强> sql fiddle demo 强>
在 SQL Server 2012 中,您可以使用lag()函数:
with cte as (
select
VendorID, Quantity, DateReceived,
case when lag(VendorID) over(order by DateReceived) <> VendorID then 1 else 0 end as rn
from Table1
), cte2 as (
select
VendorID, Quantity, sum(rn) over(order by DateReceived) as s
from cte
)
select
VendorID, sum(Quantity)
from cte2
group by VendorID, s
order by s asc
<强> sql fiddle demo 强>
BTW,看起来你的输出不正确。正确的是:+----------+----------+
| VendorID | Quantity |
+----------+----------+
| 1 | 6 |
| 2 | 3 |
| 3 | 1 |
| 1 | 9 |
| 3 | 9 |
| 2 | 2 |
+----------+----------+
答案 1 :(得分:2)
试试这个解决方案:
SELECT z.VendorID, z.GroupID,
MIN(z.DateReceived) AS DateReceivedStart,
MAX(z.DateReceived) AS DateReceivedStop,
SUM(z.Quantity) AS SumOfQuantity
FROM
(
SELECT y.VendorID,
y.RowNum1 - y.RowNum2 AS GroupID,
y.DateReceived,
y.Quantity
FROM
(
SELECT *, ROW_NUMBER() OVER(ORDER BY x.DateReceived) AS RowNum1,
ROW_NUMBER() OVER(ORDER BY x.VendorID, x.DateReceived) AS RowNum2
FROM @MyTable x
) y
) z
GROUP BY z.VendorID, z.GroupID
ORDER BY DateReceivedStart