Smart questions
Smart answers
Smart people
INTELLIGENT WORK FORUMS
FOR COMPUTER PROFESSIONALS

Member Login

Come Join Us!

Are you a
Computer / IT professional?
Join Tek-Tips now!
  • Talk With Other Members
  • Be Notified Of Responses
    To Your Posts
  • Keyword Search
  • One-Click Access To Your
    Favorite Forums
  • Automated Signatures
    On Your Posts
  • Best Of All, It's Free!

Join Tek-Tips
*Tek-Tips's functionality depends on members receiving e-mail. By joining you are opting in to receive e-mail.

LINK TO THIS FORUM!

Add Stickiness To Your Site By Linking To This Professionally Managed Technical Forum.
Just copy and paste the
code below into your site.

Partner With Us!

"Best Of Breed" Forums Add Stickiness To Your Site
Partner Button
(Download This Button Today!)

Feedback

"...This site is truly a marvel. Without a doubt the most comprehensive, friendly and just plain useful resource of its kind..."

Geography

Where in the world do Tek-Tips members come from?
Jewel142 (ISP)
15 Oct 11 11:33
I am new to SQL and I'm having a hard time with getting the right results from my subquery.

I am reporting off of three tables, Vendors, Invoices and Line Items and I am trying to get the results for each invoice that has more than one line item in the InoviceLines Table.

Here is my code so far but for some reason, I'm only getting four records instead of the six that I should be getting.  (I tested it in Excel).  What am I doing wrong?  I've tried changing my joins but nothing seems to work.


SELECT     Vendors.VendorName, Invoices.InvoiceID, InvoiceSequence, InvoiceLineItems.InvoiceLineItemAmount
FROM         Vendors INNER JOIN
                      Invoices ON Vendors.VendorID = Invoices.VendorID RIGHT OUTER JOIN
                      InvoiceLineItems ON Invoices.InvoiceID = InvoiceLineItems.InvoiceID
WHERE  InvoiceLineItems.InvoiceSequence IN
                     (SELECT InvoiceLineItems.InvoiceSequence
                     FROM InvoiceLineItems
                     WHERE InvoiceLineItems.InvoiceSequence >1)


I am so frustrated right now.  Any help would be greatly appreciated!

Jewel~
JarlH (Programmer)
17 Oct 11 2:50
Without sample data and table defintions it's hard to find your problem. Which 2 rows aren't returned? How are those 2 different from the 4 rows that are returned?


Just guessing here, but what happens if you replace the subquery part with
WHERE  InvoiceLineItems.InvoiceID IN
                     (SELECT InvoiceLineItems.InvoiceID
                      FROM InvoiceLineItems
                      WHERE InvoiceLineItems.InvoiceSequence >1)



BTW, I can't see why you need to do that RIGHT OUTER JOIN, would a regular INNER JOIN give you the same result?
r937 (TechnicalUser)
17 Oct 11 8:01

CODE

SELECT Vendors.VendorName
     , Invoices.InvoiceID
     , InvoiceLineItems.InvoiceSequence
     , InvoiceLineItems.InvoiceLineItemAmount
  FROM Vendors
INNER
  JOIN Invoices
    ON Invoices.VendorID = Vendors.VendorID
   AND EXISTS
       ( SELECT *
           FROM InvoiceLineItems
          WHERE InvoiceID = Invoices.InvoiceID
            AND InvoiceSequence > 1 )
INNER
  JOIN InvoiceLineItems
    ON InvoiceLineItems.InvoiceID = Invoices.InvoiceID

r937.com | rudy.ca
Buy my new book Simply SQL from Amazon

Jewel142 (ISP)
17 Oct 11 12:24
Thank you - it worked!

Jewel

Reply To This Thread

Posting in the Tek-Tips forums is a member-only feature.

Click Here to join Tek-Tips and talk with other members!

Back To Forum

Close Box

Join Tek-Tips® Today!

Join your peers on the Internet's largest technical computer professional community.
It's easy to join and it's free.

Here's Why Members Love Tek-Tips Forums:

Register now while it's still free!

Already a member? Close this window and log in.

Join Us             Close