2016-05-27 157 views
0

我試圖在SQL Server中獲得分隔列值的逗號,選擇逗號分隔列值

Select 
CT.ClaimTypeId, CT.ClaimTypeName,CT.MHWSchemeNo,CT.MHWClaimTypeID,CFCTR.FormId,CF.Name, 
FF.FormId,FF.FieldId,FF.Label,FF.Name,FF.IsDefaultField,FF.IsFieldLabelVisible,FF.IsRequired,FF.Type, 

Label = STUFF((
      SELECT ',' +JFFO.Label 
      FROM dbo.FormFieldOption JFFO 
      where JFFO.FieldId = FF.FieldId 
      FOR XML PATH(''), TYPE).value('.', 'NVARCHAR(MAX)'), 1, 1, ''), 
Value= STUFF((
      SELECT ',' +JFFO.Value 
      FROM dbo.FormFieldOption JFFO 
      where JFFO.FieldId = FF.FieldId 
      FOR XML PATH(''), TYPE).value('.', 'NVARCHAR(MAX)'), 1, 1, ''), 
AdditionalTextfieldName= STUFF((
      SELECT ',' +JFFO.AdditionalTextfieldName 
      FROM dbo.FormFieldOption JFFO 
      where JFFO.FieldId = FF.FieldId 
      FOR XML PATH(''), TYPE).value('.', 'NVARCHAR(MAX)'), 1, 1, '') 

from EmployeeClaimTypeRelationship ECR 
LEFT JOIN ClaimType CT on CT.MHWSchemeNo=ECR.MHWSchemeNo 
LEFT JOIN ClaimFormClaimTypeRelationship CFCTR on CFCTR.ClaimTypeId=CT.ClaimTypeId 
LEFT JOIN ClaimForm CF on CF.FormId=CFCTR.FormId 
LEFT JOIN FormField FF on FF.FormId=CFCTR.FormId 
LEFT JOIN FormFieldOption FFO on FFO.FieldId=FF.FieldId 
where ECR.MHWUserId='CITITEST1' 
group by FF.FieldId,CT.ClaimTypeId, CT.ClaimTypeName,CT.MHWSchemeNo,CT.MHWClaimTypeID,CFCTR.FormId,CF.Name, 
FF.FormId,FF.FieldId,FF.Label,FF.Name,FF.IsDefaultField,FF.IsFieldLabelVisible,FF.IsRequired,FF.Type 

這是我的查詢。它給了我正確的結果。我在這裏使用group by條款。

現在我想優化這個查詢。

正如你在上面看到的,我有3列有逗號,分隔值來自同一個表。

有什麼辦法讓我不必爲不同的字段寫多個時間相同的查詢嗎?

像這樣

Select 
CT.ClaimTypeId, CT.ClaimTypeName,CT.MHWSchemeNo,CT.MHWClaimTypeID,CFCTR.FormId,CF.Name, 
FF.FormId,FF.FieldId,FF.Label,FF.Name,FF.IsDefaultField,FF.IsFieldLabelVisible,FF.IsRequired,FF.Type, 

Label = STUFF((
      SELECT ',' +JFFO.Label 
      FROM dbo.FormFieldOption JFFO 
      where JFFO.FieldId = FF.FieldId 
      FOR XML PATH(''), TYPE).value('.', 'NVARCHAR(MAX)'), 1, 1, ''), 
Value,AdditionalTextfieldName = STUFF((
      SELECT ',' +JFFO.Value as Value,','+JFFO.AdditionalTextfieldName as AdditionalTextfieldName 
      FROM dbo.FormFieldOption JFFO 
      where JFFO.FieldId = FF.FieldId 
      FOR XML PATH(''), TYPE).value('.', 'NVARCHAR(MAX)'), 1, 1, '') 

