自我加入/案例陈述/交叉或外部申请/枢轴混乱

时间:2015-08-22 14:59:59

标签: sql-server-2012

我为我面临的问题创建了一个sqlfiddle。 http://sqlfiddle.com/#!6/fea87/2 我不确定在下面生成所需结果的最佳编码方式。或者,您可以建议更好的方式来显示结果。有人可以帮忙吗?从问卷调查表中,我们需要选择重复查询的第一个响应。一旦我找到了什么,我会发布我的代码。

CREATE TABLE Data
    ([CustomerID] int, [Name] varchar(50))
;  
INSERT INTO Data
    ([CustomerID], [Name])
VALUES
    (1001, 'Harvey'),
    (2001, 'Mike'),
    (3001, 'Dana')
;
CREATE TABLE Questionnaire
    ([CustomerID] int, [Review] varchar(50),[Response] varchar(50))
;  
INSERT INTO Questionnaire
    ([CustomerID], [Review],[Response])
VALUES
    (1001,'Query1','79'),
    (1001,'Query2','80'),
    (1001,'Query3' ,'90'),
    (1001,'Query1' ,'95'),
    (1001,'Query2' ,'98'),
    (1001,'Query3' ,'60'),
    (2001,'Query1','81'),
    (2001,'Query2','70'),
    (2001,'Query3' ,'61'),
    (2001,'Query1' ,'65'),
    (2001,'Query2' ,'70'),
    (2001,'Query3' ,'52')
;
CREATE TABLE Amenities
    ([CustomerID] int, [Utility] varchar(50))
;  
INSERT INTO Amenities
    ([CustomerID], [Utility])
VALUES
    (1001, 'Breakfast'),
    (1001, 'Lunch'),
    (1001, 'Gym');

期望的结果:

CustomerID   Query1   Query2   Query3   Utility
1001         79       80       90       Breakfast
1001         79       80       90       Lunch
1001         79       80       90       Gym
2001         81       70       61       Null
3001         Null     Null     Null     Null

这段代码对我有用,但如果您能想到比这更简单的事情,请告诉我。

select a.CustomerID,ab.Response,cd.Response,ef.Response,d.Utility
from Data a
outer apply (select top 1 b.Response
             from Questionnaire b
             where a.CustomerID=b.CustomerID
             and b.Review='Query1') as ab

outer apply (select top 1 c.Response
             from Questionnaire c
             where a.CustomerID=c.CustomerID
             and c.Review='Query2') as cd

outer apply (select top 1 d.Response
             from Questionnaire d
             where a.CustomerID=d.CustomerID
             and d.Review='Query3') as ef
left join Amenities d
on a.CustomerID=d.CustomerID

1 个答案:

答案 0 :(得分:0)

我要做的第一件事就是将问卷表改为:

CREATE TABLE Questionnaire( QuestionnaireID INT IDENTITY(1, 1)
                                              NOT NULL,
                           CustomerID     INT NULL,
                           Review         VARCHAR(50) NULL,
                           Response       VARCHAR(50) NULL )
ON [PRIMARY];

然后你可以用以下代码获取代码:

WITH x
         AS ( SELECT CustomerID,
                     Query1,
                     Query2,
                     Query3
              FROM( 
                    SELECT CustomerID,
                           Review,
                           Response,
                           ROW_NUMBER() OVER(PARTITION BY CustomerID,
                                                          Review 
                                                       ORDER BY QuestionnaireID) AS rowID
                    FROM Questionnaire ) AS sourceTable 
                PIVOT( MAX(Response) FOR Review IN( Query1,
                                                        Query2,
                                                        Query3 )) AS pivotTable
              WHERE rowid = 1 )
     SELECT d.CustomerID,
            Query1,
            Query2,
            Query3,
            a.Utility
     FROM x
          LEFT JOIN Amenities a ON x.CustomerID = a.CustomerID
          RIGHT JOIN Data d ON x.CustomerID = d.CustomerID;

一般来说,在表格中使用唯一索引是一种好习惯,因为它可以促进很多事情。

这给了我:

CustomerID  Query1  Query2  Query3  Utility
1001    79  80  90  Breakfast
1001    79  80  90  Lunch
1001    79  80  90  Gym
2001    81  70  61  NULL
3001    NULL    NULL    NULL    NULL