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

  • Home
  • SEARCH
  • 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 144817
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 11, 20262026-05-11T08:18:48+00:00 2026-05-11T08:18:48+00:00

I have a table that looks something like this CREATE TABLE MyTable( [RecordID] [bigint]

  • 0

I have a table that looks something like this

CREATE TABLE MyTable(     [RecordID] [bigint] IDENTITY(1,1) NOT NULL,     [PortName] [nvarchar](50) NULL,     [ReceivedEvent] [datetime] NULL,     [SentEvent] [datetime] NULL, ); 

The data could then be

   RecordID | PortName | ReceivedEvent       | SentEvent    1        | Port1    | 2009-10-20 10:20:00 | NULL    2        | Port2    | NULL                | 2009-10-20 10:10:00    3        | Port2    | NULL                | 2009-10-20 10:02:00    4        | Port2    | NULL                | 2009-10-20 11:00:00 

I now need two question over this table:

1) I need to get a count of number of rows grouped by port and date (not time). The date is however a parameter in the question (In this case it could be ‘2009-10-20’). I also want the last ‘activity’ in each hour

   NumberOfRows | PortName | Hour | LastActivityInHour     2            | Port2    | 10   | 2009-10-20 10:10:00    1            | Port2    | 11   | 2009-10-20 11:00:00    1            | Port1    | 10   | 2009-10-20 10:20:00 

2) I also need one question doing the same thing but grouped by day. And day is as I said a incoming parameter. In this case it could be ‘2009-10-20’

   NumberOfRows | PortName | LastActivityInDay     3            | Port2    | 2009-10-20 11:00:00    1            | Port1    | 2009-10-20 10:20:00 
  • 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. 2026-05-11T08:18:49+00:00Added an answer on May 11, 2026 at 8:18 am

    Since your data model is not properly normalized, this is uglier than it would need to be.

    SELECT   COUNT(t.RecordID) NumberOfRows,   t.PortName,   t.EventHour,   MAX(t.EventDateTime) LastActivityInHour FROM   (     SELECT       RecordID,       PortName,       COALESCE(ReceivedEvent, SentEvent) EventDateTime,       DATEPART(hh, COALESCE(ReceivedEvent, SentEvent)) EventHour,       DATEADD(dd, 0, DATEDIFF(dd, 0, COALESCE(ReceivedEvent, SentEvent))) EventDate     FROM       MyTable   ) t WHERE   t.EventDate = DATEADD(dd, 0, DATEDIFF(dd, 0, @TheDateInQuestion)) GROUP BY   t.PortName,   t.EventHour ORDER BY   t.PortName,   t.EventHour 

    The SQL to do the by-date grouping is very similar, I’ll leave deriving it as an exercise for you. 😉

    I’m sure that there are other ways to get the same result. Making a view out of what is the inner query here will help clean up the query as a whole.

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

Sidebar

Ask A Question

Stats

  • Questions 189k
  • Answers 189k
  • Best Answers 0
  • User 1
  • Popular
  • Answers
  • Editorial Team

    How to approach applying for a job at a company ...

    • 7 Answers
  • Editorial Team

    What is a programmer’s life like?

    • 5 Answers
  • Editorial Team

    How to handle personal stress caused by utterly incompetent and ...

    • 5 Answers
  • Editorial Team
    Editorial Team added an answer Revised answer The amended code you provide compiles almost cleanly… May 12, 2026 at 5:52 pm
  • Editorial Team
    Editorial Team added an answer Edit: For whatever reason, a change event is invoked when… May 12, 2026 at 5:52 pm
  • Editorial Team
    Editorial Team added an answer You can create a Formula field in Crystal and use… May 12, 2026 at 5:52 pm

Related Questions

I have a couple of tables which are used to log user activity for
I have a fairly complex query that looks something like this: create table Items(SomeOtherTableID
Lets say I have a table containing several hundred million rows that looks something
Although I have experience with SQL and generating HTML reports with PHP, I'm a

Trending Tags

analytics british company computer developers django employee employer english facebook french google interview javascript language life php programmer programs salary

Top Members

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.