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 |
qwertyjjj
Posting Yak Master
131 Posts |
Posted - 2006-09-20 : 07:11:15
|
HiI am trying to join between 3 tables and group some data to get totals.At the moment I have a query:SELECT S.[Company Description], IIf(Len([Company Code])=2,"00" & [Company Code],[Company Code]) AS Expr1, ((Sum([1To90])+Sum([Not Due])+Sum([91To120]))/1000) AS Months0To3, (Sum(baddebt_CURR.BadDebt)) AS BADDebtFROM ((SELECT [Company Code], [Company Description] FROM Structure GROUP BY [Company Code], [Company Description]) AS SLEFT JOIN zarageddebt_CURR ON S.[Company Code] = zarageddebt_CURR.CoCd)LEFT JOIN baddebt_CURR ON S.[Company Code] = baddebt_CURR.CoCdGROUP BY S.[Company Description], IIf(Len([Company Code])=2,"00" & [Company Code],[Company Code])The problem is the 3rd join onto the baddebt_CURR table. It is drastically multiplying the figures; I assume because of all the records in the 2nd table. What I want to do is join from S to zarageddebt_CURR and then from S to baddebt_CURR separately to group the figures.ANy idea on how to do this ? |
|
chiragkhabaria
Master Smack Fu Yak Hacker
1907 Posts |
Posted - 2006-09-20 : 07:51:45
|
Are you using SQL Server?Chirag |
 |
|
JoeNak
Constraint Violating Yak Guru
292 Posts |
Posted - 2006-09-20 : 08:10:48
|
Looks like it's correct. The LEFT JOINs will give you all (distinct) records from the Structure table. If this isn't what you're looking for perhaps you could explain further or post some data/results. |
 |
|
qwertyjjj
Posting Yak Master
131 Posts |
Posted - 2006-09-20 : 09:03:18
|
Using Access, which is confusing me a bit due to all the brackets it puts round joins.As an example...say I have the following dataStructure table:CompanyCode12zarageddebt_CURR tabeCompanyCode Total12 10012 100baddebt_CURR table:CompanyCode Total12 1012 10When I join structure to zarageddebt_CURR and sum the total, grouped by the company code, I get a result of 200.When I join Structure to zarageddebt_CURR and baddebt_CURR I get a result of 400 when summing and grouping.I think this is because it joins on the company code twice from the zarageddebt_CURR table but I can't seem to bypass this effect.Does make sense? Let me know if not and I can post up the DB or something... |
 |
|
SwePeso
Patron Saint of Lost Yaks
30421 Posts |
Posted - 2006-09-20 : 09:19:27
|
[code]SELECT s.[Company Description], RIGHT('0000' + [Company Code], 4) AS Expr1, (Sum([1To90]) + Sum([Not Due]) + Sum([91To120])) / 1000.0) AS Months0To3, Sum(baddebt_CURR.BadDebt) AS BADDebtFROM ( SELECT [Company Code], [Company Description] FROM Structure GROUP BY [Company Code], [Company Description] ) AS SLEFT JOIN zarageddebt_CURR ON S.[Company Code] = zarageddebt_CURR.CoCdLEFT JOIN baddebt_CURR ON S.[Company Code] = baddebt_CURR.CoCdGROUP BY s.[Company Description], RIGHT('0000' + [Company Code], 4)[/code]Peter LarssonHelsingborg, Sweden |
 |
|
qwertyjjj
Posting Yak Master
131 Posts |
Posted - 2006-09-20 : 09:25:51
|
HiThat is the same problem because as soon as you join onto the baddebt_CURR table, it duplicates the figures due to the join. Try it with the 3 tables and the data above - you get 400 instead of 200...and 40 instead of 20 for the bad debt. |
 |
|
SwePeso
Patron Saint of Lost Yaks
30421 Posts |
Posted - 2006-09-20 : 09:36:13
|
[code]SELECT s.[Company Description], RIGHT('0000' + [Company Code], 4) CompKey, t.Months0To3, y.BadDebtFROM ( SELECT [Company Code], [Company Description] FROM Structure GROUP BY [Company Code], [Company Description] ) AS sLEFT JOIN ( SELECT CoCd, (Sum([1To90]) + Sum([Not Due]) + Sum([91To120])) / 1000.0) Months0To3 FROM zarageddebt_CURR GROUP BY CoCd ) t ON t.CoCd = s.[Company Code]LEFT JOIN ( SELECT CoCd, SUM(baddebt_CURR.BadDebt) BadDebt FROM baddebt_CURR GROUP BY CoCd ) u ON u.CoCd = s.[Company Code][/code]Peter LarssonHelsingborg, Sweden |
 |
|
qwertyjjj
Posting Yak Master
131 Posts |
Posted - 2006-09-20 : 10:15:18
|
Neat little trick.Thanks ! |
 |
|
|
|
|
|
|