用于分组数据的SQL查询

时间:2016-10-13 10:29:45

标签: sql sql-server group-by

我有一些数据集如下。

Code          Values
521            1
522            1
523            1
524            0
525            0
526            1
527            1

我期待输出如下所示:

Group     CD        Values
G1      521-523       1
G2      524-525       0
G3      526-527       1

3 个答案:

答案 0 :(得分:2)

我倾向于使用行数方法的区别:

select concat('G', row_number() over (order by min(value)) as grp,
       concat(min(code), '-', max(code)) as codes,
       value
from (select t.*,
             row_number() over (order by code) as seqnum_c,
             row_number() over (partition by value order by code) as seqnum_vc
      from t
     ) t
group by value, (seqnum_c - seqnum_vc);

答案 1 :(得分:1)

尝试使用LEAD()ROW_NUMBER()和条件聚合:

SELECT 'G' + ROUND((p.rnk + 1) / 2) as group,
       MAX(CASE WHEN p.rnk%2 = 1 THEN p.code END) + '-' +
       MAX(CASE WHEN p.rnk%2 = 0 THEN p.code END) 
FROM (
    SELECT s.*,
           ROW_NUMBER() OVER(ORDER BY s.code) as rnk
    FROM (
        select t.*,
               LEAD(t.values,1) OVER(ORDER BY t.code DESC) as last_val
        FROM YourTable t) s
    WHERE s.values <> s.last_val ) p
GROUP BY 'G' + ROUND((p.rnk + 1) / 2)

答案 2 :(得分:1)

试试这个解决方案:

DECLARE @YourTable TABLE (Code INT, Value INT)

INSERT INTO @YourTable
VALUES
(521,1),
(522,1),
(523,1),
(524,0),
(525,0),
(526,1),
(527,1)

;WITH CTE AS
(
    SELECT  *, ROW_NUMBER() OVER(ORDER BY CODE) - ROW_NUMBER() OVER(PARTITION BY Value Order BY CODE) Grp
    FROM    @YourTable
)
SELECT  ROW_NUMBER() OVER(ORDER BY(SELECT MIN(Code))) Grp
        ,CAST(MIN(Code) AS VARCHAR(30)) +'-'+ CAST(MAX(Code) AS VARCHAR(30)) CD
        ,MAX(Value) Val
FROm    CTE
GROUP   BY Grp