Convert Coloumns to rows


Hi all,

I've got a table with the following data: (sample data)
Tbl_MachineHrs
ASSETIDDATEHR23OPR23HR06OPR06HR07OPR07HR14OPR14HR15OPR15HR18OPR18HR19OPR19HR22OPR22
806CD0311/07/20056817.91166824.31166824.91036827.21036827.646



6831.146
806658411/07/20055211.3925218.8925219.11885223.81885224.3107



5227.7107
806655411/07/2005405684061.784062.6454066.3454066.3




4066.3
806654511/07/2005827717828217828236828836828862



829462
806654411/07/2005829710830210830310683091068309113



8315113
806654311/07/20059569.6839575.88395761199581.7119958222



9584.722

The Coloumns represent hours. Eg Hr23 = 23:00, Hr07 = 07:00 (7 AM) Machine hours gets captured at 11pm, 7am, 2pm, 3pm, etc.

OPR = employee / operator number,

How can I write a query where this data can be shown in rows? eg:

ASSETID DATETIME OPR Hrs
8066543 11/07/2005 07:00:00 83 9576
8066543 11/07/2005 14:00:00 10 9582
8066543 11/07/2005 15:00:00 10 9593
806SP801 11/07/2005 23:00:00 99 10503
etc etc. How can I maybe cast or case or summin that to get that results?

Thanks alot in advance

Rudi

[13071 byte] By [Tjoppie] at [2008-2-11]
# 1
Hi
With the enhanced features available in SQL Server 2005. There is no need to use SELECT CASE statements. You can use PIVOT and UNPIVOT for these cases. The following is the syntax:
-- To display in the normal as your out put
SELECT * FROM Tbl_MachineHrs

-- use this to see out as per your wish
SELECT ASSETID,DATE, HRS
FROM
(SELECT ASSETID,DATE,HR23, HR06, HR07 ,HR14 FROM Tbl_MachineHrs) p
UNPIVOT
(HRS FOR Tbl_MachineHrs IN (HR23, HR06, HR07, HR14) )AS unpvt
GO

Output
ASSETID DATE HRS
806CD03 2005-11-07 00:00:00.000 6817.9000
806CD03 2005-11-07 00:00:00.000 6824.3000
806CD03 2005-11-07 00:00:00.000 6824.9000
806CD03 2005-11-07 00:00:00.000 6827.2000
8066584 2005-11-07 00:00:00.000 5211.3000
8066584 2005-11-07 00:00:00.000 5218.8000
8066584 2005-11-07 00:00:00.000 5219.1000
8066584 2005-11-07 00:00:00.000 5223.8000

You can refer to books online for complete information on PIVOT and UNPIVOT.

DBArchitect at 2007-9-9 > top of Msdn Tech,SQL Server,Transact-SQL...
# 2
You can use a query like below:

select m.ASSETID,
DATEADD(hour, CASE c.col
WHEN 1 THEN 11
WHEN 2 THEN 6
END, m.DATE) AS M_DATETIME,
CASE c.col
WHEN 1 THEN m.HR23
WHEN 2 THEN m.HR06
END AS Hrs,
CASE c.col
WHEN 1 THEN m.OPR23
WHEN 2 THEN m.OPR06
END AS Opr
from MachineHrs as m
cross join (
select 1 union all select 2
-- add as many numbers as you have columns to unpivot
) as c(col) ;

Add more expressions in the CASE for the other columns and similarly in the derived table. This can also be done via multiple SELECT statements and UNION ALL but this is a good trick to use.

Note that SQL Server 2005 does have UNPIVOT operator but that is restricted to only one set of columns. So you could unpivot all the HR columns for example but it is kind of complicated to use for your result transformation.

# 3
Hey,

Thanks man, that did the job Big Smile

Tjoppie at 2007-9-9 > top of Msdn Tech,SQL Server,Transact-SQL...

SQL Server

Site Classified