2017-08-09 170 views
1

我有如下表: - table1的將多個行分爲單列

ID  Desc 

1   ABC 

2   DEF 

3   GHI 

3   JKL 

4   MNO 

4   PQR 

4   STU 

我要顯示的數據爲: -

ID  Desc 

1   ABC 

2   DEF 

3   GHI 

      JKL 

4   MNO 

      PQR 

      STU 

我試圖使它爲: -

select distinct ID , Desc from table1 

但它不工作。

+1

這聽起來像是在表現層上應該做的事情,而不是數據訪問。 – Tanner

回答

2

什麼,我知道你不能,你有請求後修改表的外觀(例如HTML)

+0

這不提供問題的答案。一旦你有足夠的[聲譽](https://stackoverflow.com/help/whats-reputation),你將可以[對任何帖子發表評論](https://stackoverflow.com/help/privileges/comment);相反,[提供不需要提問者澄清的答案](https://meta.stackexchange.com/questions/214173/why-do-i-need-50-reputation-to-comment-what-c​​an- I-DO-代替)。 - [來自評論](/ review/low-quality-posts/16978894) –

4

在SQL Server的可能:

Select distinct ID , stuff((SELECT ','+Description FROM #a a WHERE 
    t.id=a.id for xml path('')),1,1,'') Description 
    from #a T 


    Output : Id  Desc 
      1   ABC 
      2   DEF 
      3   GHI,JKL 
      4   MNO,PQR,STU 
2

這需要要在表示層處理,但您可以查詢如下:

Select Case when Row_Number() over(Partition by Id order by Id) = 1 then Id else Null end as Id, 
    [Desc] from #data 
1

下面的查詢將生成所需的結果。

SELECT CASE WHEN (Rank() Over(ORDER BY id ASC)) = (Row_Number() Over (ORDER BY id ASC)) THEN id ELSE NULL END as ID, desc FROM table1