from EmployeeClaimTypeRelationship ECR 
LEFT JOIN ClaimType CT on CT.MHWSchemeNo=ECR.MHWSchemeNo 
LEFT JOIN ClaimFormClaimTypeRelationship CFCTR on CFCTR.ClaimTypeId=CT.ClaimTypeId 
LEFT JOIN ClaimForm CF on CF.FormId=CFCTR.FormId 
LEFT JOIN FormField FF on FF.FormId=CFCTR.FormId 
LEFT JOIN FormFieldOption FFO on FFO.FieldId=FF.FieldId 
where ECR.MHWUserId='CITITEST1' 
group by FF.FieldId,CT.ClaimTypeId, CT.ClaimTypeName,CT.MHWSchemeNo,CT.MHWClaimTypeID,CFCTR.FormId,CF.Name, 
FF.FormId,FF.FieldId,FF.Label,FF.Name,FF.IsDefaultField,FF.IsFieldLabelVisible,FF.IsRequired,FF.Type 
+0

您能否突出或指出區別? –

+0

@TimBiegeleisen:我不想再次使用Stuff。請參閱第二個查詢我正在使用'Value,AdditionalTextfieldName' –

回答

1

你可以有一個視圖。

CREATE VIEW view_name AS 
SELECT * FROM ... WHERE ... 

(而不是*,你可能更願意選擇所有列的你曾經可能需要和可能給他們的別名工會。)然後你只需要

SELECT column1, column2, column3 
FROM view_name 
WHERE [additional conditions] 
1

我將創建一個UDF表值函數和調用,它也會給你一些性能上的好處

功能定義

CREATE FUNCTION dbo.fn_FormFieldOptionList (@FieldId INT) 
RETURNS TABLE 
AS 
RETURN (
SELECT DISTINCT 
    Label = STUFF((
       SELECT ',' +JFFO.Label 
       FROM dbo.FormFieldOption JFFO 
       where JFFO.FieldId = FF.FieldId 
       FOR XML PATH(''), TYPE).value('.', 'NVARCHAR(MAX)'), 1, 1, '') 
    ,Value= STUFF((
       SELECT ',' +JFFO.Value 
       FROM dbo.FormFieldOption JFFO 
       where JFFO.FieldId = FF.FieldId 
       FOR XML PATH(''), TYPE).value('.', 'NVARCHAR(MAX)'), 1, 1, '') 
    ,AdditionalTextfieldName= STUFF((
       SELECT ',' +JFFO.AdditionalTextfieldName 
       FROM dbo.FormFieldOption JFFO 
       where JFFO.FieldId = FF.FieldId 
       FOR XML PATH(''), TYPE).value('.', 'NVARCHAR(MAX)'), 1, 1, '') 

FROM dbo.FormFieldOption FF 
WHERE FF.FieldId = @FieldId) 

您的查詢

Select 
CT.ClaimTypeId, CT.ClaimTypeName,CT.MHWSchemeNo 
,CT.MHWClaimTypeID,CFCTR.FormId,CF.Name 
,FF.FormId,FF.FieldId,FF.Label,FF.Name 
,FF.IsDefaultField,FF.IsFieldLabelVisible 
,FF.IsRequired,FF.Type,f.Label. f.Value ,f.AdditionalTextfieldName 

from EmployeeClaimTypeRelationship ECR 
LEFT JOIN ClaimType CT on CT.MHWSchemeNo=ECR.MHWSchemeNo 
LEFT JOIN ClaimFormClaimTypeRelationship CFCTR on CFCTR.ClaimTypeId=CT.ClaimTypeId 
LEFT JOIN ClaimForm CF on CF.FormId=CFCTR.FormId 
LEFT JOIN FormField FF on FF.FormId=CFCTR.FormId 
OUTER APPLY dbo.fn_FormFieldOptionList(FF.FieldId) f 
where ECR.MHWUserId='CITITEST1' 
0

在這裏,在這個查詢中,我們要加入dbo.FormFieldOption兩次。如果我們可以重新構造查詢,則可以實現良好的優化&速度。嘗試重組 ,將表dbo.FormFieldOption改爲單個。

+0

我該怎麼做。 –