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

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 5, 20262026-06-05T08:20:41+00:00 2026-06-05T08:20:41+00:00

SELECT u.FirstName + ‘ ‘ + u.LastName as ‘User’, COUNT(*) as ‘Number of Calls’,

  • 0
    SELECT      u.FirstName + ' ' + u.LastName as 'User',
            COUNT(*) as 'Number of Calls',
            CONVERT(varchar(4), SUM(CASE WHEN c.FromTime BETWEEN @FromDate AND @ToDate
                THEN DATEDIFF(mi, c.FromTime, c.ToTime) ELSE 0 END) / 60) + ':' + 
            CASE WHEN SUM(CASE WHEN c.FromTime BETWEEN @FromDate AND @ToDate
                THEN DATEDIFF(mi, c.FromTime, c.ToTime) ELSE 0 END) % 60 < 10 THEN '0' ELSE '' END +
            CONVERT(varchar(2), SUM(DATEDIFF(mi, c.FromTime, c.ToTime)) % 60) as 'Total Time Spent',
            CONVERT(varchar(4), AVG(DATEDIFF(mi, c.FromTime, c.ToTime)) / 60) + ':' +
            CASE WHEN AVG(DATEDIFF(mi, c.FromTime, c.ToTime)) % 60 < 10 THEN '0' ELSE '' END +
            CONVERT(varchar(2), AVG(DATEDIFF(mi, c.FromTime, c.ToTime)) % 60) as 'Average Call Time'
FROM        Calls c
JOIN        Users u ON u.UserID = c.TakenBy
WHERE       c.FromTime BETWEEN @FromDate AND @ToDate
GROUP BY    u.UserID, u.FirstName, u.LastName
ORDER BY    u.FirstName + ' ' + u.LastName

The preceding SQL query returns the correct “Number of Calls” but the “Total Time” and “Average Time” are always the same regardless of the # of calls (which is obviously wrong).

I’ve read and tried to implement using the CASE WHEN __ Then value ELSE 0 inside SUM but it still returns an incorrect value.

The only way I can get this query to return correct results is if I completely strip out all other info, e.g.

SELECT SUM(DATEDIFF(mi, FromTime, ToTime)) FROM Calls WHERE c.FromTime BETWEEN...

How can I still use my JOIN and GROUP BY and get the aggregate functions to give me the results I want?

Thanks for any and all help!

  • 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-05T08:20:43+00:00Added an answer on June 5, 2026 at 8:20 am

    You’re probably better off with a subquery, e.g. in the SELECT part, add something like (SELECT SUM(...) FROM ... WHERE ...) AS total_sum.

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

Sidebar

Related Questions

This query SELECT FirstName, LastName, NCAAStats.AccountId, College_Translator.school_name, StatTypeId, COUNT(*) AS 'Count' FROM NCAAstats INNER
I Want convert bit type to Yes or No For Example: SELECT FirstName, LastName,
try { PreparedStatement s = (PreparedStatement) conn.prepareStatement(SELECT voters.Check,count(*) FROM voting.voters where FirstName=+first+and LastName=+last+ and
How do I get to make this sqsl query work: SELECT student.LASTNAME, student.FIRSTNAME, student.IDNO,
I have a simple query in php on oracle: $query='select u.username,u.lastname,u.firstname,c.event,c.reason from users u,
I'm looking for a way to do this ... SELECT FirstName, LastName, Split(AddressBlock, '
public class User { public string FirstName { get; set; } public string LastName
Is their a reason I cannot do Select Firstname + Lastname from Person When
I have a SQL select statement like this: select FirstName, LastName, Age from People
this SQL statement select firstname,lastname from users where lastname in('foo','bar') group by firstname 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.