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 grouping, sub-totaling

Posted on 2011-09-28
3
215 Views
Last Modified: 2012-05-12
the attached script works fine -- except that the results for ABC & XYZ are pulling back sum (totals) for entire date-range -- what I am trying to do is for those to returning only back the current date range sum (set by overall grouping parameter).

current data format returned:

27.08.2011      24 hr      419.23      12820.37      3275.70
28.08.2011      24 hr      430.84      12820.37      3275.70
29.08.2011      24 hr      492.70      12820.37      3275.70
30.08.2011      24 hr      466.77      12820.37      3275.70
31.08.2011      24 hr      494.62      12820.37      3275.70

the last 2 columns are totalling the same dataset but with 1 additional filter - currently returning entire date range seleted as opposed to returning per-day as with the overall total column.  columns 4 & 5 for example should always be smaller than column 3.


USE [db1]

DECLARE @endDate   datetime
DECLARE @startDate datetime

SET @startDate = '2011-08-27 00:00:00.000'
SET @endDate   = '2011-09-30 23:59:59.999'
	    
select 

convert(varchar, CONVERT(DATETIME,(CAST(DATEPART(MONTH,SetupTime) AS VARCHAR)  +'-' + CAST(DATEPART(DAY,SetupTime) AS VARCHAR) + '-' + CAST(DATEPART(YEAR,SetupTime) AS VARCHAR))), 104) AS 'REPORTING DATE'

, '24 hr' AS 'PERIOD'

,cast( Cast(isnull(SUM(ChargeableAmount), 0) AS DECIMAL(18,2)) / 100000 AS DECIMAL(18,2)) AS 'TOTAL'
       
,(select cast( Cast(isnull(SUM(ChargeableAmount), 0) AS DECIMAL(18,2)) / 100000 AS DECIMAL(18,2))
	FROM [db1].[dbo].[CDRData]  
	WHERE (Direction = 1) AND (COSID =95) 
	  AND (SetupTime BETWEEN @startDate AND @endDate) 
	  AND (LocationZoneName = 'UK ECO ABC')) AS 'ABC' 
	  
,(select cast( Cast(isnull(SUM(ChargeableAmount), 0) AS DECIMAL(18,2)) / 100000 AS DECIMAL(18,2))
	FROM [db1].[dbo].[CDRData]  
	WHERE (Direction = 1) AND (COSID =95) 
	  AND (SetupTime BETWEEN @startDate AND @endDate) 
	  AND (LocationZoneName = 'UK ECO XYZ')) AS 'XYZ' 
			  
from [CDRData] 

WHERE (Direction = 1) 
	AND (COSID =95) 
	AND (SetupTime BETWEEN @startDate AND @endDate)
      
group by datepart(day,SetupTime), datepart(month,SetupTime), datepart(year,SetupTime)
order by CONVERT(DATETIME,(CAST(DATEPART(MONTH,SetupTime) AS VARCHAR)  +'-' + CAST(DATEPART(DAY,SetupTime) AS VARCHAR) + '-' + CAST(DATEPART(YEAR,SetupTime) AS VARCHAR)))

Open in new window

0
Comment
Question by:amillyard
  • 2
3 Comments
 
LVL 50

Accepted Solution

by:
Lowfatspread earned 500 total points
ID: 36715975
like this
USE [db1]

DECLARE @endDate   datetime
DECLARE @startDate datetime

SET @startDate = '2011-08-27 00:00:00.000'
SET @endDate   = '2011-09-30 23:59:59.999'
	    
select 

convert(char(10), SetupTime,104) as 'REPORTING DATE'

, '24 hr' AS 'PERIOD'

,cast( Cast(isnull(SUM(ChargeableAmount), 0) AS DECIMAL(18,2)) / 100000 AS DECIMAL(18,2)) AS 'TOTAL'
       
, cast( Cast(isnull(SUM(case locationzonename 
                            when 'UK ECO ABC' then
                          ChargeableAmount end), 0) AS DECIMAL(18,2)) / 100000 AS DECIMAL(18,2))
	 AS 'ABC' 
	  
, cast( Cast(isnull(SUM(case locationzonename
                         when 'UK ECO XYZ' then
                    ChargeableAmount end), 0) AS DECIMAL(18,2)) / 100000 AS DECIMAL(18,2))
	  AS 'XYZ' 
			  
from [CDRData] 

WHERE (Direction = 1) 
	AND (COSID =95) 
	AND (SetupTime BETWEEN @startDate AND @endDate)
      
group by convert(char(10), SetupTime,104)
order by 1

Open in new window

0
 
LVL 50

Expert Comment

by:Lowfatspread
ID: 36715984
ps the end date should be expressed as
SET @endDate   = '2011-09-30 23:59:59.997'

or you will round up to 2011-10-01 00:00:00.000

0
 

Author Closing Comment

by:amillyard
ID: 36715996
Lowfatspread:  thats was an excellent solution -- appreciate yuor time and efforts :-)
0

Featured Post

Free Tool: SSL Checker

Scans your site and returns information about your SSL implementation and certificate. Helpful for debugging and validating your SSL configuration.

One of a set of tools we are providing to everyone 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

Audit has been really one of the more interesting, most useful, yet difficult to maintain topics in the history of SQL Server. In earlier versions of SQL people had very few options for auditing in SQL Server. It typically meant using SQL Trace …
I have written a PowerShell script to "walk" the security structure of each SQL instance to find:         Each Login (Windows or SQL)             * Its Server Roles             * Every database to which the login is mapped             * The associated "Database User" for this …
A short tutorial showing how to set up an email signature in Outlook on the Web (previously known as OWA). For free email signatures designs, visit https://www.mail-signatures.com/articles/signature-templates/?sts=6651 If you want to manage em…
In an interesting question (https://www.experts-exchange.com/questions/29008360/) here at Experts Exchange, a member asked how to split a single image into multiple images. The primary usage for this is to place many photographs on a flatbed scanner…

856 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