我正在使用SQL Server 2012 Express(T-SQL),并且在此表中包含以下数据(AwardsPlayers)。我的数据库不支持MySQL。我看过here和here,但我仍然无法理解。SQL Server 2012:为单个列返回一个列中的多个字符串ID
如果我创建了一个函数,它是否可以在一个4表连接中工作,我有多个聚合列作为该连接中只有一列的多列?
playerID awardID yearID lgID notes
puckett Gold Glove 1986 AL OF
puckett Silver Slugger 1986 AL OF
puckett TSN All-Star 1986 AL OF
puckett Gold Glove 1987 AL OF
puckett Silver Slugger 1987 AL OF
puckett TSN All-Star 1987 AL OF
puckett Gold Glove 1988 AL OF
我有一个更大的加盟我使用的,但是从这个表我希望能够返回查询每个playerID和yearID对应于当年awardID。
例如从上面的查询表会返回此:
playerID yearID Awards
puckett 1986 Gold Glove, Silver Slugger, TSN All-Star
puckett 1987 Gold Glove, Silver Slugger, TSN All-Star
puckett 1988 Gold Glove
而在颁奖的空栏,应该有当年没有奖励。
Coalesce不能按原样工作,因为它不是返回INT,而是不能使用VARCHAR类型。
我目前的查询:
--Player Stat Cards, along with Awards, Salary
select
m.namefirst,
m.namelast,
b.yearID,
b.yearID-m.birthyear as Age,
b.G,b.AB,b.R,b.H,b.[2B],b.[3B],b.HR,b.RBI,b.SB,b.BB,b.SO,left(round((b.h*1.000/b.ab),3),5) as Average
,b.IBB,b.HBP,b.SH,b.SF,b.SF,b.GIDP
, **COALESCE(a.awardID + ',' + '',0) +a.awardID**
from batting b
inner join
[master] m on b.playerID=m.playerID
inner join
AwardsPlayers a
on m.playerID=a.playerID
inner join Salaries s
on m.playerID=s.playerID
where m.playerID = 'puckett'
group by
m.namefirst,
m.namelast,
b.yearID,
b.yearID-m.birthyear,
b.G,b.AB,b.R,b.H,b.[2B],b.[3B],b.HR,b.RBI,b.SB,b.BB,b.SO,left(round((b.h*1.000/b.ab),3),5)
,b.IBB,b.HBP,b.SH,b.SF,b.SF,b.GIDP, **COALESCE(a.awardID + ',' + '',0) +a.awardID**
在此先感谢。
通过增加Sswater Shi的贡献......我的表情现在给出了这个(好得多),但是,它返回在该列中找到的所有值,而不管它们是否是该玩家对该年份ID的特定值。
我不能猜测出你的表结构,你能张贴在这里简要或直接给我:[email protected] –