SQL子查询/自我加入

时间:2016-04-26 05:33:16

标签: sql-server subquery self-join

这是一个自动呼叫反馈数据库,可以针对每个问题存储客户反馈。

我正在使用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

请使用SQL存储过程帮助我完成报告的以下愿望输出:

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

请注意:

  

每次通话时,来电显示都是唯一的。

2 个答案:

答案 0 :(得分:1)

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

答案 1 :(得分:0)

简单的PIVOT查询将起作用。

<html>
<head>
<script src="jquery.min.js" type="text/javascript"></script>
<script src="mqttws31.js" type="text/javascript"></script>


    <script>
    function myFunction(p1, p2) {
    return p1 * p2;   
    };
     var mqtt,payload;
     var value = 10;
    var reconnectTimeout = 2000;
    function MQTTconnect() {
    if (typeof path == "undefined") {
        path = '/mqtt';
    }
    mqtt = new Paho.MQTT.Client(
            'broker',
            1883,
             "/mqtt",
            "a:" + "abcdef" + ":" + Date.now()
    );
        var options = {
            timeout: 3,
            useSSL: false,
            cleanSession: true,
            onSuccess: onConnect,
            onFailure: function (message) {
                $('#status').val("Connection failed: " + message.errorMessage + "Retrying");
                setTimeout(MQTTconnect, reconnectTimeout);
            }
        };

        mqtt.onConnectionLost = onConnectionLost;
        mqtt.onMessageArrived = onMessageArrived;


        options.userName = 'user';
        options.password = 'password';


        mqtt.connect(options);

    }

    function onConnect() {
        document.writeln("connected");

        // Connection succeeded; subscribe to our topic
        mqtt.subscribe('iot-2/type/+/id/+/evt/evt1/fmt', {qos: 0});
        //$('#topic').val('iot-2/type/" + "+" + "/id/" + "+" + "/evt/evt1/fmt');

    }

    function onConnectionLost(response) {
        setTimeout(MQTTconnect, reconnectTimeout);
        //$('#status').val("connection lost: " + responseObject.errorMessage + ". Reconnecting");

    };

    function onMessageArrived(message) {

        var topic = message.destinationName;
         payload = message.payloadString;  

        //document.writeln(payload);
        //document.write("\n"); 
        //$('#ws').prepend('<li>' + topic + ' = ' + payload + '</li>');

    };
    </script>


</head>
    <body>
        <?php
        $db = '<script type="text/javascript">document.write(MQTTconnect());</script>';
        $db1 = '<script type="text/javascript">document.write(payload);</script>';
        echo $db;
        echo $db1;      
        ?>
    </body>
</html>