我如何从SQL表中检索数据

时间:2014-12-04 10:56:58

标签: sql sql-server select sum pivot

这是我的数据表tbl_counter

refId       refMid      dateTimeStmp             volCtr1     volCtr2     volCtr3     volCtr4
----------- ----------- -----------------------  ----------- ----------- ----------- -----------
3           DEVICE1     2014-03-01 00:00:00.000  100         200         300         400
4           DEVICE1     2014-03-02 00:00:00.000  100         200         300         400
5           DEVICE1     2014-03-03 00:00:00.000  100         200         300         400
6           DEVICE2     2014-03-02 00:00:00.000  100         200         300         400
7           DEVICE2     2014-03-03 00:00:00.000  100         200         300         400
8           DEVICE3     2014-03-03 00:00:00.000  100         200         300         400
9           DEVICE3     2014-03-01 00:00:00.000  100         200         300         400
10          DEVICE5     2014-03-01 00:00:00.000  100         200         300         400
11          DEVICE5     2014-03-02 00:00:00.000  100         200         300         400
12          DEVICE5     2014-03-03 00:00:00.000  100         200         300         400
13          DEVICE5     2014-03-04 00:00:00.000  100         200         300         400
14          DEVICE6     2014-03-04 00:00:00.000  100         200         300         400
15          DEVICE7     2014-03-02 00:00:00.000  100         200         300         400
16          DEVICE8     2014-03-01 00:00:00.000  100         200         300         400
17          DEVICE8     2014-03-02 00:00:00.000  100         200         300         400
18          DEVICE8     2014-03-03 00:00:00.000  100         200         300         400
19          DEVICE8     2014-03-04 00:00:00.000  100         200         300         400

我需要的是数据采用以下格式

DATE        DEVICE1 DEVICE2 DEVICE3 DEVICE4 DEVICE5 DEVICE6 DEVICE7 DEVICE8
01-03-2014  1000            1000            1000                    1000
02-03-2014  1000    1000                    1000            1000    1000
03-03-2014  1000    1000    1000            1000                    1000
04-03-2014                                  1000    1000            1000

(结果表DEVICE * n colomn包含volCtr1 + volCtr2 + volCtr3 + volCtr4) 这可以通过为每个DEVICE创建临时表并使用连接来完成,但我需要更好的方法(如果有的话)。

1 个答案:

答案 0 :(得分:1)

试试这个:

SELECT dateTimeStmp, DEVICE1, DEVICE2, DEVICE3, DEVICE4, DEVICE5, DEVICE6, DEVICE7, DEVICE8 
FROM (SELECT CONVERT(DATE, c.dateTimeStmp) AS dateTimeStmp, 
            ('DEVICE' + CONVERT(VARCHAR(100), c.refMid)) AS ColHeader, 
            (c.volCtr1 + c.volCtr2 + c.volCtr3) DEVICEDATA 
        FROM tbl_counter c
     ) AS A 
PIVOT(SUM(DEVICEDATA) FOR ColHeader IN (DEVICE1, DEVICE2, DEVICE3, DEVICE4, DEVICE5, DEVICE6, DEVICE7, DEVICE8)) AS P

SELECT CONVERT(DATE, c.dateTimeStmp) AS Date, 
       SUM(CASE WHEN c.refMid = 1 THEN c.volCtr1 + c.volCtr2 + c.volCtr3 ELSE 0 END) AS DEVICE1, 
       SUM(CASE WHEN c.refMid = 2 THEN c.volCtr1 + c.volCtr2 + c.volCtr3 ELSE 0 END) AS DEVICE2, 
       SUM(CASE WHEN c.refMid = 3 THEN c.volCtr1 + c.volCtr2 + c.volCtr3 ELSE 0 END) AS DEVICE3, 
       SUM(CASE WHEN c.refMid = 4 THEN c.volCtr1 + c.volCtr2 + c.volCtr3 ELSE 0 END) AS DEVICE4, 
       SUM(CASE WHEN c.refMid = 5 THEN c.volCtr1 + c.volCtr2 + c.volCtr3 ELSE 0 END) AS DEVICE5, 
       SUM(CASE WHEN c.refMid = 6 THEN c.volCtr1 + c.volCtr2 + c.volCtr3 ELSE 0 END) AS DEVICE6, 
       SUM(CASE WHEN c.refMid = 7 THEN c.volCtr1 + c.volCtr2 + c.volCtr3 ELSE 0 END) AS DEVICE7, 
       SUM(CASE WHEN c.refMid = 8 THEN c.volCtr1 + c.volCtr2 + c.volCtr3 ELSE 0 END) AS DEVICE8
FROM tbl_counter c
GROUP BY CONVERT(DATE, c.dateTimeStmp);