Year to Month Sum

Collapse
X
 
  • Time
  • Show
Clear All
new posts
  • Ph0b0s
    New Member
    • Mar 2008
    • 3

    Year to Month Sum

    Hello,
    My problem is to summarize
    Year to Month Orders
    For example I have a table like this :
    [Date] ------------------------------------[Orders]
    2007-01-31 00:00:00.000------------- 2
    2007-02-28 00:00:00.000------------- 8
    2007-03-31 00:00:00.000------------- 7
    2007-04-30 00:00:00.000------------- 1

    and my need is to sum each month's Orders to next one
    to Look like this :

    2007-01-31 00:00:00.000------------- 2
    2007-02-28 00:00:00.000------------- 10
    2007-03-31 00:00:00.000------------- 17
    2007-04-30 00:00:00.000------------- 18

    Thanks in advance .
  • deepuv04
    Recognized Expert New Member
    • Nov 2007
    • 227

    #2
    Originally posted by Ph0b0s
    Hello,
    My problem is to summarize
    Year to Month Orders
    For example I have a table like this :
    [Date] ------------------------------------[Orders]
    2007-01-31 00:00:00.000------------- 2
    2007-02-28 00:00:00.000------------- 8
    2007-03-31 00:00:00.000------------- 7
    2007-04-30 00:00:00.000------------- 1

    and my need is to sum each month's Orders to next one
    to Look like this :

    2007-01-31 00:00:00.000------------- 2
    2007-02-28 00:00:00.000------------- 10
    2007-03-31 00:00:00.000------------- 17
    2007-04-30 00:00:00.000------------- 18

    Thanks in advance .
    Hi,
    Use the following query

    [Code=sql]
    select CurMon.Date, isnull(CurMon.O rders,0) + isnull(PrevMon. Orders,0)
    from YearSum as CurMon
    LEFT OUTER JOIN YearSum as NextMon
    ON Month(CurMon.Da te) = Month(NextMon.D ate) - 1
    LEFT OUTER JOIN YearSum as PrevMon
    ON Month(CurMon.Da te) = Month(PrevMon.D ate) + 1
    [/code]

    Comment

    • Ph0b0s
      New Member
      • Mar 2008
      • 3

      #3
      Originally posted by deepuv04
      Hi,
      Use the following query

      [Code=sql]
      select CurMon.Date, isnull(CurMon.O rders,0) + isnull(PrevMon. Orders,0)
      from YearSum as CurMon
      LEFT OUTER JOIN YearSum as NextMon
      ON Month(CurMon.Da te) = Month(NextMon.D ate) - 1
      LEFT OUTER JOIN YearSum as PrevMon
      ON Month(CurMon.Da te) = Month(PrevMon.D ate) + 1
      [/code]
      thanks 4 your answer it was exactly what i asked.
      and it was not what i wonted in the first place.... Sorry
      I want every month to summarize the priv month sum :-| .
      Ex: 1 month (1)
      2nd month (3)
      the record of the 2nd month should be (4) ,
      3rd month (5)
      the record of the 3nd month should be (9)etc..

      thanks

      Comment

      • deepuv04
        Recognized Expert New Member
        • Nov 2007
        • 227

        #4
        Originally posted by Ph0b0s
        thanks 4 your answer it was exactly what i asked.
        and it was not what i wonted in the first place.... Sorry
        I want every month to summarize the priv month sum :-| .
        Ex: 1 month (1)
        2nd month (3)
        the record of the 2nd month should be (4) ,
        3rd month (5)
        the record of the 3nd month should be (9)etc..

        thanks
        try the following query
        use appropriate table names and column names

        [code=sql]
        SELECT Date1,sum(Order s2)
        from(
        SELECT DISTINCT * --,DATEDIFF(MM,T2 .DATE2,T1.DATE1 )
        FROM
        ( SELECT Date AS DATE1,Orders AS ORDERS1
        FROM Table_Name ) AS T1 ,
        ( SELECT Date AS DATE2,Orders AS ORDERS2
        FROM Table_Name ) AS T2
        WHERE DATEDIFF(MM,T2. DATE2,T1.DATE1) >= 0
        ) as s
        group by date1
        [/code]

        Comment

        • Ph0b0s
          New Member
          • Mar 2008
          • 3

          #5
          Originally posted by deepuv04
          try the following query
          use appropriate table names and column names

          [code=sql]
          SELECT Date1,sum(Order s2)
          from(
          SELECT DISTINCT * --,DATEDIFF(MM,T2 .DATE2,T1.DATE1 )
          FROM
          ( SELECT Date AS DATE1,Orders AS ORDERS1
          FROM Table_Name ) AS T1 ,
          ( SELECT Date AS DATE2,Orders AS ORDERS2
          FROM Table_Name ) AS T2
          WHERE DATEDIFF(MM,T2. DATE2,T1.DATE1) >= 0
          ) as s
          group by date1
          [/code]
          thank you very much!

          Comment

          Working...