Solved

Query Help

Posted on 2011-09-24
2
241 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
Comment Utility
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
Comment Utility
Awesome, thank you
0

Featured Post

How to run any project with ease

Manage projects of all sizes how you want. Great for personal to-do lists, project milestones, team priorities and launch plans.
- Combine task lists, docs, spreadsheets, and chat in one
- View and edit from mobile/offline
- Cut down on emails

Join & Write a Comment

Occasionally there is a need to clean table columns, especially if you have inherited legacy data. There are obviously many ways to accomplish that, including elaborate UPDATE queries with anywhere from one to numerous REPLACE functions (even within…
In this article I will describe the Backup & Restore method as one possible migration process and I will add the extra tasks needed for an upgrade when and where is applied so it will cover all.
Via a live example, show how to setup several different housekeeping processes for a SQL Server.
Viewers will learn how to use the INSERT statement to insert data into their tables. It will also introduce the NULL statement, to show them what happens when no value is giving for any given column.

762 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

Need Help in Real-Time?

Connect with top rated Experts

10 Experts available now in Live!

Get 1:1 Help Now