Welcome to Experts Exchange

Add your voice to the tech community where 5M+ people, just like you, are talking about what matters.

  • Help others & share knowledge
  • Earn cash & points
  • Learn & ask questions
Solved

SQL sum across columns

Posted on 2007-12-02
1
6,185 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

Windows Server 2016: All you need to know

Learn about Hyper-V features that increase functionality and usability of Microsoft Windows Server 2016. Also, throughout this eBook, you’ll find some basic PowerShell examples that will help you leverage the scripts in your environments!

Question has a verified solution.

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

Suggested Solutions

I'm trying, I really am. But I've seen so many wrong approaches involving date(time) boundaries I despair about my inability to explain it. I've seen quite a few recently that define a non-leap year as 364 days, or 366 days and the list goes on. …
CCModeler offers a way to enter basic information like entities, attributes and relationships and export them as yEd or erviz diagram. It also can import existing Access or SQL Server tables with relationships.
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…

829 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