Solved

Rewriting query for performance

Posted on 2012-03-25
Medium Priority
155 Views
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
Question by:himabindu_nvn
[X]
Welcome to Experts Exchange

Add your voice to the tech community where 5M+ people just like you are talking about what matters.

• Help others & share knowledge
• Earn cash & points

LVL 75

Expert Comment

ID: 37763841
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
``````
0

LVL 29

Accepted Solution

Olaf Doschke earned 2000 total points
ID: 37764510
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
``````

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'
``````

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

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

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…
For both online and offline retail, the cross-channel business is the most recent pattern in the B2C trade space.
Familiarize people with the process of retrieving data from SQL Server using an Access pass-thru query. Microsoft Access is a very powerful client/server development tool. One of the ways that you can retrieve data from a SQL Server is by using a pa…
Via a live example, show how to set up a backup for SQL Server using a Maintenance Plan and how to schedule the job into SQL Server Agent.
Suggested Courses
Course of the Month12 days, 16 hours left to enroll