Quantcast
Channel: SQLServerCentral » SQL Server 2014 » Development - SQL Server 2014 » Latest topics
Viewing all articles
Browse latest Browse all 3145

Un-Group Data From Report

$
0
0
I wish I had more time to work on this but as usual everything needs to be done asap. This seems pretty tricky.Admittedly, I have not been the best about posting data per Jeff's very helpful Etiquette article. (http://www.sqlservercentral.com/articles/Best+Practices/61537/). So, this time around I am going to change that!We have an application that can spit out our Facility Structure into the following format. However, we have a need to take that data and feed it into another system. However, as you can see it is organized in a PARENT / CHILD structure. Indent Level Key:1 System2 Facility3 Service Line4 Division5 DepartmentIf you notice, each additional row is the child row to its parent above it as long as the Indent Level Key continues to increment. Once we start going back up the structure we essentially have a new Division/Service Line/or Facility depending on how far back we jump on the Indent Level for that next row. Here is some sample data.--===== If the test table already exists, drop itIF OBJECT_ID('TempDB..#Test_Data','U') IS NOT NULL DROP TABLE #Test_Data--===== Create the test table withSET ANSI_NULLS ONGOSET QUOTED_IDENTIFIER ONGOCREATE TABLE #Test_Data ( [Entity] [nvarchar](255) NULL, [IndentLevel] [float] NULL )--===== Insert the test data into the test table INSERT INTO #Test_Data (Entity,IndentLevel)SELECT 'Lockheed Martin Corp','1' UNION ALLSELECT 'Skunk Works - Roswell,NM','2' UNION ALLSELECT 'LM Aviation','3' UNION ALLSELECT 'Recon','4' UNION ALLSELECT 'Facility 1 - LM Cost Center: 500 - Admin','5' UNION ALLSELECT 'Facility 1 - LM Cost Center: 501 - Tech','5' UNION ALLSELECT 'Facility 1 - LM Cost Center: 502 - Clerical','5' UNION ALLSELECT 'Facility 1 - LM Cost Center: 503 - Acct','5' UNION ALLSELECT 'Facility 1 - LM Cost Center: 504 - HR','5' UNION ALLSELECT 'Fighter','4' UNION ALLSELECT 'Facility 1 - LM Cost Center: 505 - Ammunition','5' UNION ALLSELECT 'Facility 1 - LM Cost Center: 506 - Stuff','5' UNION ALLSELECT 'Facility 1 - LM Cost Center: 507 - More Stuff','5' UNION ALLSELECT 'Facility 1 - LM Cost Center: 508 - Cat5 Cables','5' UNION ALLSELECT 'Facility 1 - LM Cost Center: 509 - Mops','5' UNION ALLSELECT 'Bombers','4' UNION ALLSELECT 'Facility 1 - LM Cost Center: 510 - Wings','5' UNION ALLSELECT 'Facility 1 - LM Cost Center: 511 - HUD','5' UNION ALLSELECT 'Facility 1 - LM Cost Center: 512 - Radar','5' UNION ALLSELECT 'Facility 1 - LM Cost Center: 513 - Blackberry Phones','5' UNION ALLSELECT 'Deep 6 - Norfolk, VA','2' UNION ALLSELECT 'LM - Submarines','3' UNION ALLSELECT 'Virginia Class Subs','4' UNION ALLSELECT 'Facility 2 - LM Cost Center: 514 - Water','5' UNION ALLSELECT 'Facility 2 - LM Cost Center: 515- Reactors','5' UNION ALLSELECT 'Ohio Class Subs','4' UNION ALLSELECT 'Facility 2 - LM Cost Center: 516 - Torpedos','5' UNION ALLSELECT 'Facility 2 - LM Cost Center: 517 - Counter Measures','5' UNION ALLSELECT 'Facility 2 - LM Cost Center: 518 - Bang and Olufsen party speakers','5'GOSELECT * FROM #Test_Data-- END CURRENT DATA SAMPLESo with the data above, is it possible to get to this result?IF OBJECT_ID('TempDB..#Results','U') IS NOT NULL DROP TABLE #Results--===== Create the test table withSET ANSI_NULLS ONGOSET QUOTED_IDENTIFIER ONGOCREATE TABLE #Results ( [SYSTEM] [nvarchar](255) NULL, [FACILITY] [nvarchar](255) NULL, [SERVICE_LINE] [nvarchar](255) NULL, [DIVISION] [nvarchar](255) NULL, [DEPARTMENT] [nvarchar](255) NULL )--===== Insert the test data into the test table INSERT INTO #Results ([SYSTEM],FACILITY,SERVICE_LINE,DIVISION,DEPARTMENT)SELECT 'Lockheed Martin Corp','Skunk Works - Roswell,NM','LM Aviation','Recon','Facility 1 - LM Cost Center: 500 - Admin' UNION ALLSELECT 'Lockheed Martin Corp','Skunk Works - Roswell,NM','LM Aviation','Recon','Facility 1 - LM Cost Center: 501 - Tech' UNION ALLSELECT 'Lockheed Martin Corp','Skunk Works - Roswell,NM','LM Aviation','Recon','Facility 1 - LM Cost Center: 502 - Clerical' UNION ALLSELECT 'Lockheed Martin Corp','Skunk Works - Roswell,NM','LM Aviation','Recon','Facility 1 - LM Cost Center: 503 - Acct' UNION ALLSELECT 'Lockheed Martin Corp','Skunk Works - Roswell,NM','LM Aviation','Recon','Facility 1 - LM Cost Center: 504 - HR' UNION ALLSELECT 'Lockheed Martin Corp','Skunk Works - Roswell,NM','LM Aviation','Fighter','Facility 1 - LM Cost Center: 505 - Ammunition' UNION ALLSELECT 'Lockheed Martin Corp','Skunk Works - Roswell,NM','LM Aviation','Fighter','Facility 1 - LM Cost Center: 506 - Stuff' UNION ALLSELECT 'Lockheed Martin Corp','Skunk Works - Roswell,NM','LM Aviation','Fighter','Facility 1 - LM Cost Center: 507 - More Stuff' UNION ALLSELECT 'Lockheed Martin Corp','Skunk Works - Roswell,NM','LM Aviation','Fighter','Facility 1 - LM Cost Center: 508 - Cat5 Cables' UNION ALLSELECT 'Lockheed Martin Corp','Skunk Works - Roswell,NM','LM Aviation','Fighter','Facility 1 - LM Cost Center: 509 - Mops' UNION ALLSELECT 'Lockheed Martin Corp','Skunk Works - Roswell,NM','LM Aviation','Bombers','Facility 1 - LM Cost Center: 510 - Wings' UNION ALLSELECT 'Lockheed Martin Corp','Skunk Works - Roswell,NM','LM Aviation','Bombers','Facility 1 - LM Cost Center: 511 - HUD' UNION ALLSELECT 'Lockheed Martin Corp','Skunk Works - Roswell,NM','LM Aviation','Bombers','Facility 1 - LM Cost Center: 512 - Radar' UNION ALLSELECT 'Lockheed Martin Corp','Skunk Works - Roswell,NM','LM Aviation','Bombers','Facility 1 - LM Cost Center: 513 - Blackberry Phones' UNION ALLSELECT 'Lockheed Martin Corp','Deep 6 - Norfolk, VA','LM - Submarines','Virginia Class Subs','Facility 2 - LM Cost Center: 514 - Water' UNION ALLSELECT 'Lockheed Martin Corp','Deep 6 - Norfolk, VA','LM - Submarines','Virginia Class Subs','Facility 2 - LM Cost Center: 515- Reactors' UNION ALLSELECT 'Lockheed Martin Corp','Deep 6 - Norfolk, VA','LM - Submarines','Ohio Class Subs','Facility 2 - LM Cost Center: 516 - Torpedos' UNION ALLSELECT 'Lockheed Martin Corp','Deep 6 - Norfolk, VA','LM - Submarines','Ohio Class Subs','Facility 2 - LM Cost Center: 517 - Counter Measures' UNION ALLSELECT 'Lockheed Martin Corp','Deep 6 - Norfolk, VA','LM - Submarines','Ohio Class Subs','Facility 2 - LM Cost Center: 518 - Bang and Olufsen party speakers'GOSELECT *FROM #ResultsThank you!

Viewing all articles
Browse latest Browse all 3145

Trending Articles