Click here to monitor SSC
SQLServerCentral is supported by Redgate
 
Log in  ::  Register  ::  Not logged in
 
 
 


Complex data to join rows from the same table


Complex data to join rows from the same table

Author
Message
pjcafonsosemefeito
pjcafonsosemefeito
Grasshopper
Grasshopper (14 reputation)Grasshopper (14 reputation)Grasshopper (14 reputation)Grasshopper (14 reputation)Grasshopper (14 reputation)Grasshopper (14 reputation)Grasshopper (14 reputation)Grasshopper (14 reputation)

Group: General Forum Members
Points: 14 Visits: 42
Hi All

I had a huge situations and at the moment only revolve by VB and XML, but is not a fast and easy way to force my users to do.

Real world I receive data from several and diverse equipment and each of one insert on central table the following information.

Date/Time Insert Tag Device Value
------------------- ---------- ----------- ----------
03-28-2013 09:05 1305 125 1251,69
03-28-2013 09:05 1305 126 1261,69
03-28-2013 09:05 1305 127 1271,69
03-28-2013 09:05 1305 128 1281,69
03-28-2013 09:05 1305 129 1291,69
03-28-2013 09:05 1305 130 1301,69
03-28-2013 09:15 1305 125 1252,69
03-28-2013 09:15 1305 126 1262,69
03-28-2013 09:15 1305 127 1272,69
03-28-2013 09:15 1305 128 1282,69
03-28-2013 09:15 1305 129 1292,69
03-28-2013 09:15 1305 130 1302,69

I try several Joing {Inner / All / Out} but not with no sucess., using also a temporary table , but with VB

I need a single output from data above decribe like this:
Data/Time D125 D126 D127 D128 D129 D130
------------------ ---------- ---------- ---------- ---------- ---------- ------------
03-28-2013 09:00 1251,69 1261,69 1271,69 1281,69 1291,69 1301,69
03-28-2013 09:15 1252,69 1262,69 1272,69 1282,69 1292,69 1302,69


I now is not so easy ask, but I thought I can bring this query directly from SQL Server.

Any one can give some ideia to do this.

Thanks

Paulo Afonso
Cool
Koen Verbeeck
Koen Verbeeck
SSCoach
SSCoach (16K reputation)SSCoach (16K reputation)SSCoach (16K reputation)SSCoach (16K reputation)SSCoach (16K reputation)SSCoach (16K reputation)SSCoach (16K reputation)SSCoach (16K reputation)

Group: General Forum Members
Points: 16465 Visits: 13207
If I'm not mistaken you want a row for every date/time combination and put the values in different columns?
This would be done with a PIVOT statement.

Using PIVOT and UNPIVOT



How to post forum questions.
Need an answer? No, you need a question.
What’s the deal with Excel & SSIS?

Member of LinkedIn. My blog at SQLKover.

MCSA SQL Server 2012 - MCSE Business Intelligence
pjcafonsosemefeito
pjcafonsosemefeito
Grasshopper
Grasshopper (14 reputation)Grasshopper (14 reputation)Grasshopper (14 reputation)Grasshopper (14 reputation)Grasshopper (14 reputation)Grasshopper (14 reputation)Grasshopper (14 reputation)Grasshopper (14 reputation)

Group: General Forum Members
Points: 14 Visits: 42
Koen

When we go in one direction, forgot completly other.

Pivot/Unpivot was so obsvious I forgot of this statment.

Thanks for call me for wake up.

For others who need to read examples in easy way, please go to:
[url=http://blog.sqlauthority.com/2008/06/07/sql-server-pivot-and-unpivot-table-examples/][/url]


Paulo
Cool
pjcafonsosemefeito
pjcafonsosemefeito
Grasshopper
Grasshopper (14 reputation)Grasshopper (14 reputation)Grasshopper (14 reputation)Grasshopper (14 reputation)Grasshopper (14 reputation)Grasshopper (14 reputation)Grasshopper (14 reputation)Grasshopper (14 reputation)

Group: General Forum Members
Points: 14 Visits: 42
Koen Verbeeck (3/29/2013)
If I'm not mistaken you want a row for every date/time combination and put the values in different columns?
This would be done with a PIVOT statement.

Using PIVOT and UNPIVOT
:-D
Go


Permissions

You can't post new topics.
You can't post topic replies.
You can't post new polls.
You can't post replies to polls.
You can't edit your own topics.
You can't delete your own topics.
You can't edit other topics.
You can't delete other topics.
You can't edit your own posts.
You can't edit other posts.
You can't delete your own posts.
You can't delete other posts.
You can't post events.
You can't edit your own events.
You can't edit other events.
You can't delete your own events.
You can't delete other events.
You can't send private messages.
You can't send emails.
You can read topics.
You can't vote in polls.
You can't upload attachments.
You can download attachments.
You can't post HTML code.
You can't edit HTML code.
You can't post IFCode.
You can't post JavaScript.
You can post emoticons.
You can't post or upload images.

Select a forum

































































































































































SQLServerCentral


Search