Sign Up

Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.

Have an account? Sign In

Have an account? Sign In Now

Sign In

Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.

Sign Up Here

Forgot Password?

Don't have account, Sign Up Here

Forgot Password

Lost your password? Please enter your email address. You will receive a link and will create a new password via email.

Have an account? Sign In Now

You must login to ask a question.

Forgot Password?

Need An Account, Sign Up Here

Please briefly explain why you feel this question should be reported.

Please briefly explain why you feel this answer should be reported.

Please briefly explain why you feel this user should be reported.

Sign InSign Up

The Archive Base

The Archive Base Logo The Archive Base Logo

The Archive Base Navigation

  • SEARCH
  • Home
  • About Us
  • Blog
  • Contact Us
Search
Ask A Question

Mobile menu

Close
Ask a Question
  • Home
  • Add group
  • Groups page
  • Feed
  • User Profile
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Buy Points
  • Users
  • Help
  • Buy Theme
  • SEARCH
Home/ Questions/Q 4625804
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 22, 20262026-05-22T03:19:31+00:00 2026-05-22T03:19:31+00:00

I need to calculate sum of hours based on period column and insert into

  • 0

I need to calculate sum of hours based on period column and insert into another table. Please see below example data.
[code]

tbl_Summary

    Client_id   Store_id    attribute1  Period    Hours     attributes
1              16             1        2/25/2007  1054.8     1
1              16             1        3/11/2007  1112.8     1
1              16             1        3/25/2007  1164.8     1
1              16             1        4/8/2007   1383.2     1
1              16             1        4/22/2007  1351.6     1
1            16       1          9/21/2008      1523.6       1**
1            16       1         10/5/2008      1020.26   1
1            16       1         10/19/2008      939.94   1
1            16       1         11/2/2008       903.14   1
1            16       1         8/9/2009        866.66   1
1            16       1         8/23/2009       915.48   1
1            16       1         9/6/2009        894.26   1
1            16       1         9/20/2009      1458.58   1[/b]
1              18             1        6/1/2008   1112.8     1
1              18             1        6/15/2008  1164.8     1
1              18             1        6/29/2008  1383.2     1 [b]
1            18        1     7/13/2008 1351.6    1
1            18        1    12/28/2008 1523.6    1
1            18        1     1/11/2009  979.2    1
1            18        1     1/25/2009  913.2    1
1            18        1     2/8/2009   930.6    1
1            18        1     2/22/2009 1143.4    1
1            18        1     5/31/2009 1066.16   1
1            18        1     6/14/2009  1174.8   1
1            18        1     6/28/2009  1099.2   1
1            18        1     7/12/2009  1014.5   1 

Out put table will be like this.

tbl_history
--------------
[code]client_id store_id    attribute1  hours   attributes
1                    16          1         8521.92    1
1                    18          1        11196.92    1

Conditions for sp

  1. Parameters to sp are only @client_id, @attribute1, @attributes

  2. Find Max(Period) and go back to 52 weeks and calculate Sum(Hours) where Store_id= ? (in example 16 and 18 or each store_id) and client_id= @client_id and attribute1 = @attribute1 and attributes = @attributes. ie. Sum(Hours) will change based on store_id and when store_id will change period will change. See side moved data

store_id = 16 and period=9/20/2009 to 9/20/2009 sum(hours) =8521.92

  1. Insert into another table all output tbl_history.

Please give me solution. If you have any question ask me.

Thanks in advance

  • 1 1 Answer
  • 0 Views
  • 0 Followers
  • 0
Share
  • Facebook
  • Report

Leave an answer
Cancel reply

You must login to add an answer.

Forgot Password?

Need An Account, Sign Up Here

1 Answer

  • Voted
  • Oldest
  • Recent
  • Random
  1. Editorial Team
    Editorial Team
    2026-05-22T03:19:32+00:00Added an answer on May 22, 2026 at 3:19 am
    INSERT INTO tbl_history
    SELECT client_id, store_id, attribute1, SUM(Hours), attributes
    FROM tbl_Summary
    WHERE PERIOD <=
    (
        SELECT MAX(PERIOD)
        FROM tbl_Summary
        WHERE client_id = @client_id
        AND attribute1 = @attribute1
        AND attributes = @attributes
    )
    AND PERIOD  >= 
    (
        SELECT (MAX(PERIOD) - 1 year) --(I dont remember the sintax for sql getdate() something but thats the idea)
        FROM tbl_Summary
        WHERE client_id = @client_id
        AND attribute1 = @attribute1
        AND attributes = @attributes
    )
    AND client_id = @client_id
    AND attribute1 = @attribute1
    AND attributes = @attributes
    GROUP BY client_id, store_id, attribute1, attributes
    

    Try it and tell me if it worked.

    Regards.

    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

I have a table of data similar to below where I need to calculate
I have a table with dynamic created rows. Now I need to calculate sum
I need to calculate the net total of a column-- sounds simple. The problem
I need to calculate date (year, month, day) which is (for example) 18 working
I need to calculate the sum of the 4 integers I have inside a
I need to calculate some direction arrays in numpy. I divided 360 degrees into
I need to calculate the sum of stock from some rows, the same product
I have a dictionary of (string, decimal) and need to calculate the sum of
I want to join three tables and to calculate the Sum(Quantity) of the Table
My query is working, but I need to calculate the total of a column

Explore

  • Home
  • Add group
  • Groups page
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Users
  • Help
  • SEARCH

Footer

© 2021 The Archive Base. All Rights Reserved
With Love by The Archive Base

Insert/edit link

Enter the destination URL

Or link to existing content

    No search term specified. Showing recent items. Search or use up and down arrow keys to select an item.