SQL Clone
SQLServerCentral is supported by Redgate
 
Log in  ::  Register  ::  Not logged in
 
 
 


SQL Grouping


SQL Grouping

Author
Message
caldrumr1234
caldrumr1234
Old Hand
Old Hand (327 reputation)Old Hand (327 reputation)Old Hand (327 reputation)Old Hand (327 reputation)Old Hand (327 reputation)Old Hand (327 reputation)Old Hand (327 reputation)Old Hand (327 reputation)

Group: General Forum Members
Points: 327 Visits: 24
I am having some issues with this.
There must be something I am formatting incorrectly.
I tried summing it in the outer query and the inner, the results are line by line (not summed).
Any ideas?

SELECT s.AgentName
, s.ReasonCode
, Sum(s.StateDuration) AS 'StateDuration'
FROM (
SELECT [Agent Name] AS 'AgentName'
, [Reason Code] AS 'ReasonCode'
, Cast(Datepart(Second, [Duration]) AS INT) + Cast(60 * Datepart(Minute, [Duration]) AS INT) + Cast(3600 * Datepart(Hour, [Duration]) AS INT) AS 'StateDuration'
FROM State_Details
GROUP BY [Agent Name]
, [Reason Code]
, [Duration]
) s
GROUP BY s.AgentName
, s.ReasonCode
, s.StateDuration

pietlinden
pietlinden
SSC-Dedicated
SSC-Dedicated (32K reputation)SSC-Dedicated (32K reputation)SSC-Dedicated (32K reputation)SSC-Dedicated (32K reputation)SSC-Dedicated (32K reputation)SSC-Dedicated (32K reputation)SSC-Dedicated (32K reputation)SSC-Dedicated (32K reputation)

Group: General Forum Members
Points: 32097 Visits: 15178
Remove the s.StateDuration from the GROUP BY. You're SUMMING that... Your query should look like this:
SELECT s.AgentName
, s.ReasonCode
, Sum(s.StateDuration) AS 'StateDuration'
FROM (
SELECT [Agent Name] AS 'AgentName'
, [Reason Code] AS 'ReasonCode'
, Cast(Datepart(Second, [Duration]) AS INT) + Cast(60 * Datepart(Minute, [Duration]) AS INT) + Cast(3600 * Datepart(Hour, [Duration]) AS INT) AS 'StateDuration'
FROM State_Details
GROUP BY [Agent Name]
, [Reason Code]
) s
GROUP BY s.AgentName
, s.ReasonCode

caldrumr1234
caldrumr1234
Old Hand
Old Hand (327 reputation)Old Hand (327 reputation)Old Hand (327 reputation)Old Hand (327 reputation)Old Hand (327 reputation)Old Hand (327 reputation)Old Hand (327 reputation)Old Hand (327 reputation)

Group: General Forum Members
Points: 327 Visits: 24
So simple. I was so close.
Thanks so much! I really appreciate it.
GilaMonster
GilaMonster
SSC Guru
SSC Guru (554K reputation)SSC Guru (554K reputation)SSC Guru (554K reputation)SSC Guru (554K reputation)SSC Guru (554K reputation)SSC Guru (554K reputation)SSC Guru (554K reputation)SSC Guru (554K reputation)

Group: General Forum Members
Points: 554360 Visits: 47746
There's no reason to have a subquery there.

  SELECT [Agent Name] AS AgentName
, [Reason Code] AS ReasonCode
, SUM(Cast(Datepart(Second, [Duration]) AS INT) + Cast(60 * Datepart(Minute, [Duration]) AS INT) + Cast(3600 * Datepart(Hour, [Duration]) AS INT)) AS StateDuration
FROM State_Details
GROUP BY [Agent Name]
, [Reason Code]


Gail Shaw
Microsoft Certified Master: SQL Server, MVP, M.Sc (Comp Sci)
SQL In The Wild: Discussions on DB performance with occasional diversions into recoverability

We walk in the dark places no others will enter
We stand on the bridge and no one may pass


Go


Permissions

You can't post new topics.
You can't post topic replies.
You can't post new polls.
You can't post replies to polls.
You can't edit your own topics.
You can't delete your own topics.
You can't edit other topics.
You can't delete other topics.
You can't edit your own posts.
You can't edit other posts.
You can't delete your own posts.
You can't delete other posts.
You can't post events.
You can't edit your own events.
You can't edit other events.
You can't delete your own events.
You can't delete other events.
You can't send private messages.
You can't send emails.
You can read topics.
You can't vote in polls.
You can't upload attachments.
You can download attachments.
You can't post HTML code.
You can't edit HTML code.
You can't post IFCode.
You can't post JavaScript.
You can post emoticons.
You can't post or upload images.

Select a forum








































































































































































SQLServerCentral


Search