Solved

Query to look for accounts with both positive and negative values

Posted on 2014-03-21
9
293 Views
Last Modified: 2014-03-26
I have a table (tblInvoices) that has three (relevant) fields:

- AccountNumber
- InvoiceNumber
- InvoiceAmount

Some of the invoice amounts are positive and some are negative.

I'm looking for a query that will show all the accounts that have both positive and negative invoice amounts (i.e. an account that has at least one invoice that is positive and at least one invoice that is negative).

It doesn't matter to me if the query results show just one line for each matching AccountNumber of if the query results show all the invoices for each matching AccountNumber.

Thanks in advance!
0
Comment
Question by:jrmcanada2
  • 4
  • 2
  • 2
9 Comments
 
LVL 7

Expert Comment

by:COACHMAN99
ID: 39945729
sorry, needs more work
0
 
LVL 69

Expert Comment

by:ScottPletcher
ID: 39945747
Change the code below to ">= 0" if by positive you mean "0 or more" rather than "more than 0":


SELECT
    AccountNumber,
    SUM(CASE WHEN InvoiceAmount > 0 THEN 1 ELSE 0 END) AS Positive_Invoice_Count,
    SUM(CASE WHEN InvoiceAmount < 0 THEN 1 ELSE 0 END) AS Negative_Invoice_Count
FROM tblInvoices
GROUP BY
    AccountNumber
HAVING
    SUM(CASE WHEN InvoiceAmount > 0 THEN 1 ELSE 0 END) >= 1 AND
    SUM(CASE WHEN InvoiceAmount < 0 THEN 1 ELSE 0 END) >= 1
0
 
LVL 7

Expert Comment

by:COACHMAN99
ID: 39945779
or
SELECT DISTINCT Table1.AccountNumber, Table1.InvoiceNumber, Table1.InvoiceAmount
FROM Table1
WHERE (((Table1.InvoiceAmount)>0)) OR (((Table1.InvoiceAmount)<=0));
0
 

Author Comment

by:jrmcanada2
ID: 39945783
This works:

SELECT tblInvoices.AccountNumber
FROM tblInvoices
WHERE tblInvoices.InvoiceAmount>0 And
tblInvoices.AccountNumber IN (SELECT tblInvoices.AccountNumber FROM tblInvoices WHERE tblInvoices.InvoiceAmount<0)
ORDER BY tblInvoices.AccountNumber;
0
What is SQL Server and how does it work?

The purpose of this paper is to provide you background on SQL Server. It’s your self-study guide for learning fundamentals. It includes both the history of SQL and its technical basics. Concepts and definitions will form the solid foundation of your future DBA expertise.

 

Author Comment

by:jrmcanada2
ID: 39945994
I've requested that this question be closed as follows:

Accepted answer: 0 points for jrmcanada2's comment #a39945783

for the following reason:

Shortly after posting my question, I realized how to make the query.
0
 
LVL 69

Expert Comment

by:ScottPletcher
ID: 39945995
Could we see the answer?  That would add the solution to the EE db, and allow us to confirm that it was independent of the answers provided here.
0
 

Author Comment

by:jrmcanada2
ID: 39946183
I posted the answer above and accepted it as the solution.
0
 

Accepted Solution

by:
jrmcanada2 earned 0 total points
ID: 39946185
In order to avoid confusion, here it is again:

SELECT tblInvoices.AccountNumber
FROM tblInvoices
WHERE tblInvoices.InvoiceAmount>0 And
tblInvoices.AccountNumber IN (SELECT tblInvoices.AccountNumber FROM tblInvoices WHERE tblInvoices.InvoiceAmount<0)
ORDER BY tblInvoices.AccountNumber;
0

Featured Post

Zoho SalesIQ

Hassle-free live chat software re-imagined for business growth. 2 users, always free.

Question has a verified solution.

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

I see at least one EE question a week that pertains to using temporary tables in MS Access.  But surprisingly, I was unable to find a single article devoted solely to this topic. I don’t intend to describe all of the uses of temporary tables in t…
Ever wondered why sometimes your SQL Server is slow or unresponsive with connections spiking up but by the time you go in, all is well? The following article will show you how to install and configure a SQL job that will send you email alerts includ…
In Microsoft Access, learn the trick to repeating sub-report headings at the top of each page. The problem with sub-reports and headings: Add a dummy group to the sub report using the expression =1: Set the “Repeat Section” property of the dummy…
Viewers will learn how to use the UPDATE and DELETE statements to change or remove existing data from their tables. Make a table: Update a specific column given a specific row using the UPDATE statement: Remove a set of values using the DELETE s…

929 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

12 Experts available now in Live!

Get 1:1 Help Now