daily weekly monthly calculate sum

  • 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

  • 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.

  • 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

  • Please reread Anthony Green's post.

    The absence of evidence is not evidence of absence
    - Martin Rees
    The absence of consumable DDL, sample data and desired results is, however, evidence of the absence of my response
    - Phil Parkin

  • 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![/I]

    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?[/url]
    Since random numbers are too important to be left to chance, let's generate some![/url]
    Learn to understand recursive CTEs by example.[/url]
    [url url=http://www.sqlservercentral.com/articles/St

  • 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.

    ----------------------------------------------------

  • 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

    WHEREtimecreated >= @startdate

    ANDtimecreated <= @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

    WHEREtimecreated >= @startdate

    ANDtimecreated <= @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

    WHEREtimecreated >= @startdate

    ANDtimecreated <= @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

    --FROMdailySums

    /* weekly cte statement */

    --SELECT[state], AgentName, Agenttype, weeknumber, WeeklyAmount

    --FROMweeklySums

    /* monthly cte statement */

    --SELECT[state], AgentName, Agenttype, monthnumber, MonthlyAmount

    --FROMmonthlySums

    ---- 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

    SELECTd.[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 ???? )

    FROMdailySums d-- first cte statement

    INNER JOINweeklySums w-- second cte statement

    ONd.[state] = w.[state]

    ANDd.AgentName = w.AgentName

    ANDd.Agenttype = w.Agenttype

    ANDd.wk = w.weeknumber-- number value of the week

    INNER JOINmonthlySums m-- third cte statement

    ONd.[state] = m.[state]

    ANDd.AgentName = m.AgentName

    ANDd.Agenttype = m.Agenttype

    ANDd.mm = m.monthnumber-- number value of the year

Viewing 7 posts - 1 through 6 (of 6 total)

You must be logged in to reply to this topic. Login to reply