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 )
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. ** Edit Ends **
we have upto 4 Meters to record the values, DataID is not important. However I want all Meter data to be aligned, the above query that I wrote does it's work partially, i.e. 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; once per meter used in application. i.e. 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. thanks.
Max(LAValue)rather than just selecting all values ofLAValue. Can you add more data to your example that would yield the repetition you are looking for? You state that you want the values for "every meter used" but I only see one value for meter in your sample data set.