日期:2014-05-16 浏览次数:20759 次
WITH a1 (pluname,qty) AS
(
SELECT '香皂',1 UNION all
SELECT '香皂',2 UNION ALL
SELECT '洗手液',1 UNION all
SELECT '香皂',1 UNION all
SELECT '香皂',1 UNION ALL
SELECT '香皂',2
)
,a2 AS
(
SELECT *,ROW_NUMBER() OVER(ORDER BY @@SERVERNAME) re
FROM a1
)
,a3 AS
(
SELECT *,re-ROW_NUMBER() OVER(PARTITION BY pluname ORDER BY re) re2
FROM a2
)
SELECT pluname,SUM(qty) qty
FROM a3
GROUP BY pluname,re2
ORDER BY MIN(re)