为每个记录子集提供一个组ID

时间:2012-12-21 00:29:11

标签: sql sql-server

我需要为记录子集分配排名。 样本数据:

Colour Code        Size Code
DWMUL040             7
DWMUL040             8
DWMUL040             9
DWMUL040             10
DWMUL040             7
DWMUL040             8
DWMUL040             9
DWMUL040             10
DWMUL040             7
DWMUL040             8
DWMUL040             9
DWMUL040             10
DWMUL040             7
DWMUL040             8
DWMUL040             9
DWMUL040             10

我需要数据看起来像这样:

Group ID            Colour Code        Size Code
1                   DWMUL040             7
1                   DWMUL040             8
1                   DWMUL040             9
1                   DWMUL040             10
2                   DWMUL040             7
2                   DWMUL040             8
2                   DWMUL040             9
2                   DWMUL040             10
3                   DWMUL040             7
3                   DWMUL040             8
3                   DWMUL040             9
3                   DWMUL040             10
4                   DWMUL040             7
4                   DWMUL040             8
4                   DWMUL040             9
4                   DWMUL040             10

列表将被订购,基本上,只要尺码改变,它就是一个新的组。我觉得这个会很难,因为尺码(这些是服装尺码)可能是“像XL一样”。

1 个答案:

答案 0 :(得分:0)

  select [colour code], [size code],
         row_number() over (partition by [colour code], [size code]
                            order by 1/0) [group id]
    from tbl
order by [group id], [colour code], [size code];