2016-11-30 184 views
1

對不起,如果標題太可怕了。有一個表T.與列名稱,貨號,那要麼包含我的狀態,O,或S.SQL - 查詢單獨列並根據列數據創建新列

例:

Name Num Status 
Bob 1  I 
Bob 2  O 
Bob 3  O 
John 4  I 
John 5  S 
Joe 6  O 

想要的結果看起來像:

Name Num I O S 
Bob 1 x 
Bob 2  x 
Bob 3  X 
John 4 X 
John 5   X 

謝謝!

編輯:後續問題。

Name Num I O S 
Bob  1 x 
Bob  1  x 
Bob  2  X 

需要的結果是:

Name Num I O S 
Bob  1 X X 
Bob  2  X 

編輯2:實際查詢:

SELECT Name, Card_Nmbr, 

[Out] = case when d.Status='I' then 'X' else '' end, 
[In] = case when d.Status='O' then 'X' else '' end, 
[Sales] = case when d.Status='S' then 'X' else '' end 
FROM [PCOdb].[dbo].[GC_Header] as h 
INNER JOIN GC_Detail as d on h.GC_TransNmbr = d.GC_TransNmbr 
INNER JOIN GC_Master as m on d.GCM_Nmbr = m.GCM_Nmbr 
INNER JOIN Galaxy1.dbo.GxUsers as u on h.UserID = u.UserID 
WHERE GC_TransDate between '11/29/16' and dateadd(day,1, '11/29/16') 
Group BY Card_Nmbr, Name 
+0

編輯2:實際查詢; – escapeclaws

回答

2

一個簡單的例子聲明,就可以在這裏

Select Name 
     ,Num 
     ,[I] = case when [Status]='I' then 'X' else '' end 
     ,[O] = case when [Status]='O' then 'X' else '' end 
     ,[S] = case when [Status]='S' then 'X' else '' end 
From YourTable 

EDIT to handle multiple rows

Select Name 
     ,Num 
     ,[I] = max(case when [Status]='I' then 'X' else '' end) 
     ,[O] = max(case when [Status]='O' then 'X' else '' end) 
     ,[S] = max(case when [Status]='S' then 'X' else '' end) 
From YourTable 
Group By Name,Num 

Edit 2 - Full Query (Assuming the Joins are doing what you need)

SELECT Name 
     ,Card_Nmbr 
     ,[Out] = max(case when d.[Status]='I' then 'X' else '' end) 
     ,[In] = max(case when d.[Status]='O' then 'X' else '' end) 
     ,[Sales] = max(case when d.[Status]='S' then 'X' else '' end) 
FROM [PCOdb].[dbo].[GC_Header] as h 
JOIN GC_Detail as d on h.GC_TransNmbr = d.GC_TransNmbr 
JOIN GC_Master as m on d.GCM_Nmbr = m.GCM_Nmbr 
JOIN Galaxy1.dbo.GxUsers as u on h.UserID = u.UserID 
WHERE GC_TransDate between '2016-11-29' and DateAdd(DD,1,'2016-11-29') 
Group BY Card_Nmbr, Name 
+0

謝謝!我有一個快速跟進;現在可以說Bob有兩個項目,Num = 2,其中一個狀態爲O,一個狀態爲I.如果我需要顯示一列然後像這樣:Bob 2 XX(其中有兩個X,在I和O之下專欄)再次感謝! – escapeclaws

+0

@escapeclaws就這樣,我清楚,你想讓鮑勃有一個與O和我選擇的行? –

+0

是的,我在上面添加了一個用於說明的編輯。再次謝謝你! – escapeclaws

1

使用Case表達。

演示:

Create table #temp 
     (Name varchar (10), 
     Num int , 
     [Status] char(1)) 

insert into #temp values ('Bob' , '1' , 'I') 
insert into #temp values ('Bob' , '2' , 'O') 
insert into #temp values ('Bob' , '3' , 'O') 
insert into #temp values ('John' , '4' , 'I') 
insert into #temp values ('John' , '5' ,'S') 
insert into #temp values ('Joe' , '6' , 'O') 



select Name, 
     Num, 
     [I] = case 
       when [Status] ='I' 
       then 'X' 
       else '' 
       end 
     ,[O] = case 
       when [Status] ='O' 
       then 'X' 
       else '' 
       end 
     ,[S] = case when [Status] ='S' 
       then 'X' 
       else '' 
       end 
from #temp 

drop table #temp 

結果:

enter image description here

0

對於SQL Server 2012和更高版本可以使用IIF (Transact-SQL)功能了。

Select Name 
     ,Num 
     ,IIF([Status] = 'I' , 'X', '') AS [I] 
     ,IIF([Status] = 'O' , 'X', '') AS [O] 
     ,IIF([Status] = 'S' , 'X', '') AS [S] 
FROM TableName