?
Solved

help needed with sql query

Posted on 2006-04-27
3
Medium Priority
?
280 Views
Last Modified: 2008-02-26
Hi

I have the following set up in a database

Customer makes Orders (1 to M)
Orders contain Products (M to M)
Products have a configuration (M to 1 - e.g. many different products belong to the same configuration)
Configurations belong to a standard (M to 1 - many different configurations belong to the same standard)
Standards belong to a Category (M to 1 - many different standards belong to the same category)

Given a particular order ID i have to get all of the products in that order and then sort the products so they are arranged into groups of categories. Within a category they have to be sorted into standards. Within a standard they have to be sorted into groups. For example:

Category 1
  Standard 1
     Configuration 1
       Product a
       Product b
    Configuration 2
       Product c
       Product d

  Standard 2
     Configuration 3
       Product e
       Product f
    Configuration 4
       Product g
       Product h

Category 2
  Standard 3
     Configuration5
       Product i
       Product j
    Configuration 6
       Product k
       Product l

  Standard 4
     Configuration 7
       Product m
       Product n
    Configuration 8
       Product o
       Product p


I don't know how to sort by query so that the products are returned in this order.

The query also has to contain details of the category name, the standard name and the configuration name.

Here is my query so far:

select * from orders 0
inner join order details OD on 0.OrderID = Od.oRDERid
iiner join products P on P.ProductID = OD.OrderID
inner join configuration CF on P.ConfigID = CF.ConfigID
inner join standard S on s.standardID = CF.standardID
inner join category CAT on s.categoruyID = cat.categoryID
where O.oRDERid = @ORDERid

Like I say, it's the sorting of the products into the appropriate order that I don't know how to do.

Thanks in advance
andrea
0
Comment
Question by:andieje
3 Comments
 
LVL 12

Assisted Solution

by:wstuph
wstuph earned 1000 total points
ID: 16556852
You mean like this?

order by CategoryID, StandardID, ConfigurationID, ProductID

Assuming you actually have those fields - there's no way to tell w/ what you've posted.
0
 
LVL 2

Accepted Solution

by:
disentropy earned 1000 total points
ID: 16557893
Try this:

select * from orders 0
inner join order details OD on 0.OrderID = Od.oRDERid
iiner join products P on P.ProductID = OD.OrderID
inner join configuration CF on P.ConfigID = CF.ConfigID
inner join standard S on s.standardID = CF.standardID
inner join category CAT on s.categoruyID = cat.categoryID
where O.oRDERid = @ORDERid
ORDER BY cat.categoryID,CF.standardID,CF.ConfigID,P.ProductID
0
 

Author Comment

by:andieje
ID: 16559753
i'm sorry i confused myself. it was the middle of the night when i posted. :)
0

Featured Post

[Webinar] Cloud and Mobile-First Strategy

Maybe you’ve fully adopted the cloud since the beginning. Or maybe you started with on-prem resources but are pursuing a “cloud and mobile first” strategy. Getting to that end state has its challenges. Discover how to build out a 100% cloud and mobile IT strategy in this webinar.

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

Ever wondered why sometimes your SQL Server is slow or unresponsive with connections spiking up but by the time you go in, all is well? The following article will show you how to install and configure a SQL job that will send you email alerts includ…
In this article we will learn how to fix  “Cannot install SQL Server 2014 Service Pack 2: Unable to install windows installer msi file” error ?
This videos aims to give the viewer a basic demonstration of how a user can query current session information by using the SYS_CONTEXT function
Viewers will learn how the fundamental information of how to create a table.
Suggested Courses

862 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question