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

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 25, 20262026-05-25T10:11:35+00:00 2026-05-25T10:11:35+00:00

Another newbie question here, I have the following Select: SELECT DATEADD(dd, 0, DATEDIFF(dd, 0,

  • 0

Another newbie question here,

I have the following Select:

SELECT    
    DATEADD(dd, 0, DATEDIFF(dd, 0, CIC.StatusDateTime)) AS ST_Date,
    CIC.UserId,
    MIN(CIC.StatusDateTime) as Beginning,
    MAX(CIC.EndDateTime) as ending,
    DATEPART(hour,MAX(CIC.EndDateTime) - MIN(CIC.StatusDateTime)) * 3600 
     + DATEPART(minute, MAX(CIC.EndDateTime) - MIN(CIC.StatusDateTime)) * 60 
     + DATEPART(second,MAX(CIC.EndDateTime) - MIN(CIC.StatusDateTime)) AS Login_Time, 
    SUM(CASE WHEN RS.AGT_STATE_GRP = 5 THEN StateDuration ELSE 0 END) AS Lunch, 
    SUM(CASE WHEN RS.AGT_STATE_GRP = 3 THEN StateDuration ELSE 0 END) AS Break_, 
    SUM(CASE WHEN RS.AGT_STATE_GRP = 4 THEN StateDuration ELSE 0 END) AS Coaching, 
    SUM(CASE WHEN RS.AGT_STATE_GRP = 10 THEN StateDuration ELSE 0 END) AS SYS_Down, 
    SUM(CASE WHEN RS.AGT_STATE_GRP = 7 THEN StateDuration ELSE 0 END) AS Training, 
    SUM(CASE WHEN RS.AGT_STATE_GRP = 6 THEN StateDuration ELSE 0 END) AS Meeting, 
    SUM(CASE WHEN RS.AGT_STATE_GRP = 11 THEN StateDuration ELSE 0 END) AS Default_
FROM dbo.CIC_AgentActivityLog_TMP AS CIC
INNER JOIN dbo.REF_AGT_STATES AS RS 
ON CIC.StatusKey = RS.AGT_STATE_DESC
Where CIC.StatusDateTime >='2011-09-01'
GROUP BY CIC.StatusDateTime, CIC.UserId, CIC.EndDateTime
ORDER BY ST_Date,UserID,StatusDateTime;

Using this, my results do not seem to be grouping at all. What I am trying to have is a single row with date,userid,firstlogin,lastlogin,totallogintime,and then a bunch of codes with their duration in seconds. I have something very similar for another data source which works fine.

I have tried converting and using cast with the first date thinking the 00:00:00 at the end might be conflicting, but to no avail. I am unsure what more info you guys might need, just let me know.

*Edit1 – My MIN(CIC.StatusDateTime) as Beginning,MAX(CIC.EndDateTime) as ending seem to not be helping. For an agent, i have three rows that all have different start and end times. The min and max are not giving me the actual min and max for the whole day.

Here is the source Data:

userID  StatusDateTime  StatusKey   EndDateTime StateDuration
bBarsby 9/1/2011 11:36  Out of the office   9/1/2011 11:36  0
bBarsby 9/1/2011 11:36  Out of the office   9/1/2011 11:36  5
bBarsby 9/1/2011 11:36  Available   9/1/2011 14:18  9711
bBarsby 9/1/2011 14:18  Away from desk  9/1/2011 14:39  1261
bBarsby 9/1/2011 14:39  Available   9/1/2011 19:51  18693
bBarsby 9/1/2011 19:51  Out of the office   9/1/2011 19:51  4
bBarsby 9/1/2011 19:51  Out of the office   9/1/2011 19:51  0

Here is the results

ST_Date UserID  Beginning   Ending  LoginTime   Lunch   Break   Coaching      Etc.
9/1/2011 0:00   bBarsby 9/1/2011 11:36  9/1/2011 14:18  9711    0   0   0   0
9/1/2011 0:00   bBarsby 9/1/2011 14:18  9/1/2011 14:39  1261    0   0   0   0
9/1/2011 0:00   bBarsby 9/1/2011 14:39  9/1/2011 19:51  18693   0   0   0   0
  • 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-25T10:11:36+00:00Added an answer on May 25, 2026 at 10:11 am

    Aaron Bertrand has the right idea. You absolutely should not define groups by the enddate.

    Here’s the query re-written with that group by.

    SELECT
      sub.ST_Date,
      sub.UserId,
      MIN(sub.StatusDateTime) as Beginning,
      MAX(sub.EndDateTime) as ending,
      DATEPART(hour,MAX(sub.EndDateTime) - MIN(sub.StatusDateTime)) * 3600
      + DATEPART(minute, MAX(sub.EndDateTime) - MIN(sub.StatusDateTime)) * 60
      + DATEPART(second,MAX(sub.EndDateTime) - MIN(sub.StatusDateTime)) AS Login_Time,
      SUM(CASE WHEN sub.AGT_STATE_GRP = 5 THEN StateDuration ELSE 0 END) AS Lunch,
      SUM(CASE WHEN sub.AGT_STATE_GRP = 3 THEN StateDuration ELSE 0 END) AS Break_,
      SUM(CASE WHEN sub.AGT_STATE_GRP = 4 THEN StateDuration ELSE 0 END) AS Coaching,
      SUM(CASE WHEN sub.AGT_STATE_GRP = 10 THEN StateDuration ELSE 0 END) AS SYS_Down,
      SUM(CASE WHEN sub.AGT_STATE_GRP = 7 THEN StateDuration ELSE 0 END) AS Training,
      SUM(CASE WHEN sub.AGT_STATE_GRP = 6 THEN StateDuration ELSE 0 END) AS Meeting,
      SUM(CASE WHEN sub.AGT_STATE_GRP = 11 THEN StateDuration ELSE 0 END) AS Default_
    FROM
    (
      SELECT 
        DATEADD(dd, 0, DATEDIFF(dd, 0, CIC.StatusDateTime)) AS ST_Date
        CIC.UserId,
        CIC.StatusDateTime,
        CIC.EndDateTime,
        RS.AGT_STATE_GRP,
        StateDuration
      FROM
        dbo.CIC_AgentActivityLog_TMP AS CIC
        JOIN dbo.REF_AGT_STATES AS RS
        ON CIC.StatusKey = RS.AGT_STATE_DESC
      WHERE CIC.StatusDateTime >='2011-09-01'
    ) sub
    GROUP BY sub.ST_Date, sub.UserId
    ORDER BY sub.ST_Date, sub.UserId
    
    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

Another basic Rails question: I have a database table that needs to contain references
Another easy one hopefully. Let's say I have a collection like this: List<DateTime> allDates;
Another poster asked about preferred syntax for infinite loops . A follow-up question: Why
Another question asked about determining odd/evenness in C, and the idiomatic (x & 1)
In another question I posted yesterday, I got very good advice on how a
In another question, Mark speaks highly of IDEs, saying some people still just dont
In another question on SO I answered with code like the one below and
In another Stack Overflow question Leon Timmermans asserted: I would advice you not to use
another request sorry.. Right now I am reading the tokens in one by one
another issue in IE 6... link The navbar link list has a border applied

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.