Solved

SQL sum across columns

Posted on 2007-12-02
1
6,169 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
Comment Utility
Yes, the same column can appear in as many aggregates, formulas, whatever as you want.
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

PL/SQL can be a very powerful tool for working directly with database tables. Being able to loop will allow you to perform more complex operations, but can be a little tricky to write correctly. This article will provide examples of basic loops alon…
I annotated my article on ransomware somewhat extensively, but I keep adding new references and wanted to put a link to the reference library.  Despite all the reference tools I have on hand, it was not easy to find a way to do this easily. I finall…
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…

743 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

17 Experts available now in Live!

Get 1:1 Help Now