SQL Clone
SQLServerCentral is supported by Redgate
 
Log in  ::  Register  ::  Not logged in
 
 
 


daily weekly monthly calculate sum


daily weekly monthly calculate sum

Author
Message
bhupesh.nic
bhupesh.nic
Grasshopper
Grasshopper (15 reputation)Grasshopper (15 reputation)Grasshopper (15 reputation)Grasshopper (15 reputation)Grasshopper (15 reputation)Grasshopper (15 reputation)Grasshopper (15 reputation)Grasshopper (15 reputation)

Group: General Forum Members
Points: 15 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
anthony.green
anthony.green
SSChampion
SSChampion (10K reputation)SSChampion (10K reputation)SSChampion (10K reputation)SSChampion (10K reputation)SSChampion (10K reputation)SSChampion (10K reputation)SSChampion (10K reputation)SSChampion (10K reputation)

Group: General Forum Members
Points: 10466 Visits: 6378
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
When a question, really isn't a question - Jeff Smith
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


bhupesh.nic
bhupesh.nic
Grasshopper
Grasshopper (15 reputation)Grasshopper (15 reputation)Grasshopper (15 reputation)Grasshopper (15 reputation)Grasshopper (15 reputation)Grasshopper (15 reputation)Grasshopper (15 reputation)Grasshopper (15 reputation)

Group: General Forum Members
Points: 15 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
Phil Parkin
Phil Parkin
SSCoach
SSCoach (19K reputation)SSCoach (19K reputation)SSCoach (19K reputation)SSCoach (19K reputation)SSCoach (19K reputation)SSCoach (19K reputation)SSCoach (19K reputation)SSCoach (19K reputation)

Group: General Forum Members
Points: 19610 Visits: 20462
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.

If the answer to your question can be found with a brief Google search, please perform the search yourself, rather than expecting one of the SSC members to do it for you.

Please surround any code or links you post with the appropriate IFCode formatting tags. It helps readability a lot.
dwain.c
dwain.c
SSCertifiable
SSCertifiable (7.7K reputation)SSCertifiable (7.7K reputation)SSCertifiable (7.7K reputation)SSCertifiable (7.7K reputation)SSCertifiable (7.7K reputation)SSCertifiable (7.7K reputation)SSCertifiable (7.7K reputation)SSCertifiable (7.7K reputation)

Group: General Forum Members
Points: 7711 Visits: 6431
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!
My temporal SQL musings: Calendar Tables, an Easter SQL, Time Slots and Self-maintaining, Contiguous Effective Dates in Temporal Tables
MMartin1
MMartin1
SSCrazy
SSCrazy (2.9K reputation)SSCrazy (2.9K reputation)SSCrazy (2.9K reputation)SSCrazy (2.9K reputation)SSCrazy (2.9K reputation)SSCrazy (2.9K reputation)SSCrazy (2.9K reputation)SSCrazy (2.9K reputation)

Group: General Forum Members
Points: 2921 Visits: 2031
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.

----------------------------------------------------
How to post forum questions to get the best help
gregs-1000765
gregs-1000765
SSC-Enthusiastic
SSC-Enthusiastic (126 reputation)SSC-Enthusiastic (126 reputation)SSC-Enthusiastic (126 reputation)SSC-Enthusiastic (126 reputation)SSC-Enthusiastic (126 reputation)SSC-Enthusiastic (126 reputation)SSC-Enthusiastic (126 reputation)SSC-Enthusiastic (126 reputation)

Group: General Forum Members
Points: 126 Visits: 1279
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


Go


Permissions

You can't post new topics.
You can't post topic replies.
You can't post new polls.
You can't post replies to polls.
You can't edit your own topics.
You can't delete your own topics.
You can't edit other topics.
You can't delete other topics.
You can't edit your own posts.
You can't edit other posts.
You can't delete your own posts.
You can't delete other posts.
You can't post events.
You can't edit your own events.
You can't edit other events.
You can't delete your own events.
You can't delete other events.
You can't send private messages.
You can't send emails.
You can read topics.
You can't vote in polls.
You can't upload attachments.
You can download attachments.
You can't post HTML code.
You can't edit HTML code.
You can't post IFCode.
You can't post JavaScript.
You can post emoticons.
You can't post or upload images.

Select a forum

































































































































































SQLServerCentral


Search