Click here to monitor SSC
SQLServerCentral is supported by Red Gate Software Ltd.
 
Log in  ::  Register  ::  Not logged in
 
 
 
        
Home       Members    Calendar    Who's On


Add to briefcase

daily weekly monthly calculate sum Expand / Collapse
Author
Message
Posted Wednesday, February 6, 2013 5:37 AM
Forum Newbie

Forum NewbieForum NewbieForum NewbieForum NewbieForum NewbieForum NewbieForum NewbieForum Newbie

Group: General Forum Members
Last Login: Wednesday, February 20, 2013 5:06 AM
Points: 3, Visits: 16
hi guys, i need urgent sql query,kindly help me
i need sql output like

given parameter @startdate and @enddate->01/01/2011 to 01/01/2012

output like

state productname salesamtdaily salesamtweekly saleamtmonthly

aaa abc 1000 5000 20000
.
.
.
.
like this ..its sample,any help guys
tnkxin advance
b raj

Post #1416436
Posted Wednesday, February 6, 2013 5:40 AM


SSCertifiable

SSCertifiableSSCertifiableSSCertifiableSSCertifiableSSCertifiableSSCertifiableSSCertifiableSSCertifiableSSCertifiable

Group: General Forum Members
Last Login: Today @ 6:53 AM
Points: 5,132, Visits: 4,922
Please follow the second link in my signature on posting code and data for the best help, that will then give us a play pen to test ideas on to help you out quicker.



Want an answer fast? Try here
How to post data/code for the best help - Jeff Moden
Need a string splitter, try this - Jeff Moden
How to post performance problems - Gail Shaw
CrossTabs-Part1 & Part2 - Jeff Moden
SQL Server Backup, Integrity Check, and Index and Statistics Maintenance - Ola Hallengren
Managing Transaction Logs - Gail Shaw
Troubleshooting SQL Server: A Guide for the Accidental DBA - Jonathan Kehayias and Ted Krueger

Post #1416439
Posted Thursday, February 7, 2013 5:57 AM
Forum Newbie

Forum NewbieForum NewbieForum NewbieForum NewbieForum NewbieForum NewbieForum NewbieForum Newbie

Group: General Forum Members
Last Login: Wednesday, February 20, 2013 5:06 AM
Points: 3, Visits: 16
hi guys,

i hv following data,

i have the table value:

state AgentName Agenttype Timecreated amount

Kolkata 000123AA Distributor 2012-08-01 0.00

Kolkata 000123AA Distributor 2012-08-01 2.00

Kolkata 000555VB Distributor 2012-09-04 10.00

Kolkata 000555 VB Distributor 2012-09-05 10.00

mumbai 000567 AD Agent 2012-09-05 100.00

mumbai 000567 AD Agent 2012-09-05 200.00



i need o/p like ,

state AgentName Agenttype dailysum weeklysum monthlysum

Kolkata 000123AA Distributor 15 25 50

Kolkata 000555VB Distributor 45 110.00 500

mumbai 000567 AD Agent 67 810.00 1000


tnkx in regards

b raj



Post #1417018
Posted Thursday, February 7, 2013 9:41 AM


SSCarpal Tunnel

SSCarpal TunnelSSCarpal TunnelSSCarpal TunnelSSCarpal TunnelSSCarpal TunnelSSCarpal TunnelSSCarpal TunnelSSCarpal TunnelSSCarpal Tunnel

Group: General Forum Members
Last Login: Monday, July 21, 2014 11:49 PM
Points: 4,973, Visits: 11,660
Please reread Anthony Green's post.


Help us to help you. For better, quicker and more-focused answers to your questions, consider following the advice in this link.

When you ask a question (and please do ask a question: "My T-SQL does not work" just doesn't cut it), please provide enough information for us to understand its context.
Post #1417195
Posted Thursday, February 7, 2013 8:01 PM


Hall of Fame

Hall of FameHall of FameHall of FameHall of FameHall of FameHall of FameHall of FameHall of FameHall of Fame

Group: General Forum Members
Last Login: Yesterday @ 6:03 PM
Points: 3,609, Visits: 5,221
Further to what Phil and Anthony said, you need to show your expected results exactly as they should be derived from your sample data.

Otherwise we're not going to know how you expect to derive daily, weekly and monthly amounts.



My mantra: No loops! No CURSORs! No RBAR! Hoo-uh!

My thought question: Have you ever been told that your query runs too fast?

My advice:
INDEXing a poor-performing query is like putting sugar on cat food. Yeah, it probably tastes better but are you sure you want to eat it?
The path of least resistance can be a slippery slope. Take care that fixing your fixes of fixes doesn't snowball and end up costing you more than fixing the root cause would have in the first place.


Need to UNPIVOT? Why not CROSS APPLY VALUES instead?
Since random numbers are too important to be left to chance, let's generate some!
Learn to understand recursive CTEs by example.
Splitting strings based on patterns can be fast!
Post #1417433
Posted Monday, February 11, 2013 3:21 PM


Old Hand

Old HandOld HandOld HandOld HandOld HandOld HandOld HandOld Hand

Group: General Forum Members
Last Login: Yesterday @ 12:11 PM
Points: 308, Visits: 812
Also we would like to know if you would be running the report once a day, week , month? It seems to me that at any point you execute the report you would like to see the aggregates for that day, that week and that month for the given agent. In theory it looks like you need three record sets, one for each aggregation, and then join them together by the agent ID. You need to start with a Calendar table. Please follow the advise of the previous posts so you can be better guided.
Post #1418655
Posted Tuesday, February 12, 2013 8:27 AM
SSC-Enthusiastic

SSC-EnthusiasticSSC-EnthusiasticSSC-EnthusiasticSSC-EnthusiasticSSC-EnthusiasticSSC-EnthusiasticSSC-EnthusiasticSSC-Enthusiastic

Group: General Forum Members
Last Login: Yesterday @ 6:33 AM
Points: 106, Visits: 1,107
Something to help get you started. One of many ways including numbers/tally tables.


--DROP TABLE #Agent


-- create a temp table
CREATE TABLE #Agent ([state] varchar(50)
,AgentName varchar(50)
,Agenttype varchar(50)
,Timecreated datetime
,amount int
)
-- insert data into temp table
INSERT INTO #Agent

