我有四列,我想合併如下,按年份分組,game_dtMYSQL合併兩個結果行 - (失去和優勝隊伍)合併到一列
GAME_DT YEAR WIN LOSS
-------------------------------
20040804 2004 NYN MIL
20040804 2004 MIN ANA
20040804 2004 NYA OAK
輸出
GAME_DT YEAR TEAM RESULT
--------------------------------
20040804 2004 NYN W
20040804 2004 MIL L
20040804 2004 MIN W
20040804 2004 ANA L
20040804 2004 NYA W
20040804 2004 OAK L
UPDATE: 我的代碼,然後尼廷:
**
SELECT
GAME_DT,GAME_ID,
YEAR_ID,
if(home_score_ct>away_score_ct,home_team_id,away_team_id) as Team,
'W' as result
FROM
games
UNION
SELECT
GAME_DT,game_id,
YEAR_id,
if(home_score_ct<away_score_ct,home_team_id,away_team_id) as Team,
'L' as result
FROM
games
group by year_id,game_dt,game_id,result
order by game_dt,game_id
這工作!謝謝Nitin讓我朝着正確的方向前進。
歡迎來到StackOverflow!你到目前爲止嘗試過什麼嗎? StackOverflow不是一個免費的代碼寫入服務,並期望你[**嘗試首先解決你自己的問題**](http://meta.stackoverflow.com/questions/261592)。請更新您的問題以顯示您已經嘗試的內容,在[**最小,完整和可驗證的示例**](http://stackoverflow.com/help/mcve)中展示您面臨的特定問題。有關詳細信息,請參閱[**如何提出良好問題**](http://stackoverflow.com/help/how-to-ask),並參加[**遊覽**](http://該網站:) –