加入多个表顺序

时间:2017-08-25 15:57:01

标签: sql-server join

我正在尝试将几个不同的表连接到一个表。我没有得到我正在寻找的确切输出,我想知道我是否以错误的顺序加入他们。

基本上我只想Q2IMSSiteHistory2015old。但我也有表格不能直接加入主表。所以我必须加入到一起才能加入Q2IMSSiteHistory2015Old。

Select Hsp.HspId, CMSid as MSFacility
From Q2IMSSiteHistory2015old
Right Join HSP on Cast(HSP.CMSid as nvarchar) = Q2IMSSiteHistory2015old.POS
right Join Q2IMSSurgeonHistory2015 on Q2IMSSurgeonHistory2015.POS = Q2IMSSiteHistory2015old.POS
Right join SurgeonMaster on SurgeonMaster.NPI = Q2IMSSurgeonHistory2015.NPI
Group by Hsp.HspId, CMSId

感谢您的帮助。

1 个答案:

答案 0 :(得分:0)

如果未在nvarchar上设置长度,则会将其截断为1个字符。

DECLARE @txt AS NVARCHAR

SET @txt = 'hello'
SELECT @txt

- 结果='h'

TRY:

Select Hsp.HspId, CMSid as MSFacility
From Q2IMSSiteHistory2015old
Right Join HSP on Cast(HSP.CMSid as nvarchar(255)) = Q2IMSSiteHistory2015old.POS
right Join Q2IMSSurgeonHistory2015 on Q2IMSSurgeonHistory2015.POS = Q2IMSSiteHistory2015old.POS
Right join SurgeonMaster on SurgeonMaster.NPI = Q2IMSSurgeonHistory2015.NPI
Group by Hsp.HspId, CMSId