SELECT 'Kolkata', '000123AA', 'Distributor', '2012-08-01', 0.00
UNION ALL
SELECT 'Kolkata', '000123AA', 'Distributor', '2012-08-01', 2.00
UNION ALL
SELECT 'Kolkata', '000123AA', 'Distributor', '2012-08-21', 15.00
UNION ALL
SELECT 'Kolkata', '000123AA', 'Distributor', '2012-09-01', 20.00
UNION ALL
SELECT 'Kolkata','000555VB','Distributor','2012-09-04', 10.00
UNION ALL
SELECT 'Kolkata','000555VB','Distributor','2012-09-05', 10.00
UNION ALL
SELECT 'mumbai','000567AD','Agent','2012-09-05', 100.00
UNION ALL
select 'mumbai','000567AD','Agent','2012-09-05', 200.00
UNION ALL
SELECT 'mumbai','000567AD','Agent','2012-10-05', 50.00


-- declare parameters
DECLARE @startdate DATETIME
DECLARE @enddate DATETIME

-- set date range in parameters
SET @startdate = '20120801'
SET @enddate = '20121005'

---- possible dates in temp table (use for parameters above)
----2012-08-01
----2012-08-21
----2012-09-01
----2012-09-04
----2012-09-05
----2012-09-05
----2012-10-05


-- create cte's (common table expression) to hold calculated values for days, weeks, months
-- does not account for different years for date parameters, use DATEPART with year

;WITH dailySums
AS (
-- daily amounts
SELECT [state]
,AgentName
,Agenttype
,Timecreated

-- for joining below cte statements
,DATEPART(wk, Timecreated) wk -- get the number of the week
,DATEPART(mm, Timecreated) mm -- get the number of the month

,ISNULL(SUM(amount),0) AS DailyAmount -- daily sums to be grouped on


FROM #agent -- source table (temp table in this case)

-- assums the time portion of the date field is not used (all zero's, ex: 2012-08-01 00:00:00.000)
-- filter on date parameters
WHERE timecreated >= @startdate
AND timecreated <= @enddate

-- group the sum on these fields
GROUP BY [state]
,AgentName
,Agenttype
,Timecreated
),
weeklySums
AS (
-- weekly amounts (if spanning multiple years this needs further refinement)
SELECT [state]
,AgentName
,Agenttype
,DATEPART(wk, Timecreated) weeknumber -- get the number of the week

,ISNULL(SUM(amount),0) AS WeeklyAmount -- weekly sums

FROM #agent -- source table (temp table in this case)

-- assums the time portion of the date field is not used (all zero's, ex: 2012-08-01 00:00:00.000)
-- filter on date parameters
WHERE timecreated >= @startdate
AND timecreated <= @enddate

-- group the sum on these fields
GROUP BY [state]
,AgentName
,Agenttype

,DATEPART(wk, Timecreated)

),
monthlySums
AS (
-- monthly amounts (if spanning multiple years this needs further refinement)
SELECT [state]
,AgentName
,Agenttype
,DATEPART(mm, Timecreated) monthnumber -- get the number of the month

,ISNULL(SUM(amount),0) AS MonthlyAmount

FROM #agent -- source table (temp table in this case)

-- assums the time portion of the date field is not used (all zero's, ex: 2012-08-01 00:00:00.000)
-- filter on date parameters
WHERE timecreated >= @startdate
AND timecreated <= @enddate

-- group the sum on these fields
GROUP BY [state]
,AgentName
,Agenttype

,DATEPART(mm, Timecreated)
)
-- NOTE: only one cte statement can be executed at a time, comment all others

-- test each individually

/* daily cte statment */
--SELECT [state], AgentName, Agenttype, Timecreated, wk, mm, DailyAmount
--FROM dailySums

/* weekly cte statement */
--SELECT [state], AgentName, Agenttype, weeknumber, WeeklyAmount
--FROM weeklySums

/* monthly cte statement */
--SELECT [state], AgentName, Agenttype, monthnumber, MonthlyAmount
--FROM monthlySums



---- join each cte statement to get summed values daily, weekly, monthly
---- NOTE: this report / query really does not make much sense
---- but gives an idea (one out of many) of how this might be done
SELECT d.[state]
,d.AgentName
,d.Agenttype
,d.Timecreated
,d.wk
,d.mm

,d.DailyAmount -- daily sums
,w.WeeklyAmount -- weekly sums (does not make sense to include with daily sums ???? )
,m.MonthlyAmount -- monthly sums (does not make sense to included with daily and weekly sums ???? )

FROM dailySums d -- first cte statement

INNER JOIN weeklySums w -- second cte statement
ON d.[state] = w.[state]
AND d.AgentName = w.AgentName
AND d.Agenttype = w.Agenttype

AND d.wk = w.weeknumber -- number value of the week

INNER JOIN monthlySums m -- third cte statement
ON d.[state] = m.[state]
AND d.AgentName = m.AgentName
AND d.Agenttype = m.Agenttype

AND d.mm = m.monthnumber -- number value of the year

Post #1419000
« Prev Topic | Next Topic »

Add to briefcase

Permissions Expand / Collapse