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 8709871
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 13, 20262026-06-13T04:28:47+00:00 2026-06-13T04:28:47+00:00

I have the following information (return by view): DateTime ItemID UserTyp Seconds 2012-01-01 10

  • 0

I have the following information (return by view):

DateTime   ItemID  UserTyp Seconds

2012-01-01 10      S       12      

2012-01-01 10      S       18  

2012-01-01 11      S       22  

2012-01-02 11      M       52  

2012-01-02 10      S       120  

2012-01-02 11      S       NULL  

2012-01-03 15      M       112  

2012-01-03 12      S       182  

2012-01-04 10      M       NULL  

What I need to done is to calculated the sum of all seconds by user types, but for five periods.

The periods are set in the following way:

  1. Get the first and the last date
  2. Divided all days on five
  3. The result of the division is the days that one period includes

Then for each period, depending on the DateTime and the UserType of the records, I should come up with this records:

Periods  UserTypeS_SUM(Seconds) UserTypeM_SUM(Seconds)
1        
2        
3
4
5

Also, I should check if there is at least 5 records, before the division is make – if I have 4 for example, only one period will be used.

I know it does not look so different, I have started to make a solution using function, but it looks kinda ineffective to me. Are there some build-in functions that could do this easy?

EDIT: I am really sorry but I have forget to mention that the I should this in view or table-valued function. No store procedures are allowed in this case.

  • 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-06-13T04:28:49+00:00Added an answer on June 13, 2026 at 4:28 am

    try this:

    ;with cte as(
    select *,NTILE(5) over(order by [DateTime]) as period 
    from Table1
    ),
    type_S as( select period,SUM([Seconds]) as UserTypeS_SUM
     from cte
     where [UserTyp]='S'
     group by period),
    type_M as( select period,SUM([Seconds]) as UserTypeM_SUM
     from cte
     where [UserTyp]='M'
     group by period),
     seq as (
      select 1 as sno union all
      select 2 union all
      select 3 union all
      select 4 union all
      select 5  )
     select  seq.sno as period,UserTypeS_SUM, UserTypeM_SUM
     from seq 
     left join  type_s s
     on seq.sno=s.period
     left join type_m m
     on seq.sno=m.period
    

    SQL fiddle demo

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

Sidebar

Related Questions

I have the following code in a view to get some of the information
I have the following information return in the grid, but it would not order
In my mysql database I have the following information in a page name field.
I have a file with the following information: dog<> cat<> cow<> bird<> tiger<> lion<>
I have multiple data entries that contain the following information: id_number name1 date name2
I have a DLL that I can use to pull the following information about
Ok, I have an example table with the following information and query. First up
I have the following function in ruby for retrieving certain information from a database.
I have the following formatted sample string: == header == information about things ==headeragain==
I have the following scenarios: Scenario: Create a game with valid information Given I

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.