SQL-数据仅在行中可见一次,而不是多次可见

时间:2018-10-23 19:59:06

标签: sql sql-server tsql ssms

在下面的屏幕快照中,我的输出以这种方式可见,但是如果他们使用许多代码,我确实需要在一行中输出。

我目前有这样的输出:

originalID       datein    Dateout      CameID       code    code2    code3
---------------------------------------------------------------------------
V000123456789    1/8/2018   1/8/2018    V1245425486  43239    NULL    NULL
V000123456       1/8/2018   1/8/2018    V1245432734  45385    43239   NULL
V000123456       1/8/2018   1/8/2018    V1245432734  43239    43239   NULL
V008569745213    1/8/2018   1/8/2018    V1245434733  45385    NULL    NULL
V00014795142     1/10/2018  1/10/2018   V1245435872  49520    NULL    NULL
V00098745697     3/1/2018   3/1/2018    V1245436554  43239    43450   NULL
V00098745697     3/1/2018   3/1/2018    V1245436554  43450    43450   NULL
V000147856394    1/4/2018   1/4/2018    V1245437154  49587    NULL    NULL
V00085274112     1/4/2018   1/5/2018    V1245438086  29888    29881   NULL
V00085274112     1/4/2018   1/5/2018    V1245438086  29881    29881   NULL
V000789456123    1/3/2018   1/3/2018    V1245442595  29827    29826  24358
V000789456123    1/3/2018   1/3/2018    V1245442595  29826    29826  24358
V000789456123    1/3/2018   1/3/2018    V1245442595  24358    29826  24358

但是我需要将其分组,例如,如果一个originalID具有许多代码,则应将其分组为包含OriginalID,date in,dateout,cameID,code,code2,code3的一行

我需要输出看起来像这样:

  originalID       datein     Dateout      CameID      code    code2   code3
  ---------------------------------------------------------------------------         
  V000123456789    1/8/2018   1/8/2018   V1245425486    43239   NULL    NULL
  V000123456       1/8/2018   1/8/2018   V1245432734    45385   43239   NULL
  V008569745213    1/8/2018   1/8/2018   V1245434733    45385   NULL    NULL
  V00014795142     1/10/2018  1/10/2018  V1245435872    49520   NULL    NULL
  V00098745697     3/1/2018   3/1/2018   V1245436554    43239   43450   NULL
  V000147856394    1/4/2018   1/4/2018   V1245437154    49587   NULL    NULL
  V00085274112     1/4/2018   1/5/2018   V1245438086    29888   29881   NULL
  V000789456123    1/3/2018   1/3/2018   V1245442595    24358   29826  24358

1 个答案:

答案 0 :(得分:0)

我认为您只需要汇总:

select originalId, datein, dateout, camelid, min(code), min(code2), min(code3)
from t
group by originalId, datein, dateout, camelid