Solved

Rewriting query for performance

Posted on 2012-03-25
2
149 Views
Last Modified: 2012-07-24
I have a query .
table_1
clientid memberid  denom num
a              x                  1        1
a              y                  1        1
b             z                   0        1

table_2
clientid memberid type
a              x               c
a              y               s
b              z               c

I need to select values where denom=1 and type='c' from the two tables. The denominator value should be sum(denom) where type='c' and denom=1.
denom from table_1 is a flag. I wrote a sub query here. But there are many fields like numerator1,numerator2,etc. So I wrote many subqueries . I dont want to use subqueries here as there is much data. Can anyone suggest me a query which is much efficient than subqueries
select clientid,denominator=(select sum(denom) from table_1 t1
                                      join table_2 t2 on t1.clientid=t2.clientid
                                      where t2.type='C' and denom=1)
from dbo.table_1 t1
join table_2 t2
on t1.clietid=t2.clientid
where t2.type='C'
group by e.lob
0
Comment
Question by:himabindu_nvn
2 Comments
 
LVL 75

Expert Comment

by:Anthony Perkins
Comment Utility
Something like this:
SELECT  t1.clientid,
        SUM(t1.denom)
FROM    table_1 t1
        JOIN table_2 t2 ON t1.clientid = t2.clientid
WHERE   t2.type = 'C'
GROUP BY t1.clientid
HAVING  SUM(t1.denom) = 1

Open in new window

0
 
LVL 29

Accepted Solution

by:
Olaf Doschke earned 500 total points
Comment Utility
It depends what you need, maybe even simpler

SELECT  t1.clientid,
        SUM(t1.denom) as SumDenom, sum(t1.numerator1) as SumNumerator1,...
FROM    table_1 t1
        INNER JOIN table_2 t2 ON t1.clientid = t2.clientid and t2.type='c'
GROUP BY t1.clientid

Open in new window


I'd recommend you first do a query
SELECT  t1.*
FROM    table_1 t1
        INNER JOIN table_2 t2 ON t1.clientid = t2.clientid and t2.type='c'

Open in new window


and inspect if you want the sums of this query result. If the join condition differs for each of the sums you want to make, there could still be ways of summing on condition using CASE.

I suspect you also want to add t1.memberid = t2.memberid in your join condition, otherwise you sum same records of t1 multiple times. Not with your sample data, but there might be cases you join records of other members also of type 'c'.

Bye, Olaf.
0

Featured Post

How to improve team productivity

Quip adds documents, spreadsheets, and tasklists to your Slack experience
- Elevate ideas to Quip docs
- Share Quip docs in Slack
- Get notified of changes to your docs
- Available on iOS/Android/Desktop/Web
- Online/Offline

Join & Write a Comment

Suggested Solutions

Title # Comments Views Activity
SQL FUNCTION TO Capitalize Each Word in a String 10 59
Complex SQL 10 32
Report Builder 9 24
Access Migration to Sql Server 2 7
Introduced in Microsoft SQL Server 2005, the Copy Database Wizard (http://msdn.microsoft.com/en-us/library/ms188664.aspx) is useful in copying databases and associated objects between SQL instances; therefore, it is a good migration and upgrade tool…
The Delta outage: 650 cancelled flights, more than 1200 delayed flights, thousands of frustrated customers, tens of millions of dollars in damages – plus untold reputational damage to one of the world’s most trusted airlines. All due to a catastroph…
This video shows how to set up a shell script to accept a positional parameter when called, pass that to a SQL script, accept the output from the statement back and then manipulate it in the Shell.
Via a live example combined with referencing Books Online, show some of the information that can be extracted from the Catalog Views in SQL Server.

772 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question

Need Help in Real-Time?

Connect with top rated Experts

12 Experts available now in Live!

Get 1:1 Help Now