對不起,如果標題太可怕了。有一個表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
編輯2:實際查詢; – escapeclaws