合并查询结果中的记录

时间:2018-03-29 16:34:50

标签: sql sql-server database tsql

在下面的查询中,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条记录。但是,查询返回的NULL CustomFieldIDs不适用于CASE语句。

例如:

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

2 个答案:

答案 0 :(得分:2)

我认为你最需要的是:

module.exports = function (callback, html) {
var jsreport = require('jsreport-core')();

jsreport.init().then(function () {
    return jsreport.render({
        template: {
            content: html,
            engine: 'jsrender',
            recipe: 'phantom-pdf'
        }
    }).then(function (resp) {
        callback(/* error */ null, resp.content.toJSON().data);
    }).catch(function (e) {
        callback(/* error */ e, null);
    });
}).catch(function (e) {
    callback(/* error */ e, null);
});
};

答案 1 :(得分:0)

使用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

编辑:我将问题和CustomFieldData合并到示例中的单个“问题”表中。在您的情况下,您只需要在内部查询(SourceTable)

中进行额外的连接