Complex SQL Query

Posted on 2003-03-30
Medium Priority
Last Modified: 2012-08-13
Hi, I have a very complex (for me it is) query to built. the reason why I neeed this complex query is because I need to use the new datagrid element from ASP.NET but that is another story. so I need the result of this query to fit exactly what I try to do.

Now, I will continue to try this until it will work and/or until somebody (like you) will getr me back with the explication of how to do it right.

I have a table for the product:

Product (table)
id (integer, PK)
name (varchar)
description (varchar)

I have an order table:
id (integer, PK)
orderdate (date)
clientid (integer)

I have an orderdetails table:
id (integer, PK)
orderid (integer)
productid (integer)
quantity (integer)
price (float)

I have a client table:
id (integer, PK)
firstname (varchar)
lastname (varchar)

Here is what I need:

For each Order.ClientID, I need all :
- Product.name
- OrderDetails.Quantity
- OrderDetails.Price
- Subtotal (OrderDetails.Quantity * OrderDetails.Price).


I know I have to play with join but to be honiest with you, since 1996 I develop dynamic web site that interact with database but I never be complex like this because I always plys with For...next, Do while... etc statement that let me create complex table instead of complex query (and repass into the database like 4-6 times instead of only one like what I try to do now). But for what I try to do 9datagrid), I need only one result from the database.

Thank you in advance if you can help me or if you can't, just let me know so I will try some other resource. If you need any more details or informations about this request, just let me know.
Question by:cyberfyb
LVL 15

Accepted Solution

andrewst earned 500 total points
ID: 8234235
Like this?

SELECT Order.ClientID
, Product.name
, OrderDetails.Quantity
, OrderDetails.Price
, (OrderDetails.Quantity * OrderDetails.Price) AS SubTotal
FROM Order, OrderDetails, Product
WHERE Order.ID = OrderDetails.OrderID
AND OrderDetails.ProductID = Product.ID;
LVL 50

Assisted Solution

Lowfatspread earned 500 total points
ID: 8238434
you seem to have some columns missing,
surely you want the products ordered by orderid
or to indicate which order they related to ... or when...

use the join syntax  and then put your specific requirements into the where clause..  

SELECT Order.ClientID
, Product.name
, OrderDetails.Quantity
, OrderDetails.Price
, (OrderDetails.Quantity * OrderDetails.Price) AS SubTotal
FROM Order
inner join OrderDetails
on Order.ID = OrderDetails.OrderID
Inner Join Product
On OrderDetails.ProductID = Product.ID

--put you customer/order  selections in here...
Where Order.clientID= xxx

order by  ClientID


LVL 17

Expert Comment

ID: 10360692
No comment has been added lately, so it's time to clean up this TA.
I will leave the following recommendation for this question in the Cleanup topic area:

Split: andrewst {http:#8234235} & Lowfatspread {http:#8238434}

Please leave any comments here within the next four days.

EE Cleanup Volunteer

Featured Post

Free Tool: Subnet Calculator

The subnet calculator helps you design networks by taking an IP address and network mask and returning information such as network, broadcast address, and host range.

One of a set of tools we're offering as a way of saying thank you for being a part of the community.

Question has a verified solution.

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

In this article, I’ll look at how you can use a backup to start a secondary instance for MongoDB.
MSSQL DB-maintenance also needs implementation of multiple activities. However, unprecedented errors can hamper the database management. In that case, deploying Stellar SQL Database Toolkit ensures fast and accurate database and backup repair as wel…
Polish reports in Access so they look terrific. Take yourself to another level. Equations, Back Color, Alternate Back Color. Write easy VBA Code. Tighten space to use less pages. Launch report from a menu, considering criteria only when it is filled…
With just a little bit of  SQL and VBA, many doors open to cool things like synchronize a list box to display data relevant to other information on a form.  If you have never written code or looked at an SQL statement before, no problem! ...  give i…
Suggested Courses

621 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