Solved

Query Help

Posted on 2011-09-24
2
276 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

Suggested Solutions

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.
Ever needed a SQL 2008 Database replicated/mirrored/log shipped on another server but you can't take the downtime inflicted by initial snapshot or disconnect while T-logs are restored or mirror applied? You can use SQL Server Initialize from Backup…
Familiarize people with the process of retrieving data from SQL Server using an Access pass-thru query. Microsoft Access is a very powerful client/server development tool. One of the ways that you can retrieve data from a SQL Server is by using a pa…
Viewers will learn how the fundamental information of how to create a table.

685 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