Solved

SQL sum across columns

Posted on 2007-12-02
1
6,179 Views
Last Modified: 2012-06-27
Is it allowed to sum across the same columns twice in the same query? I get the correct result for the first sum but the second gives me 17962. That number makes no sense at all. While I was testing the code I was getting 17962 for both results.

            SELECT
            SUM(Fills) as sffillsy,
         SUM(
CASE WHEN ISNULL(InvoiceDate1, 0) BETWEEN '#yearRange1#'  AND '#YearRange2#' THEN ISNULL(InvoiceAmount1, 0) ELSE 0 END +
CASE WHEN ISNULL(InvoiceDate2, 0) BETWEEN '#yearRange1#'  AND '#YearRange2#' THEN ISNULL(InvoiceAmount2, 0) ELSE 0 END +
CASE WHEN ISNULL(InvoiceDate3, 0) BETWEEN '#yearRange1#'  AND '#YearRange2#' THEN ISNULL(InvoiceAmount3, 0) ELSE 0 END +
CASE WHEN ISNULL(InvoiceDate4, 0) BETWEEN '#yearRange1#'  AND '#YearRange2#' THEN ISNULL(InvoiceAmount4, 0) ELSE 0 END +
CASE WHEN ISNULL(InvoiceDate5, 0) BETWEEN '#yearRange1#'  AND '#YearRange2#' THEN ISNULL(InvoiceAmount5, 0) ELSE 0 END +
CASE WHEN ISNULL(InvoiceDate6, 0) BETWEEN '#yearRange1#'  AND '#YearRange2#' THEN ISNULL(InvoiceAmount6, 0) ELSE 0 END +
CASE WHEN ISNULL(InvoiceDate7, 0) BETWEEN '#yearRange1#'  AND '#YearRange2#' THEN ISNULL(InvoiceAmount7, 0) ELSE 0 END +
CASE WHEN ISNULL(InvoiceDate8, 0) BETWEEN '#yearRange1#'  AND '#YearRange2#' THEN ISNULL(InvoiceAmount8, 0) ELSE 0 END +
CASE WHEN ISNULL(InvoiceDate9, 0) BETWEEN '#yearRange1#'  AND '#YearRange2#' THEN ISNULL(InvoiceAmount9, 0) ELSE 0 END) as nwproductystat,
         SUM(
CASE WHEN ISNULL(InvoiceDate1, 0) <= '#YearRange2#' THEN ISNULL(InvoiceAmount1, 0) ELSE 0 END +
CASE WHEN ISNULL(InvoiceDate2, 0) <= '#YearRange2#' THEN ISNULL(InvoiceAmount2, 0) ELSE 0 END +
CASE WHEN ISNULL(InvoiceDate3, 0) <= '#YearRange2#' THEN ISNULL(InvoiceAmount3, 0) ELSE 0 END +
CASE WHEN ISNULL(InvoiceDate4, 0) <= '#YearRange2#' THEN ISNULL(InvoiceAmount4, 0) ELSE 0 END +
CASE WHEN ISNULL(InvoiceDate5, 0) <= '#YearRange2#' THEN ISNULL(InvoiceAmount5, 0) ELSE 0 END +
CASE WHEN ISNULL(InvoiceDate6, 0) <= '#YearRange2#' THEN ISNULL(InvoiceAmount6, 0) ELSE 0 END +
CASE WHEN ISNULL(InvoiceDate7, 0) <= '#YearRange2#' THEN ISNULL(InvoiceAmount7, 0) ELSE 0 END +
CASE WHEN ISNULL(InvoiceDate8, 0) <= '#YearRange2#' THEN ISNULL(InvoiceAmount8, 0) ELSE 0 END +
CASE WHEN ISNULL(InvoiceDate9, 0) <= '#YearRange2#' THEN ISNULL(InvoiceAmount9, 0) ELSE 0 END) as rcproductystat
            FROM JobBoardStats
            LEFT JOIN JOrder as J
            ON JobBoardStats.JobOrder=J.JobOrderNumber          
            where Date between '#YearRange1#' AND '#YearRange2#' AND ConsRec = '#user#' and (StatGoal = '2' or StatGoal = ' ') and StatType = 'RSF'

Open in new window

0
Comment
Question by:larksys
1 Comment
 
LVL 7

Accepted Solution

by:
multithreading earned 125 total points
ID: 20391446
Yes, the same column can appear in as many aggregates, formulas, whatever as you want.
0

Featured Post

3 Use Cases for Connected Systems

Our Dev teams are like yours. They’re continually cranking out code for new features/bugs fixes, testing, deploying, testing some more, responding to production monitoring events and more. It’s complex. So, we thought you’d like to see what’s working for us.

Question has a verified solution.

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

Suggested Solutions

Title # Comments Views Activity
Character matching different date formats for dates between 6 46
Documenting Data flow 4 39
Foxpro errors 4 30
Connecting to multiple databases to create a Dashboard 5 25
Composite queries are used to retrieve the results from joining multiple queries after applying any filters. UNION, INTERSECT, MINUS, and UNION ALL are some of the operators used to get certain desired results.​
Entering a date in Microsoft Access can be tricky. A typo can cause month and day to be shuffled, entering the day only causes an error, as does entering, say, day 31 in June. This article shows how an inputmask supported by code can help the user a…
Video by: Steve
Using examples as well as descriptions, step through each of the common simple join types, explaining differences in syntax, differences in expected outputs and showing how the queries run along with the actual outputs based upon a simple set of dem…
Polish reports in Access so they look terrific. Take yourself to another level. Equations, Back Color, Alternate Back Color. Write easy VBA Code. Tighten space to use less pages. Launch report from a menu, considering criteria only when it is filled…

776 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