sql query to unpivot

Here is some sample data from a sql server 2012 table

CREATE TABLE [dbo].[progressive](
	[Bank] [varchar](255) NULL,
	[MachineNumber] [float] NULL,
	[ProgressiveDescription] [varchar](255) NULL,
	[ProgressiveType] [varchar](255) NULL,
	[Level1Name] [varchar](255) NULL,
	[Level2Name] [varchar](255) NULL,
	[Level3Name] [varchar](255) NULL,
	[TotalReset] [float] NULL,
	[Level1Reset] [money] NULL,
	[Level2Reset] [money] NULL,
	[Level3Reset] [money] NULL,
	[TotalRate] [float] NULL,
	[Level1Rate] [float] NULL,
	[Level2Rate] [float] NULL,
	[Level3Rate] [float] NULL,
	[EmbedType] [char](1) NULL
) ON [PRIMARY]

GO
SET ANSI_PADDING OFF
GO



INSERT [dbo].[progressive] ([Bank], [MachineNumber], [ProgressiveDescription], [ProgressiveType], [Level1Name], [Level2Name], [Level3Name], [TotalReset], [Level1Reset], [Level2Reset], [Level3Reset], [TotalRate], [Level1Rate], [Level2Rate], [Level3Rate], [EmbedType]) VALUES (N'0106', 11423, N'88 FORTUNES', N'Linked', N'Grand', N'Major', N'Minor', 1835, 1000.0000, 800.0000, 25.0000, 2.35, 0.4, 0.3, 0.65, N'1')

Open in new window


Each row has information for 3 levels


Bank	Number	  Description	       Type  	Lev1Name	Lev2Name	Lev3Name	Lev1Reset	Lev2Reset	Lev3Reset	Lev1Rate	Lev2Rate	Lev3Rate	EmbedType
106	      11423	88 FORTUNES	Linked	Grand	       Major	       Minor	        1000	        800            	25	                0.4            	0.3	                0.65	                1

Open in new window


Trying to write a query that will return 3 rows for each machine with each of the 3 rows containing level info


Bank      Number        Description             Type        LevName      LevReset       LevRate         EmbedType
106            11423      88 FORTUNES      Linked      Grand              1000              .4                       1
106            11423      88 FORTUNES      Linked      Major              800                      .3                       1
106            11423      88 FORTUNES      Linked      Minor              25                      .65                       1


Perhaps unpivot ????
johnnyg123Asked:
Who is Participating?
I wear a lot of hats...

"The solutions and answers provided on Experts Exchange have been extremely helpful to me over the last few years. I wear a lot of hats - Developer, Database Administrator, Help Desk, etc., so I know a lot of things but not a lot about one thing. Experts Exchange gives me answers from people who do know a lot about one thing, in a easy to use platform." -Todd S.

PortletPaulfreelancerCommented:
Not sure if this works in Excel/Office, it does in SQL Server

There is an efficient technique that unpivots, without using that word.
See Unpivotting by CROSS APPLY and VALUES
and Spotlight on UNPIVOT, Part 1  

sselect
       p.Bank, p.ProgressiveDescription, p.ProgressiveType, p.EmbedType
     , ca.Type, ca.LevName, ca.LevReset, ca.LevRate
from progressive p
cross apply (
    values
        ('lvl1', p.Level1Name, p.Level1Reset, p.Level1Rate)
      , ('lvl2', p.Level2Name, p.Level2Reset, p.Level2Rate)
      , ('lvl3', p.Level3Name, p.Level3Reset, p.Level3Rate)
    ) ca (Type, LevName, LevReset, LevRate)         -- << column aliases defined here
;

Open in new window

see it working here: http://rextester.com/OCT63934
0

Experts Exchange Solution brought to you by

Your issues matter to us.

Facing a tech roadblock? Get the help and guidance you need from experienced professionals who care. Ask your question anytime, anywhere, with no hassle.

Start your 7-day free trial
johnnyg123Author Commented:
Perfect...thanks for the links as well!


thank you!
1
It's more than this solution.Get answers and train to solve all your tech problems - anytime, anywhere.Try it for free Edge Out The Competitionfor your dream job with proven skills and certifications.Get started today Stand Outas the employee with proven skills.Start learning today for free Move Your Career Forwardwith certification training in the latest technologies.Start your trial today
Microsoft Excel

From novice to tech pro — start learning today.