2016-04-22 58 views
0

以下是在單個表查詢的結果是:結合兩行

ID ColumnA ColumnB ColumnC 1 -1 300 400 2 200 300 0 將查詢什麼樣子,如果我想類似下面的結果:

ColumnA ColumnB ColumnC 200 300 400 基本上,使用ColumnB作爲參考,並用ColumnAColumnC(展平?)下各自的非零對應取代-1和0。

一些已知條件:

  • 總是兩排
  • 始終-1 ColumnA和0 ColumnC

這是我所沒有的經驗,所以我問SO的幫幫我。謝謝。

+1

'從表中選擇max(columna),max(columnb),max(columnc)? –

回答

1

所有CLM1,CLM2,CLM3都是必須定義了int類型,域名,浮然後將其應用於

select Top(1) max(clm1), max(clm2), max(clm3) from tbl group by clm1,clm2,clm3 

其他試試這個,

select max(clm1), max(clm2), max(clm3) from tbl 

試試這個

1
select * 
from 
(select ID ColumnA, ColumnB, ColumnC, 
     , lead (ColumnA, 1, 'default') over (order over ID) as ColumnAlead 
    from table 
) tt 
where tt.ColumnC <> 0