Need help with a query

Hi Experts,

I want to write a query to group the data by groupID.

GroupID      GroupName      MenuItemID      MenuItemName
 8      Cheese      25      Cheese Pizza (VEG)
 8      Cheese      12      Bean & Cheese Quesadilla (GF & VEG)
 8      Cheese      13      Bean & DF Cheese Quesadilla (GF, VEG, & V)
 8      Cheese      26      Cheese Quesadilla (GF & VEG)
 8      Cheese      28      Cheese Quesadilla w/ DF Cheese (GF, VEG, & V)
 8      Cheese      27      Cheese Quesadilla (VEG)
 8      Cheese      121      Vegetable Quesadilla (VEG)
 8      Cheese      122      Vegetable Quesadilla w/ DF cheese (VEG & V)
 18      Grilled Cheese      64      Grilled Cheese on GF Bread (GF & VEG)
 18      Grilled Cheese      65      Grilled Cheese w/ DF Cheese (VEG & V)
 18      Grilled Cheese      63      Grilled Cheese (VEG)

 I want like this

 Cheese
     Cheese Pizza (VEG)
     Bean & Cheese Quesadilla (GF & VEG)
     ...
     ...
 Grilled Cheese
     Grilled Cheese on GF Bread (GF & VEG)
     Grilled Cheese w/ DF Cheese (VEG & V)

Thanks in advance.
RadhaKrishnaKiJayaAsked:
Who is Participating?

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

x
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.

Patrick MatthewsCommented:
What you really need there is a report to display the data the way you want.  Any of the major report packages would make this a fairly trivial exercise if you already have a query delivering the data as above.
0
PortletPaulEE Topic AdvisorCommented:
SQL does not provide features for pretty presentation

For example, Headings/Sub-Headings and indenting are things SQL does not do (or at least not do well). With all of that said this may help:
declare @YourTable table
    ([GroupID] int, [GroupName] varchar(14), [MenuItemID] int, [MenuItemName] varchar(45))

    
INSERT INTO @YourTable
    ([GroupID], [GroupName], [MenuItemID], [MenuItemName])
VALUES
    (8, 'Cheese', 25, 'Cheese Pizza (VEG)'),
    (8, 'Cheese', 12, 'Bean & Cheese Quesadilla (GF & VEG)'),
    (8, 'Cheese', 13, 'Bean & DF Cheese Quesadilla (GF, VEG, & V)'),
    (8, 'Cheese', 26, 'Cheese Quesadilla (GF & VEG)'),
    (8, 'Cheese', 28, 'Cheese Quesadilla w/ DF Cheese (GF, VEG, & V)'),
    (8, 'Cheese', 27, 'Cheese Quesadilla (VEG)'),
    (8, 'Cheese', 121, 'Vegetable Quesadilla (VEG)'),
    (8, 'Cheese', 122, 'Vegetable Quesadilla w/ DF cheese (VEG & V)'),
    (18, 'Grilled Cheese', 64, 'Grilled Cheese on GF Bread (GF & VEG)'),
    (18, 'Grilled Cheese', 65, 'Grilled Cheese w/ DF Cheese (VEG & V)'),
    (18, 'Grilled Cheese', 63, 'Grilled Cheese (VEG)')


select
      MenuName
from (
        select distinct GroupName, 0 rn, GroupName as MenuName from @YourTable
        union all
        select GroupName, 1, '    ' + MenuItemName from @YourTable
        ) d
order by 
      GroupName
    , rn
    , MenuName

Open in new window

Using this sample:
declare @YourTable table
    ([GroupID] int, [GroupName] varchar(14), [MenuItemID] int, [MenuItemName] varchar(45))

    
INSERT INTO @YourTable
    ([GroupID], [GroupName], [MenuItemID], [MenuItemName])
VALUES
    (8, 'Cheese', 25, 'Cheese Pizza (VEG)'),
    (8, 'Cheese', 12, 'Bean & Cheese Quesadilla (GF & VEG)'),
    (8, 'Cheese', 13, 'Bean & DF Cheese Quesadilla (GF, VEG, & V)'),
    (8, 'Cheese', 26, 'Cheese Quesadilla (GF & VEG)'),
    (8, 'Cheese', 28, 'Cheese Quesadilla w/ DF Cheese (GF, VEG, & V)'),
    (8, 'Cheese', 27, 'Cheese Quesadilla (VEG)'),
    (8, 'Cheese', 121, 'Vegetable Quesadilla (VEG)'),
    (8, 'Cheese', 122, 'Vegetable Quesadilla w/ DF cheese (VEG & V)'),
    (18, 'Grilled Cheese', 64, 'Grilled Cheese on GF Bread (GF & VEG)'),
    (18, 'Grilled Cheese', 65, 'Grilled Cheese w/ DF Cheese (VEG & V)'),
    (18, 'Grilled Cheese', 63, 'Grilled Cheese (VEG)')

Open in new window

the query above produced this result:
 	MenuName
	Cheese
	    Bean & Cheese Quesadilla (GF & VEG)
	    Bean & DF Cheese Quesadilla (GF, VEG, & V)
	    Cheese Pizza (VEG)
	    Cheese Quesadilla (GF & VEG)
	    Cheese Quesadilla (VEG)
	    Cheese Quesadilla w/ DF Cheese (GF, VEG, & V)
	    Vegetable Quesadilla (VEG)
	    Vegetable Quesadilla w/ DF cheese (VEG & V)
	Grilled Cheese
	    Grilled Cheese (VEG)
	    Grilled Cheese on GF Bread (GF & VEG)
	    Grilled Cheese w/ DF Cheese (VEG & V)

Open in new window

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
RadhaKrishnaKiJayaAuthor Commented:
Thanks a lot!!
0
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.