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
Welcome to Experts Exchange

Add your voice to the tech community where 5M+ people just like you are talking about what matters.

  • Help others & share knowledge
  • Earn cash & points
  • Learn & ask questions
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

On Demand Webinar: Networking for the Cloud Era

Did you know SD-WANs can improve network connectivity? Check out this webinar to learn how an SD-WAN simplified, one-click tool can help you migrate and manage data in the cloud.

Question has a verified solution.

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

Microsoft Access is a place to store data within tables and represent this stored data using multiple database objects such as in form of macros, forms, reports, etc. After a MS Access database is created there is need to improve the performance and…
In this blog post, we’ll look at how using thread_statistics can cause high memory usage.
Video by: Steve
Using examples as well as descriptions, step through each of the common simple join types, explaining differences in syntax, differences in expected outputs and showing how the queries run along with the actual outputs based upon a simple set of dem…
In this video, Percona Director of Solution Engineering Jon Tobin discusses the function and features of Percona Server for MongoDB. How Percona can help Percona can help you determine if Percona Server for MongoDB is the right solution for …

752 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