SQL Server Forums
Profile | Register | Active Topics | Members | Search | Forum FAQ
 
Register Now and get your question answered!
Username:
Password:
Save Password
Forgot your Password?

 All Forums
 General SQL Server Forums
 New to SQL Server Programming
 Split summary to multi rows
 New Topic  Reply to Topic
 Printer Friendly
Author Previous Topic Topic Next Topic  

flamblaster
Constraint Violating Yak Guru

384 Posts

Posted - 10/08/2012 :  14:22:58  Show Profile  Reply with Quote
I'd like to take summary data and split it into multiple rows. I thought of Unpivot, but I'm not sure if that's the right method.

Basically, take the following data:

declare @Ridership table (id int primary key identity not null, BusNum int, RouteNum int, RegularFare int, SeniorFare int)

insert into @Ridership (BusNum, RouteNum, RegularFare, SeniorFare)
values
(50, 1, 3, 2),
(51, 2, 4, 1)

I'd like to to spread out the data in the following manner:

BusNum/RouteNum/FareType
50 /1 /Regular
50 /1 /Regular
50 /1 /Regular
50 /1 /Senior
50 /1 /Senior
51 /2 /Regular
51 /2 /Regular
51 /2 /Regular
51 /2 /Regular
51 /2 /Senior

I'm not sure the best way to get go about this.

Thanks!


jimf
Flowing Fount of Yak Knowledge

USA
2870 Posts

Posted - 10/08/2012 :  15:06:34  Show Profile  Reply with Quote
declare @Ridership table (id int primary key identity not null, BusNum int, RouteNum int, RegularFare int, SeniorFare int)

insert into @Ridership (BusNum, RouteNum, RegularFare, SeniorFare)
values
(50, 1, 3, 2),
(51, 2, 4, 1)



select ride.BusNum,ride.RouteNum,a.Regular
from @Ridership ride
CROSS APPLY (Select 'Regular'
from master..spt_values val
where ride.RegularFare >= val.number
and val.type = 'P' and val.number > 0
) a(regular)

UNION all
select ride.BusNum,ride.RouteNum,b.Senior
from @Ridership ride
CROSS APPLY (Select 'Senior'
from master..spt_values val
where ride.SeniorFare >= val.number
and val.type = 'P' and val.number > 0
) b(Senior)

ORDER BY 1,2

Jim

Everyday I learn something that somebody else already knew
Go to Top of Page

flamblaster
Constraint Violating Yak Guru

384 Posts

Posted - 10/08/2012 :  15:22:04  Show Profile  Reply with Quote
Awesome! Thanks..this is so much simpler than the direction I was heading!
Go to Top of Page

jimf
Flowing Fount of Yak Knowledge

USA
2870 Posts

Posted - 10/08/2012 :  15:45:06  Show Profile  Reply with Quote
Glad I could help!

Jim

Everyday I learn something that somebody else already knew
Go to Top of Page

visakh16
Very Important crosS Applying yaK Herder

India
52325 Posts

Posted - 10/08/2012 :  19:49:25  Show Profile  Reply with Quote
quote:
Originally posted by flamblaster

Awesome! Thanks..this is so much simpler than the direction I was heading!


if you dont want to reky upon system table master..spt_values you can create a number table yourselves and use it like


;With BusRegular
AS
(
SELECT BusNum,RouteNum,RegularFare,'Regular' AS FareType,1 AS Cnt
FROM @RideShip
UNION ALL
SELECT BusNum,RouteNum,RegularFare,FareType,Cnt + 1
FROM BusRegular
WHERE Cnt + 1 <= RegularFare
),
BusSenior
AS
(
SELECT BusNum,RouteNum,SeniorFare,'Senior' AS FareType,1 AS Cnt
FROM @RideShip
UNION ALL
SELECT BusNum,RouteNum,SeniorFare,FareType,Cnt + 1
FROM BusSenior
WHERE Cnt + 1 <= SeniorFare
)

SELECT BusNum,RouteNum,FareType
FROM BusRegular
UNION ALL
SELECT BusNum,RouteNum,FareType
FROM BusSenior
ORDER BY BusNum,RouteNum,FareType


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

Go to Top of Page
  Previous Topic Topic Next Topic  
 New Topic  Reply to Topic
 Printer Friendly
Jump To:
SQL Server Forums © 2000-2009 SQLTeam Publishing, LLC Go To Top Of Page
This page was generated in 0.08 seconds. Powered By: Snitz Forums 2000