Solved

Query Help

Posted on 2011-09-24
2
262 Views
Last Modified: 2012-05-12
I'm trying to return only 1 row for each order.
An order can have multiple line items.

I want to check each line item to make sure the quantity in stock is greater than 0.
Right now, there is an order that has two lines and both are in stock, but I just want it to return one row.
I pretty much just need a couple columns from the orders table and not the line items table.

SELECT
      *
FROM Orders a
INNER JOIN LineItems b
ON a.OrderID = b.OrderID
INNER JOIN Products c ON c.ProductID = b.ProductID
WHERE
      c.QuantityInStock > 0
AND
      a.OrderStatusID = 1
AND
      a.GatewaySuccessful = 1
AND
      a.ToBePrinted = 1
0
Comment
Question by:JRockFL
2 Comments
 
LVL 92

Accepted Solution

by:
Patrick Matthews earned 500 total points
ID: 36592383
Then try something like this:

SELECT
      a.ColumnX, a.ColumnY, a.ColumnZ
FROM Orders a
INNER JOIN LineItems b
ON a.OrderID = b.OrderID 
INNER JOIN Products c ON c.ProductID = b.ProductID
WHERE
      c.QuantityInStock > 0
AND 
      a.OrderStatusID = 1
AND 
      a.GatewaySuccessful = 1
AND 
      a.ToBePrinted = 1
GROUP BY
      a.ColumnX, a.ColumnY, a.ColumnZ

Open in new window

0
 
LVL 8

Author Closing Comment

by:JRockFL
ID: 36592395
Awesome, thank you
0

Featured Post

Best Practices: Disaster Recovery Testing

Besides backup, any IT division should have a disaster recovery plan. You will find a few tips below relating to the development of such a plan and to what issues one should pay special attention in the course of backup planning.

Question has a verified solution.

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

If you have heard of RFC822 date formats, they can be quite a challenge in SQL Server. RFC822 is an Internet standard format for email message headers, including all dates within those headers. The RFC822 protocols are available in detail at:   ht…
In this article we will get to know that how can we recover deleted data if it happens accidently. We really can recover deleted rows if we know the time when data is deleted by using the transaction log.
Using examples as well as descriptions, and references to Books Online, show the different Recovery Models available in SQL Server and explain, as well as show how full, differential and transaction log backups are performed
Viewers will learn how the fundamental information of how to create a table.

785 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