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 server 2008 - Derived Table SUM (Aggregate)

Posted on 2014-10-04
4
388 Views
Last Modified: 2014-11-03
Hello Experts,
Can you please look at this and give me a solution. I don't want to change this format of the derived table as I have tons created same way but want to aggregate result of both sums like this
SUM ( A1.CM_Sales + B1.YTD_Sales) AS 'Total Sales'  -> I want to sum result of both the derived tables


SELECT a1.Sto_Name,
       CM_Count,
       CM_Sales,
       YTD_Count,
       YTD_Sales
FROM (SELECT b.Sto_Name,
             COUNT(*) AS CM_Count,
             SUM(Sal_Amt) AS CM_Sales
      FROM Sales a
      JOIN Stores b ON a.Sto_ID = b.Sto_ID
      WHERE DATEPART(yy,Sal_Date) = DATEPART(yy,@RunDate) AND
            DATEPART(mm,Sal_Date) = (DATEPART(mm,@RunDate)-1)
      GROUP BY b.Sto_Name) AS A1

JOIN (SELECT Sto_Name,
             COUNT(*) AS YTD_Count,
             SUM(Sal_Amt) AS YTD_Sales
      FROM Sales a
      JOIN Stores b ON a.Sto_ID = b.Sto_ID
      WHERE DATEPART(yy,Sal_Date) = DATEPART(yy,@RunDate)
      GROUP BY b.Sto_Name) AS B1  ON a1.Sto_Name = b1.Sto_Name
0
Comment
Question by:BI 0824
  • 2
  • 2
4 Comments
 
LVL 48

Expert Comment

by:PortletPaul
ID: 40361832
Are you sure you want to add the Year To Date with Current Month? (normally YTD already includes CM)

That could double count the current month.

Are you certain DATEPART(mm,Sal_Date) = (DATEPART(mm,@RunDate)-1) will always work correctly?
(what happens if @RunDate is in Januarury?)

And, are you sure you don't want to simplify, e.g.

SELECT
      Sto_Name
    , COUNT(CASE WHEN DATEPART(mm, Sal_Date) = (DATEPART(mm, @RunDate) - 1) THEN a.Sto_ID_ID END)  AS CM_Count
    , SUM(CASE WHEN DATEPART(mm, Sal_Date) = (DATEPART(mm, @RunDate) - 1) THEN Sal_Amt ELSE 0 END) AS CM_Sales
    , COUNT(*)                                                                                     AS YTD_Count
    , SUM(Sal_Amt)                                                                                 AS YTD_Sales
FROM Sales a
      JOIN Stores b
                  ON a.Sto_ID = b.Sto_ID
WHERE DATEPART(yy, Sal_Date) = DATEPART(yy, @RunDate)
GROUP BY
      b.Sto_Name
;

Open in new window

0
 

Author Comment

by:BI 0824
ID: 40362129
The one given is not mine I mean the query but all the queries I need to deal with r the same like example given. So I just need to know how to sum both the values in the given format
0
 
LVL 48

Accepted Solution

by:
PortletPaul earned 500 total points
ID: 40362722
Whether they are yours or not isn't the issue - if you add those 2 numbers together the result will be wrong

The existing query will not work in January either.

Are you really determined to get a query that gives you bad results?

Here is how you would add those 2 numbers together:
All you need to do is place the calculation into the select clause, but you don't need SUM()

SELECT a1.Sto_Name,
       CM_Count,
       CM_Sales,
       YTD_Count,
       YTD_Sales,
       A1.CM_Sales + B1.YTD_Sales AS 'Total Sales' --<< BUT THIS IS A BAD NUMBER
FROM (
0
 

Author Closing Comment

by:BI 0824
ID: 40420881
Sorry about the late reply. It worked for me the way you did. Thanks.
0

Featured Post

Free Tool: Path Explorer

An intuitive utility to help find the CSS path to UI elements on a webpage. These paths are used frequently in a variety of front-end development and QA automation tasks.

One of a set of tools we're offering as a way of saying thank you for being a part of the community.

Question has a verified solution.

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

Suggested Solutions

Title # Comments Views Activity
Stored Proc - Rewrite 42 59
SSRS Deployment problem 5 66
RESTORE MASTER DATABASE -- NOW 2 20
interpreting data from function COLUMNS_UPDATED 2 16
Occasionally there is a need to clean table columns, especially if you have inherited legacy data. There are obviously many ways to accomplish that, including elaborate UPDATE queries with anywhere from one to numerous REPLACE functions (even within…
Use this article to create a batch file to backup a Microsoft SQL Server database to a Windows folder.  The folder can be on the local hard drive or on a network share.  This batch file will query the SQL server to get the current date & time and wi…
This video shows how to set up a shell script to accept a positional parameter when called, pass that to a SQL script, accept the output from the statement back and then manipulate it in the Shell.
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…

860 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