SQL子查询/自加入

问题描述:

这是一个自动呼叫反馈数据库,存储客户对每个问题的反馈。SQL子查询/自加入

我使用SQL Server 2012中我在表名以下数据[NPS_Feedback]

CLI   CallerID Customer_Account Question Feedback  Date 
34622968 F22141B854 400004775250  Q1   Satisfie  2016-03-25 
34622968 F22141B854 400004775250  Q2   Not Satisfied 2016-03-25 
34622968 F22141B854 400004775250  Q3   Not Satisfied 2016-03-25 
30227453 GED903EDL 400001913180  Q1   Not Satisfied 2016-03-25 
30227453 GED903EDL 400001913180  Q2   Satisfied  2016-03-25 
30227453 GED903EDL 400001913180  Q3   Not Satisfied 2016-03-25 
34622968 DAED19FDE 400004775250  Q1   Satisfied  2016-03-25 
34622968 DAED19FDE 400004775250  Q2   Satisfied  2016-03-25 
34622968 DAED19FDE 400004775250  Q3   Satisfied  2016-03-25 

请帮我用下面的愿望输出使用SQLstored程序报告

CLI  CallerID  Customer_Account Q1    Q2    Q3    Date 
34622968 F22141B854 400004775250  Satisfied  Not-Satisfied Not-Satisfied 2016-03-25 
30227453 GED903EDL 400001913180  Not-Satisfied Satisfied  Not-Satisfied 2016-03-25 
34622968 DAED19FDE 400004775250  Satisfied  Satisfied  Satisfied  2016-03-25 

请注意:

对于每个呼叫,来电显示都是唯一的。

+0

退房PIVOT操作 – Squirrel

+0

使用轴。是Q1,Q2,Q3修复还是可以有更多的反馈类型。 – KumarHarsh

除了使用PIVOT命令,你也可以使用有条件聚集:

SELECT 
    CLI, 
    CallerID, 
    Customer_Account, 
    Q1 = MAX(CASE WHEN Question = 'Q1' THEN Feedback END), 
    Q2 = MAX(CASE WHEN Question = 'Q2' THEN Feedback END), 
    Q3 = MAX(CASE WHEN Question = 'Q3' THEN Feedback END), 
    Date 
FROM NPS_Feedback 
GROUP BY 
    CLI, CallerID, Customer_Account, Date 

ONLINE DEMO

+0

先生,我需要所有的人在一条线上......! – BilalAhmed

+0

@BilalAhmed,我的答案有什么问题?检查演示。 –

+1

谢谢先生,这是我的错误,我有DateTime和时间是不同的,这就是为什么它分别显示每条记录。标记为答案...! – BilalAhmed

一个简单的PIVOT查询将起作用。

select CLI,CallerID,Customer_Account, [Q1],[Q2],[Q3], Date 
from 
(
    select 
    CLI,CallerID,Customer_Account,Question,Feedback,Date 
    from [NPS_Feedback] 
)s 
pivot 
(
    max(Feedback) for Question in ([Q1],[Q2],[Q3]) 
) p 
+0

请仔细阅读说明('p'附近的语法错误) – BilalAhmed

+0

亲爱的,它显示不同的行中的每个记录..请指导我显示屏幕截图。有没有可能在这里附上截图? – BilalAhmed

+0

@BilalAhmed这不可能发生。你一定错过了什么 – DhruvJoshi