繁体   English   中英

合并查询结果中的记录

[英]Combining records from query results

在下面我的查询,在数据customfielddata.fielddata保存独立基础上的价值customfielddefinitions.customfieldname

例如:

CustomFieldDefinitions

CustomFieldID  CustomFieldName
------------------------------
1234           Business Owner
5678           Client
9012           Cost Center

CustomFieldData

CustomFieldID  FieldData    Updated
--------------------------------------
1234           barb         1/1/2018
5678           health plan  1/1/2018
9012           68121        1/1/2018

我希望能够提取具有所有FieldData值组合的1条记录。 但是,查询将返回不适用于CASE语句的CustomFieldIDs NULL

例如:

IssueID  Business_Owner  Client       Cost_Center
-------------------------------------------------
176367   NULL            NULL         68121
176367   Barb S          NULL         NULL
176367   NULL            Health Plan  NULL

我知道为什么会这样,但是我不确定是否有解决方案。 我正在使用SSMS。 感谢您对初学者的任何帮助,谢谢!

 SELECT 
     GI.issueid,
     CASE 
        WHEN GCD.customfieldname = 'Business Owner'
            THEN GC1.fielddata
    END AS Business_Owner,
    CASE 
        WHEN GCD.customfieldname = 'Client'
            THEN GC1.fielddata
    END AS Client,
    CASE 
        WHEN GCD.customfieldname = 'Cost Center'
            THEN GC1.fielddata
    END AS Cost_Center,
    GI.closeddate AS Closed
FROM
    dbo.gemini_issues GI
INNER JOIN 
    (SELECT 
         MAX(created) AS Created,
         fielddata, issueid, customfieldid
     FROM 
         gemini_customfielddata
     GROUP BY 
         fielddata, issueid, customfieldid) GC1 ON GI.issueid = GC1.issueid
INNER JOIN 
    dbo.gemini_customfielddefinitions GCD ON GC1.customfieldid = GCD.customfieldid 
                                          AND GCD.customfieldname IN ('cost center', 'business owner', 'client')
WHERE 
    GI.projectid IN (193, 194, 195)
ORDER BY 
    issueid

我认为您只需要在此处设置一个最大值:

 SELECT GI.issueid
    ,max(CASE 
        WHEN GCD.customfieldname = 'Business Owner'
            THEN GC1.fielddata
        END) AS Business_Owner
    ,max(CASE 
        WHEN GCD.customfieldname = 'Client'
            THEN GC1.fielddata
        END) AS Client
    ,max(CASE 
        WHEN GCD.customfieldname = 'Cost Center'
            THEN GC1.fielddata
        END) AS Cost_Center
    ,GI.closeddate AS Closed
FROM dbo.gemini_issues GI
INNER JOIN (
    SELECT max(created) AS Created
        ,fielddata
        ,issueid
        ,customfieldid
    FROM gemini_customfielddata
    GROUP BY fielddata
        ,issueid
        ,customfieldid
    ) GC1 ON GI.issueid = GC1.issueid
INNER JOIN dbo.gemini_customfielddefinitions GCD ON GC1.customfieldid = GCD.customfieldid AND GCD.customfieldname IN ('cost center', 'business owner', 'client')
WHERE GI.projectid IN (193, 194, 195)
group by GI.issueid
ORDER BY GI.issueid

使用PIVOT运算符将行旋转到列是解决此问题的最佳方法,并且在大多数情况下还可以提高性能。 请参阅以下示例:

-- Sample data
CREATE TABLE Issues (
    IssueID int NOT NULL,
    CustomFieldID int NOT NULL,
    FieldData nvarchar(max) NOT NULL,
    Created datetime default getutcdate()
)

INSERT Issues (IssueID, CustomFieldID, FieldData)
VALUES
(176367, 1234, 'barb'),
(176367, 5678, 'health plan'),
(176367, 9012, '68121'),
(176368, 1234, 'don'),
(176368, 5678, 'health plan2'),
(176368, 9012, '12345')


CREATE TABLE CustomFieldDefinitions (
    CustomFieldID int NOT NULL PRIMARY KEY,
    CustomFieldName nvarchar(max) NOT NULL)

INSERT CustomFieldDefinitions VALUES
(1234, 'Business Owner'),
(5678, 'Client'),
(9012, 'Cost Center')


-- Query
SELECT IssueID, [Business Owner], [Client], [Cost Center]
FROM
(
   SELECT i.IssueID, c.CustomFieldName, i.FieldData
   FROM Issues i
   INNER JOIN CustomFieldDefinitions c ON i.CustomFieldID = c.CustomFieldID
   -- add other conditions to filter Issues
) AS SourceTable
PIVOT
(
   MAX(FieldData)
   FOR CustomFieldName IN ([Business Owner], [Client], [Cost Center])
) AS PivotTable;

-- Results
| IssueID | Business Owner |       Client | Cost Center |
|---------|----------------|--------------|-------------|
|  176367 |           barb |  health plan |       68121 |
|  176368 |            don | health plan2 |       12345 |

有关工作示例,请参见此sql小提琴: http : //sqlfiddle.com/#!18/5cd01/4

编辑:我将Issues和CustomFieldData合并到示例中的单个Issues表中。 对于您的情况,您只需要在内部查询(SourceTable)中进行其他联接即可

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM