Solved

sql server 2008 - Derived Table SUM (Aggregate)

Posted on 2014-10-04
4
392 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
[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
  • 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

Guide to Performance: Optimization & Monitoring

Nowadays, monitoring is a mixture of tools, systems, and codes—making it a very complex process. And with this complexity, comes variables for failure. Get DZone’s new Guide to Performance to learn how to proactively find these variables and solve them before a disruption occurs.

Question has a verified solution.

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

Suggested Solutions

Title # Comments Views Activity
how to solve "recursive" database tables 2 41
SQL Server 2005 running VERY slowly on new hardware 22 54
SQL Lag Function DateDiff 2 23
mssql 7 32
Why is this different from all of the other step by step guides?  Because I make a living as a DBA and not as a writer and I lived through this experience. Defining the name: When I talk to people they say different names on this subject stuff l…
A Stored Procedure in Microsoft SQL Server is a powerful feature that it can be used to execute the Data Manipulation Language (DML) or Data Definition Language (DDL). Depending on business requirements, a single Stored Procedure can return differe…
Via a live example combined with referencing Books Online, show some of the information that can be extracted from the Catalog Views in SQL Server.
Via a live example, show how to shrink a transaction log file down to a reasonable size.

742 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