Please start any new threads on our new
site at https://forums.sqlteam.com. We've got lots of great SQL Server
experts to answer whatever question you can come up with.
Author |
Topic |
dikob
Starting Member
2 Posts |
Posted - 2006-08-18 : 00:53:16
|
Hi all i'm new here and just wanted to share if anyone could have any thoughts on my delima :((glad that i found this site :))i have these tables below:table Aff:Aff_ID | SiteName----------------------001 ------> yahoo002 ------> msn003 ------> espn... so ontable Affcamps:Aff_C | Aff_ID | Aff_Type_ID001 ---> 002 ------> 2222 ---> 007 ------> 3333 ---> 001 ------> 2002 ---> 007 ------> 2... so ontable AffAgg:Aff_ID | ecount | signcount | Concount | clickcount003 --------> 200 ------> 53 ------> 56 ------> 89001 --------> 647 ------> 64 ------> 66 ------> 87003 --------> 54 ------> 14 ------> 15 ------> 24002 --------> 34 ------> 5 ------> 4 ------> 3003 --------> 12 ------> 2 ------> 1 ------> 5... so onnow i was advise to sum the followingper Aff_ID w/ Aff_Type_ID = 2:1. sum(ecount) = ?2. sum(signcount) = ?3. sum(Concount) = ?4. sum(clickvount = ?my querry goes like this:SELECT af.SiteName,aa.Aff_ID,aa.Ecount,ab.Scount,ac.Ccount,ad.Kcount,ae.Aff_Type_IDFROMAff afINNER JOIN(SELECT Aff_ID, sum(ecount) AS EcountFROM AffAggGROUP BY Aff_ID) aaONaf.Aff_ID= aa.Aff_IDINNER JOIN(SELECT Aff_ID, sum(signcount) AS ScountFROM AffAggGROUP BY Aff_ID) abONaf.Aff_ID = ab.Aff_IDINNER JOIN(SELECT Aff_ID, sum(Concount) AS CcountFROM AffAggGROUP BY Aff_ID) acONaf.Aff_ID = ac.Aff_IDINNER JOIN(SELECT Aff_ID, sum(clickcount) AS kcountFROM AffAggGROUP BY Aff_ID) adONaf.Aff_ID = ad.Aff_IDINNER JOIN(SELECT DISTINCT Aff_ID, Aff_Type_IDFROM Affcamps where Aff_Type_ID=2) aeONaf.Aff_ID = ae.Aff_IDORDER BY aa.Aff_IDi'm sorry if my text is long but i just wanted to show the details of my request. we got 3+ mil rec. on our database thats why i need to omptimize my querry as fine as possible. my querry above runs ok but the DBA advised me to shorten my sql. i have 4 inner joins made because most of the fields i wanted to look for the sum is not indexed. i think only Aff_ID is indexed. Is there anyaway i could shorten my SQL syntax with at least the same quickness as my above querry.thanks your help is very much appreciated :)regards,dennisReply With Quote |
|
SwePeso
Patron Saint of Lost Yaks
30421 Posts |
Posted - 2006-08-18 : 03:40:17
|
Replace all of your query above with this oneSELECT a.SiteName, a.Aff_ID, ac.Aff_Type_ID, SUM(aa.Ecount) / COUNT(ac.Aff_ID) ECount, SUM(aa.SignCount) / COUNT(ac.Aff_ID) SCount, SUM(aa.ConCount) / COUNT(ac.Aff_ID) CCount, SUM(aa.ClickCount) / COUNT(ac.Aff_ID) KCountFROM Aff aINNER JOIN AffCamps ac ON ac.Aff_ID = a.Aff_IDINNER JOIN AffAgg aa ON aa.Aff_ID = a.Aff_IDWHERE ac.Aff_Type_ID = 2GROUP BY a.SiteName, a.Aff_ID, ac.Aff_Type_IDORDER BY a.Aff_ID Please let us know if there was a speed improvement.Peter LarssonHelsingborg, Sweden |
 |
|
dikob
Starting Member
2 Posts |
Posted - 2006-08-18 : 19:53:10
|
hi peter,thanks for your reply; I tried to use your method and it seems i cant get same result with what my query above display. Also it takes a long time to get the data. maybe because of how you inner join those tables that makes the difference. but anyhow i aprreciate your support on this. :)regards,dennis |
 |
|
madhivanan
Premature Yak Congratulator
22864 Posts |
Posted - 2006-08-21 : 01:07:06
|
Read about Cross-tab Reports in sql server help fileMadhivananFailing to plan is Planning to fail |
 |
|
|
|
|
|
|