我的问题似乎非常简单,但我正在努力掌握一种更简单的方法来实现所需的输出。
问题:我在表格中有条目。这些条目由ID列标识。他们与一张表格相结合,该表格显示了他们持有或持有的特定志愿者职位。如果它们被识别为具有多个位置,则它们的记录会多次返回。
解决方案:我想在一列中列出他们的位置,以便他们基于ID列的输入只返回一次。我尝试过使用STUFF和CONCAT,但它们返回的是每个条目保持的相同位置的倍数,而不是查明它们是否有多个位置并列出它们,这意味着我可能错误地使用了这些函数。
这是当前的输出:
ID FIRST_NAME current_flag Position
---------- -------------------- ------------ ---------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------
106379 Christine 1 NOMAL
106379 Christine 1 SFC
106418 Mary 1 CVP
106751 Denise 1 SDFA
106885 Marianna 1 RCRA
107244 Jennifer 1 RCF
这是所需的输出:
ID FIRST_NAME current_flag Position
---------- -------------------- ------------ ---------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------
106379 Christine 1 NOMAL,SFC
106418 Mary 1 CVP
106751 Denise 1 SDFA
106885 Marianna 1 RCRA
107244 Jennifer 1 RCF
我删除了无关的列,因为输出不利于显示所需的输出,但它们包含在我的代码中,如下所示。 现行守则:
SELECT DISTINCT Name.ID,
Name.FIRST_NAME,
Name.LAST_NAME,
Name_Address.ADDRESS_1,
Name_Address.CITY,
Name_Address.STATE_PROVINCE,
Name_Address.ZIP,
Name_Address.PREFERRED_MAIL,
vcsiboCurrentCommittee.current_flag,
CONVERT(VARCHAR(10), Name.BIRTH_DATE, 101) AS BirthDate,
vcsiboCurrentCommittee.Position
FROM Name INNER JOIN
Name_Address ON Name.ID = Name_Address.ID INNER JOIN
vcsiboCurrentCommittee ON Name.ID = vcsiboCurrentCommittee.ID
WHERE (Name_Address.PREFERRED_MAIL = 1.00)
AND (vcsiboCurrentCommittee.current_flag = 1.00)
AND (Name.ID <> 10) AND (Name.BIRTH_DATE <> '')
AND (vcsiboCurrentCommittee.CommitteeCode IN
('ALUMNAE_DEPT', 'COLLEGIATE-DEPT', 'EDUCATION', 'FINANCIAL', 'INTERN_COUNCIL', 'IPDEPT', 'MEMBERSHIP','PANHELLENIC', 'REG_1', 'REG_2', 'REG_3', 'REG_4', 'REG_5', 'REG_6', 'REG_7', 'REG_8'))
使用STUFF的代码:
SELECT DISTINCT Name.ID,
Name.FIRST_NAME,
Name.LAST_NAME,
Name_Address.ADDRESS_1,
Name_Address.CITY,
Name_Address.STATE_PROVINCE,
Name_Address.ZIP,
Name_Address.PREFERRED_MAIL,
vcsiboCurrentCommittee.current_flag,
CONVERT(VARCHAR(10), Name.BIRTH_DATE, 101) AS BirthDate,
STUFF(
(SELECT
CAST(',' AS varchar(max)) + vcsiboCurrentCommittee.Position
FROM vcsiboCurrentCommittee AS vcc
WHERE vcc.ID = Name.ID
ORDER BY vcc.Position
FOR xml path('')
), 1, 1, '') AS Positions,
vcsiboCurrentCommittee.Position
FROM Name INNER JOIN
Name_Address ON Name.ID = Name_Address.ID INNER JOIN
vcsiboCurrentCommittee ON Name.ID = vcsiboCurrentCommittee.ID
WHERE (Name_Address.PREFERRED_MAIL = 1.00) AND
(vcsiboCurrentCommittee.current_flag = 1.00)
AND (Name.ID <> 10) AND (Name.BIRTH_DATE <> '')
AND (vcsiboCurrentCommittee.CommitteeCode IN ('ALUMNAE_DEPT', 'COLLEGIATE-DEPT', 'EDUCATION', 'FINANCIAL', 'INTERN_COUNCIL', 'IPDEPT', 'MEMBERSHIP', 'PANHELLENIC', 'REG_1', 'REG_2', 'REG_3', 'REG_4', 'REG_5', 'REG_6', 'REG_7', 'REG_8'))
创造了这个输出:
ID FIRST_NAME current_flag Positions Position
---------- -------------------- ------------ ---------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------- ---------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------
106379 Christine 1 NOMAL,NOMAL,NOMAL,NOMAL,NOMAL,NOMAL,NOMAL,NOMAL,NOMAL,NOMAL NOMAL
106379 Christine 1 SFC,SFC,SFC,SFC,SFC,SFC,SFC,SFC,SFC,SFC SFC
106418 Mary 1 CVP,CVP,CVP,CVP,CVP,CVP,CVP,CVP,CVP,CVP,CVP,CVP,CVP CVP
106751 Denise 1 SDFA,SDFA,SDFA,SDFA,SDFA,SDFA,SDFA,SDFA,SDFA,SDFA,SDFA
提前致谢。 SQL Server 2012
答案 0 :(得分:2)
我认为您需要修复子查询中的别名,并且可能需要select distinct
:
STUFF((SELECT DISTINCT CAST(',' AS varchar(max)) + vcc.Position
---------------------------------------------------^
FROM vcsiboCurrentCommittee AS vcc
WHERE vcc.ID = Name.ID
ORDER BY vcc.Position
FOR xml path('')
), 1, 1, '') AS Positions,
修复别名后(因此Position
引用内部表而不是外部表),可能不需要DISTINCT
。如果您使用DISTINCT
,则可能还需要调整ORDER BY
。
编辑:
外部查询中的重复项由外部查询中的逻辑创建。您可能不希望在子查询中使用相同的表。要说出你真正想要的逻辑是很难的。在将vcsiboCurrentCommittee
加入其他表之前,可能会对def update
@newevent = Event.find(params[:id])
if @newevent.update_attributes(params[:newevent])
redirect_to @user
else
render action: "edit"
end
end
进行某种聚合。