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

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 25, 20262026-05-25T17:29:47+00:00 2026-05-25T17:29:47+00:00

I have a table for logging method calls. It has LogId, MethodId, DateTime columns.

  • 0

I have a table for logging method calls. It has LogId, MethodId, DateTime columns.

I need to write a select statement that counts all logs for specific method IDs over a specific time period and also show the number of logs for the specific methods over a different time period.

The first bit is simple:

select
    l.[MethodId],
    count(l.[LogId]) as [Count]
from
    [Log] l (nolock)
where
    l.[DateTime] between @from and @to
    and l.[MethodId] in @methodIds
group by
    l.[MethodId]

But now I need a second column in that table, Previous, which would look like this if it was in a separate statement:

select
    l.[MethodId],
    count(l.[LogId]) as [Previous]
from
    [Log] l (nolock)
where
    l.[DateTime] between @before and @from 
    and l.[MethodId] in @methodIds
group by
    l.[MethodId]

Not all methods will will have logs for the two time periods, so would be nice if the join would insert 0 in the count/previous columns in those cases instead of them being null. It’s ok if a method doesn’t have any logs in either periods.

What I want to see is MethodId, Count, Previous in one table. How do I make this happen?

  • 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-25T17:29:48+00:00Added an answer on May 25, 2026 at 5:29 pm

    Something like:

    select 
        l.[MethodId], 
        sum(case when datetime between @from and @to then 1 else 0 end) as count,
        sum(case when datetime between @before and @from then 1 else 0 end) as previous
    from 
        [Log] l
    where 
        l.[DateTime] between @before and @to 
        and l.[MethodId] in @methodIds 
    group by 
        l.[MethodId] 
    

    The BETWEEN clause in the where doesn’t affect the output then, but it might affect performance if you have an index on datetime. And if this table can get big, you probably should have such an index.

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

Sidebar

Related Questions

I have a logging table which has three columns. One column is a unique
I have the following need I have a logging table which logs som leads
I use SQL Server 2005. I have a simple logging table that my web
I have an application that records activity in a table (Oracle 10g). The logging
I have a situation where I have a table that has a soft foreign
I have a simple data logging table with two data columns A and B
I have an Oracle table which has a name,value,time columns.Basically the table is for
I have a table wchih has 2 columns. The definition is CREATE TABLE LOGGING_T
We have a project where data is written to a logging table. Now, when
I have table inside a div tab. The table has 40 rows in it

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.