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

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 1, 20262026-06-01T15:34:13+00:00 2026-06-01T15:34:13+00:00

I have a database table of events. Each event has a category and a

  • 0

I have a database table of events. Each event has a category and a date/time.

(actually events have sub-categories which are contained in categories and various other properties, left out in this simplified example)

I want to generate a list with an entry for each hour and category, listing the number of events and their percentage.

date            | category | number of events | percentage
----------------------------------------------------------
2012-01-01 1:00 | cat-1    | 10               | 50%
2012-01-01 1:00 | cat-2    | 10               | 50%
2012-01-01 2:00 | cat-1    | 1                | 10%
2012-01-01 2:00 | cat-2    | 9                | 90%
2012-01-01 3:00 | cat-1    | 3                | 100%
...

I am having trouble calculating the percentage efficiently.
Here is what I have so far:

var results = datacontext.events.
  groupBy(e=>new   
  {  // group by category and hour
     category = e.category,
     datetime = new DateTime (
       e.datetime.Year, e.datetime.Month, e.datetime.Day, 
       e.datetime.Hour, 0, 0 ) 
     // use date+hour for grouping, ignore minutes/seconds
  }).
  select(group => new 
  {
     category = group.Key.category,
     datetime = group.Key.datetime,
     numberOfEvents = group.count(),
     percentage = group.count() / ["total number of events for this hour"] * 100.0
  }

How can I get the value for [total number of events for this hour], ideally in an elegant and efficient way?

  • 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-01T15:34:14+00:00Added an answer on June 1, 2026 at 3:34 pm

    Here is what I came up with:

            var results = from events in datacontext.events
                          let date = new DateTime(
                                 events.datetime.Year, events.datetime.Month, events.datetime.Day,
                                 events.datetime.Hour, 0, 0)
                          let total = datacontext.events.Where(x => x.datetime.Date == date.Date && x.datetime.Hour == date.Hour).Count()
                          group events by new
                          {  // group by category and hour
                              category = events.category,
                              datetime = date,
                              total = total
                          } into e
                          select new
                          {
                              category = e.Key.category,
                              day = e.Key.datetime.Day,
                              numberOfEvents = e.Count(),
                              percentage = (float)e.Count() / (float)e.Key.total
                          };
    

    Two points of focus:

    1. Using floats for percentage
    2. using let to calculate the total for the Date
    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

I have a PostgreSQL database with events. Each event has a datetime or an
My application has one table called 'events' and each event has approx 30 standard
I have two entities: User and Event. Each user has a list of events
I have a database of events which gets updated every night. A single event
I have a table (Dataview) with content from a database, where each row/object has
I have two entities User and Event. Each user has a list of events
I have a database table that represents Events. The table has 2 main fields
i have database table like this +-------+--------------+----------+ | id | ip | date |
I have one database table which contains 8 columns. One of the columns is
I have an Event database loaded into Core Data that has duplicate Event titles.

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.