MS SQL Server Data Loop

I need to loop though some data that gets the company...
Then adds the customers in a loop
Then gets the detail data in a loop

And builds "layered" data set

This is my "desired" output "look"
All data will be full left flush of course
CompanyStart   ('CompanyStart,' + Company)
   CustomerStart	('CustStart,' + Customer + ',' + CustomerAddress)
       Detail   ('Detail,' + FullName + ',' +  Amount)   
   CustomerEnd  ('CustEnd')
CompanyEnd  ('CompanyEnd')

Open in new window

Here is example data
        Company VARCHAR(100) ,
        Customer VARCHAR(100) ,
        CustomerAddress VARCHAR(1000) ,
        FullName VARCHAR(100) ,
        Amount VARCHAR(100)
INSERT INTO @table ( Company ,
                     Customer ,
                     FullName ,
                     Amount )
VALUES ( 'MyCompany', 'ABC Company', '100 Main Street Anywhere Texas 12345', 'Tom Adams', '1.00' ) ,
       ( 'MyCompany', 'ABC Company', '100 Main Street Anywhere Texas 12345', 'Sarah Branch', '1.14' ) ,
       ( 'MyCompany', 'ABC Company', '100 Main Street Anywhere Texas 12345', 'Jim Smith', '.26' ) ,
       ( 'MyCompany', 'Wholesale Foods', '2301 65th Street Somewhere Texas 25689', 'Benjamin Derikson', '3.25' ) ,
       ( 'MyCompany', 'Wholesale Foods', '2301 65th Street Somewhere Texas 25689', 'Carley Huts', '10.25' );

SELECT * FROM @table

Open in new window

Larry Bristersr. DeveloperAsked:
Who is Participating?

[Product update] Infrastructure Analysis Tool is now available with Business Accounts.Learn More

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.

Mark WillsTopic AdvisorCommented:
So, it is an XML style hierarchy but without the XML -  is that a fair description ?

Will have a look - probably a few different ways to achieve - will be interesting to see the 'Answers' :)

And thank you for supplying a well constructed question including test data :)
Larry Bristersr. DeveloperAuthor Commented:
Key Mark...
Correct... xml style hierarchy without the xml tags

Mark WillsTopic AdvisorCommented:
Well, here is my first foray and testing other possibilities -  including stripping the xml tags...

Also wondered about customer branches, so decided that a grouping on customeraddress was needed. Might need to consider another level.

But need some sleep preferably before I have to wake up :) So, decided to post a working version...

  Select company, min(customer) as customer,1 as lvl, 'CompanyStart,' + company as strData
  from #table group by company
  union all
  Select company, customer,2 as lvl,'  CustStart,' + Customer + ',' + CustomerAddress as strData
  from #table group by company,customer,CustomerAddress
  union all
  Select company, customer,3 as lvl, '    Detail,' + FullName + ',' +  convert (varchar(20),Amount) as strData
  from #table 
  union all
  Select company, customer,4 as lvl, '  CustEnd' as strData
  from #table group by company,customer,customerAddress
  union all
  Select company, max(customer),5 as lvl, 'CompanyEnd' as strData
  from #table group by company
  order by 1,2,3

Open in new window

Hopefully a more elegant solution to follow - the above was loosely inspired by xml explicit....

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
Larry Bristersr. DeveloperAuthor Commented:
Hey Mark...
I am testing but looks good so far!
Larry Bristersr. DeveloperAuthor Commented:
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 SQL Server

From novice to tech pro — start learning today.