Please start any new threads on our new site at https://forums.sqlteam.com. We've got lots of great SQL Server experts to answer whatever question you can come up with.

 All Forums
 General SQL Server Forums
 New to SQL Server Programming
 Help sorting data

Author  Topic 

ab2121
Starting Member

3 Posts

Posted - 2012-07-05 : 10:08:53
I would like to take data in a table formatted like:

DateStamp | Reading | Channel |
01-06-2012 | 1.0 | 111 |
01-06-2012 | 2.0 | 111 |
01-06-2012 | 3.0 | 222 |
01-06-2012 | 4.0 | 222 |

And use a query to format it like this:

DateStamp | Reading | Channel | DateStamp | Reading | Channel |
01-06-2012 | 1.0 | 111 | 01-06-2012 | 3.0 | 222 |
01-06-2012 | 2.0 | 111 | 01-06-2012 | 4.0 | 222 |

As in the grouping of columns is distinguished by channel number.

Is there a query that can do this for me?

visakh16
Very Important crosS Applying yaK Herder

52326 Posts

Posted - 2012-07-05 : 10:53:00
so what if there are more than 2 channels available? you want them to appear 2 per row? ie like


SELECT MAX(CASE WHEN Rn=1 THEN DateStamp END) AS DateStamp1,
MAX(CASE WHEN Rn=1 THEN Reading END) AS Reading1,
MAX(CASE WHEN Rn=1 THEN Channel END) AS Channel1,
MAX(CASE WHEN Rn=2 THEN DateStamp END) AS DateStamp2,
MAX(CASE WHEN Rn=2 THEN Reading END) AS Reading2,
MAX(CASE WHEN Rn=2 THEN Channel END) AS Channel2
FROM
(
SELECT ROW_NUMBER() OVER (PARTITION BY Grp ORDER BY Channel) AS Rn,*
FROM
(
SELECT DENSE_RANK() OVER(PARTITION BY Channel ORDER BY Reading) AS Grp,*
FROM table
)t
)r
GROUP BY Grp


------------------------------------------------------------------------------------------------------
SQL Server MVP
http://visakhm.blogspot.com/

Go to Top of Page

X002548
Not Just a Number

15586 Posts

Posted - 2012-07-05 : 10:56:04
What if you had n number?
Go to Top of Page

ab2121
Starting Member

3 Posts

Posted - 2012-07-05 : 11:29:12
For my case the database has a very large number of rows but still only 3 columns
would it be easier (or possible) to format it like this :

Datestamp | Reading (from channel 111) | Reading (from Channel 121)| Reading (from Channel 131)|... etc

The maximum number of channels chosen would be 5
Go to Top of Page

visakh16
Very Important crosS Applying yaK Herder

52326 Posts

Posted - 2012-07-05 : 11:39:26
so is the channel number always fixed(111,121 etc?

------------------------------------------------------------------------------------------------------
SQL Server MVP
http://visakhm.blogspot.com/

Go to Top of Page

TG
Master Smack Fu Yak Hacker

6065 Posts

Posted - 2012-07-05 : 11:39:29
In that case you could do it with PIVOT:

with yourTable (DateStamp , Reading , Channel) as (
select '01-06-2012' , 1.0 , 111 union all
select '01-06-2012' , 2.0 , 111 union all
select '01-06-2012' , 3.0 , 222 union all
select '01-06-2012' , 4.0 , 222 )


select dateStamp
,[111]
,[222]
from (
select DateStamp
,Reading
,Channel
,row_number() over (partition by channel order by reading) rn
from yourTable
) d
pivot (max(Reading) for channel in ([111],[222])) p

OUTPUT:
dateStamp 111 222
---------- --------------------------------------- ---------------------------------------
01-06-2012 1.0 3.0
01-06-2012 2.0 4.0



Be One with the Optimizer
TG
Go to Top of Page

visakh16
Very Important crosS Applying yaK Herder

52326 Posts

Posted - 2012-07-05 : 11:40:33
quote:
Originally posted by ab2121

For my case the database has a very large number of rows but still only 3 columns
would it be easier (or possible) to format it like this :

Datestamp | Reading (from channel 111) | Reading (from Channel 121)| Reading (from Channel 131)|... etc

The maximum number of channels chosen would be 5


you should be able to extend my earlier suggestion for getting this output

------------------------------------------------------------------------------------------------------
SQL Server MVP
http://visakhm.blogspot.com/

Go to Top of Page

ab2121
Starting Member

3 Posts

Posted - 2012-07-05 : 11:57:05
Yes the channel numbers chosen will always be fixed

Perfect works great! thanks!
Go to Top of Page

visakh16
Very Important crosS Applying yaK Herder

52326 Posts

Posted - 2012-07-05 : 12:02:22
welcome

------------------------------------------------------------------------------------------------------
SQL Server MVP
http://visakhm.blogspot.com/

Go to Top of Page
   

- Advertisement -