简体   繁体   English

使用SQL Server PIVOTing数据

[英]PIVOTing data with SQL server

Okay, I have hit the bull's eye when I even didn't know how to fire a gun. 好的,当我什至不知道如何开枪时,我都望而却步。 I wrote this SQL based on Technet basic article ( http://technet.microsoft.com/en-us/library/ms177410(v=sql.105).aspx ) 我根据Technet基本文章( http://technet.microsoft.com/zh-cn/library/ms177410(v=sql.105).aspx )编写了此SQL。

Select LogTime, Location, LA95Day, LA95Evening, LA95DEn, LA95Night from 
(Select LogTime, Location, LAValue, ValueType  from Project_5_CalculatedData 
     where ValueType in ('LA95Day','LA95Evening','LA95DEn','LA95Night')  )  as c
PIVOT(
Max(LAValue) For ValueType in (LA95Day,LA95Evening,LA95DEn,LA95Night)) as PVt

My Table structure is something like 我的表结构类似于

DataID  Location    LogTime LAValue ValueType
10117   Meter1  2012-09-25 00:00:00.000 71.12   LA95Day
10118   Meter1  2012-09-25 00:00:00.000 55.52   LA95Evening
10119   Meter1  2012-09-25 00:00:00.000 52.69   LA95Night
10120   Meter1  2012-09-25 00:00:00.000 68.62   LA95Den
10121   Meter1  2012-09-26 00:00:00.000 72.21   LA95Day
10122   Meter1  2012-09-26 00:00:00.000 54.10   LA95Evening
10123   Meter1  2012-09-26 00:00:00.000 48.30   LA95Night
10124   Meter1  2012-09-26 00:00:00.000 69.38   LA95Den

EDIT : Data is incomplete here.. we have 4000 rows .. so you can assume MEter2, Meter3, Meter4 location will exists with same or different days as in sample data. 编辑 :这里的数据不完整..我们有4000行..因此,您可以假设MEter2,Meter3,Meter4的位置将与示例数据中的日期相同或不同。 ** Edit Ends ** **编辑结束**

we have upto 4 Meters to record the values, DataID is not important. 我们最多可以记录4米,数据ID并不重要。 However I want all Meter data to be aligned, the above query that I wrote does it's work partially, ie it shows data as : 但是我希望所有仪表数据都对齐,我编写的上述查询确实部分起作用,即它显示数据为:

LogTime Location    LA95Day LA95Evening LA95DEn LA95Night
2012-09-25 00:00:00.000 Meter1  71.12   55.52   68.62   52.69
2012-09-26 00:00:00.000 Meter1  72.21   54.10   69.38   48.30
2012-09-27 00:00:00.000 Meter1  68.88   47.77   66.05   46.02
2012-09-28 00:00:00.000 Meter1  73.52   49.23   70.56   43.60
2012-09-29 00:00:00.000 Meter1  54.09   44.87   52.85   41.64
2012-09-30 00:00:00.000 Meter1  51.46   48.61   51.94   41.60
2012-10-01 00:00:00.000 Meter1  73.09   51.78   70.17   46.20

But I want that based on meter used; 但是我要根据使用的表计; LAday, evening, Den and night will get repeated up to 4 time; LAday,晚上,Den和晚上最多重复4次; once per meter used in application. 应用中每米使用一次。 ie desire output [columns] needs to be 即欲望输出[列]需要是

LogTime M1-LA95Day  M1-LA95Evening  M1-LA95DEn  M1-LA95Night M2-LA95Day M2-LA95Evening  M2-LA95DEn  M2-LA95Night M3-LA95Day M3-LA95Evening  M3-LA95DEn  M3-LA95Night M4-LA95Day M4-LA95Evening  M4-LA95DEn  M4-LA95Night

Oh, I am trying via hit and trial as I never use Pivot before and doesn't know even how my above query works, if you can just explain that query will help me do next step. 哦,我正在尝试尝试,因为我以前从未使用过Pivot,甚至不知道我上面的查询是如何工作的,如果您能解释一下该查询将帮助我进行下一步的话。 thanks. 谢谢。

It seems to me that you are trying to list all meters along with their valueTypes as columns. 在我看来,您正在尝试将所有仪表及其valueTypes列为列。 If that is the case, then you should be able to use the following: 如果是这种情况,那么您应该可以使用以下内容:

Select LogTime, 
    Meter1_LA95Day, Meter1_LA95Evening, Meter1_LA95DEn, Meter1_LA95Night,
    Meter2_LA95Day, Meter2_LA95Evening, Meter2_LA95DEn, Meter2_LA95Night,
    Meter3_LA95Day, Meter3_LA95Evening, Meter3_LA95DEn, Meter3_LA95Night,
    Meter4_LA95Day, Meter4_LA95Evening, Meter4_LA95DEn, Meter4_LA95Night 
from 
(
    Select LogTime, 
        ValueType = Location+'_'+ValueType,
        LAValue       
    from Project_5_CalculatedData 
    where ValueType in ('LA95Day','LA95Evening','LA95DEn','LA95Night')  
)  as c
PIVOT
(
    Max(LAValue) 
    For ValueType in (Meter1_LA95Day, Meter1_LA95Evening, Meter1_LA95DEn, Meter1_LA95Night,
                      Meter2_LA95Day, Meter2_LA95Evening, Meter2_LA95DEn, Meter2_LA95Night,
                      Meter3_LA95Day, Meter3_LA95Evening, Meter3_LA95DEn, Meter3_LA95Night,
                      Meter4_LA95Day, Meter4_LA95Evening, Meter4_LA95DEn, Meter4_LA95Night)
) as PVt;

Note, this assumes the meters are actually named meter1 , meter2 , etc. If your meters aren't called meter1 , etc and they have names that might not be the same, then you might want to use dynamic SQL to get the result: 请注意,这假设仪表实际上被命名为meter1meter2等等。如果您的仪表没有被称为meter1等等,并且它们的名称可能不相同,那么您可能想要使用动态SQL来获得结果:

DECLARE @cols AS NVARCHAR(MAX),
    @query  AS NVARCHAR(MAX)

select @cols = STUFF((SELECT distinct ',' + QUOTENAME(Location+'_'+ValueType) 
                    from Project_5_CalculatedData
            FOR XML PATH(''), TYPE
            ).value('.', 'NVARCHAR(MAX)') 
        ,1,1,'')

set @query = 'SELECT LogTime,' + @cols + ' 
            from 
            (
                Select LogTime, 
                    ValueType = Location+''_''+ValueType,
                    LAValue       
                from Project_5_CalculatedData 
                where ValueType in (''LA95Day'',''LA95Evening'',''LA95DEn'',''LA95Night'')  
            ) x
            pivot 
            (
                max(LAValue)
                for ValueType in (' + @cols + ')
            ) p '

execute sp_executesql @query;

These will give a result: 这些将产生结果:

+-------------------------+----------------+----------------+--------------------+------------------+----------------+----------------+--------------------+------------------+----------------+----------------+--------------------+------------------+
|         LogTime         | Meter1_LA95Day | Meter1_LA95Den | Meter1_LA95Evening | Meter1_LA95Night | Meter2_LA95Day | Meter2_LA95Den | Meter2_LA95Evening | Meter2_LA95Night | Meter3_LA95Day | Meter3_LA95Den | Meter3_LA95Evening | Meter3_LA95Night |
+-------------------------+----------------+----------------+--------------------+------------------+----------------+----------------+--------------------+------------------+----------------+----------------+--------------------+------------------+
| 2012-09-25 00:00:00.000 | 71.12          | 68.62          | 55.52              | 52.69            | 71.12          | 68.62          | 55.52              | 52.69            | NULL           | NULL           | NULL               | NULL             |
| 2012-09-26 00:00:00.000 | 72.21          | 69.38          | 54.10              | 48.30            | NULL           | NULL           | NULL               | NULL             | 72.21          | 69.38          | 54.10              | 48.30            |
+-------------------------+----------------+----------------+--------------------+------------------+----------------+----------------+--------------------+------------------+----------------+----------------+--------------------+------------------+

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM