如何"崩溃"行?

时间:2016-01-15 21:44:06

标签: sql sql-server sql-server-2012

所以,如果我当前的结果集是:

 Set  |  Label  |  Val
------------------------
  A   |  "LabA" |  NULL
  A   |  "LabA" |   1
  A   |  "LabB" |  NULL
  A   |  "LabB" |   2
  B   |  "LabA" |  NULL
  B   |  "LabA" |   3
  B   |  "LabC" |  NULL

我真正想要的是:

 Set  |  Label  |  Val
------------------------
  A   |  "LabA" |   1
  A   |  "LabB" |   2
  B   |  "LabA" |   3
  B   |  "LabC" |  NULL

我怎能"崩溃"获取所需结果集的行?

1 个答案:

答案 0 :(得分:1)

您只需要在SetLabel列上使用MAX()进行GROUP BY汇总:

Select  Set, Label, Max(Val) Val
From    YourTable
Group By Set, Label
Order By Set, Label