Want to win a PS4? Go Premium and enter to win our High-Tech Treats giveaway. Enter to Win

x
?
Solved

SQL sum across columns

Posted on 2007-12-02
1
Medium Priority
?
6,203 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
[X]
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
1 Comment
 
LVL 7

Accepted Solution

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

Featured Post

Free learning courses: Active Directory Deep Dive

Get a firm grasp on your IT environment when you learn Active Directory best practices with Veeam! Watch all, or choose any amount, of this three-part webinar series to improve your skills. From the basics to virtualization and backup, we got you covered.

Question has a verified solution.

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

Your data is at risk. Probably more today that at any other time in history. There are simply more people with more access to the Web with bad intentions.
Backups and Disaster RecoveryIn this post, we’ll look at strategies for backups and disaster recovery.
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…
In this video, Percona Solution Engineer Dimitri Vanoverbeke discusses why you want to use at least three nodes in a database cluster. To discuss how Percona Consulting can help with your design and architecture needs for your database and infras…
Suggested Courses

597 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