How can I do "double-aggregation" in T-SQL / SQL Server?

483560002:
Sojoodi

I have a table which contains Issues and SubIssues in a customer service system. The data might look like this:

id ticket_id sub_ticket_id response_time_in_mins comment cond1 cond2 cond3
1 1000 NULL NULL "the overall ticket about how quickly you closed down my account" 1 0 1
2 1000 1 12 "send ack email" 1 0 1
3 1000 2 30 "look up user in all DBs and remove" 1 1 1
4 1000 3 5 "send finished email" 1 0 0
5 1001 NULL NULL "the overall ticket about do you have my email in your sys?" 1 0 1
6 1001 1 2 "send ack email" 0 0 1
7 1001 2 10 "look up and notify" 1 1 0
...

I'd like to aggregate total response times for each ticket_id satisfying each of the conditions at a time. For example the resulting table may look like this (but assume N conditions)

ticket_id total_resp_time_cond_1 total_resp_time_cond_2 total_resp_time_cond_3
1000 47 30 42
1001 10 10 2
...

Here it is in pseudo code if I could do Python+SQL:

foreach ticket_number in issues_and_subissues:
  foreach condition in cond1, cond2, cond3, ... condN:
    SELECT sum(response_time) FROM issues_and_subissues isst WHERE isst.ticket_id=ticket_number AND condition=1

Is there a T-SQL-only way to achieve this via JOINs? or would I have to use CURSERS? any other options?

I searched on Stackoverflow and beyond and all I could find was the CURSER way of doing it. Thank you!

Joel Coehoorn

It's called conditional aggregation. You do it by putting a CASE expression inside an aggregate function:

SELECT ticket_id,
    SUM(CASE WHEN cond1 = 1 THEN response_time_in_min ELSE 0 END) total_resp_time_cond_1,
    SUM(CASE WHEN cond2 = 1 THEN response_time_in_min ELSE 0 END) total_resp_time_cond_2,
    SUM(CASE WHEN cond3 = 1 THEN response_time_in_min ELSE 0 END) total_resp_time_cond_3   
FROM [table]
GROUP BY ticket_id

Collected from the Internet

Please contact [email protected] to delete if infringement.

edited at
0

Comments

0 comments
Login to comment

Related

How can I do an UPDATE statement with JOIN in SQL Server?

How can I do an in-memory SQL Server for integration testing?

C# - How can I do MS SQL Server Connection?

How can i do cumulative total in SQL Server?

How Can I do 1 Click Execute This T-SQL

I can't sign in to SQL Server 2014

How do I format a number in T-SQL for SQL Server 2008 R2?

How can I reshape a table in SQL Server?

How can I disable multithreading in SQL Server?

How can I truncate a datetime in SQL Server?

How can I solve this query in sql server?

How can I insert audio into SQL Server

How can I print this in sql server?

How can I write this SQL Server query?

How do I to use Pivot in SQL Server?

How can I do this SQL query correctly?

How can I do this in SQL/phpmyadmin?

How can i do a concat() in SQL 2008

How do I write this sql query in SQL Server?

How do I translate SQL SERVER Query into Oracle SQL?

SQL Server : how do I get SQL to recognize ö and ß correctly

How do I kill connections to Azure SQL database if I can't access it?

How can I do a switched outer join with more than one table in SQL Server 2012?

How can I do something like "STRING_AGG" in Sql Server Compact?

Can't figure out how to get the description field for the field I want in SQL Server 2008

How do I solve "couldn't be bound error" in SQL Server in my criteria?

SQL Server How do I change my view so it doesn't produces duplicates when inserting into a table?

Can I use LINQ to do a join on an IQueryable from SQL Server?

Why can't I generate an asymmetric key in SQL Server for a dll?