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 |
aJaggedsky
Starting Member
1 Post |
Posted - 2006-11-01 : 19:44:38
|
Select sum(SOP30200.SUBTOTAL),Count(SOP30200.SOPNUMBE)From SOP30200, SOP10106 Where SOP30200.SOPTYPE = '3'AND SOP30200.SOPNUMBE = SOP10106.SOPNUMBEAND SOP30200.DOCDATE BETWEEN @STARTMONTH AND @CURRENTMONTHENDAND SOP10106.USRTAB01 <> ''GROUP BY SOP10106.USRTAB01Returns the correct results of:70.17000 17637.50000 3Incorporating this logic into this join statement returns incorrect data in the last column, should be 1 and 3.Select UPR00100.EMPLOYID,sum(PA30101.PABILLED_AMOUNT) Billings,SOP10106.USRTAB01 , Count(SOP30200.SOPNUMBE)--sum(SOP30200.SUBTOTAL) As AdditionalFrom PA30101 Inner Join UPR00100 ON PA30101.EMPLOYID = UPR00100.EMPLOYID AND PA30101.PADT BETWEEN @STARTMONTH AND @CURRENTMONTHEND Left Outer Join SOP10106 ON PA30101.EMPLOYID = SOP10106.USRTAB01 AND SOP10106.USRTAB01 <> '' Left Outer Join SOP30200 ON SOP10106.SOPNUMBE = SOP30200.SOPNUMBE And SOP30200.SOPTYPE = '3' AND SOP30200.DOCDATE BETWEEN @STARTMONTH AND @CURRENTMONTHENDGroup By UPR00100.EMPLOYID,SOP10106.USRTAB01ORDER BY UPR00100.EMPLOYIDa 114064.58000 a 955b 37275.00000 NULL 0c 36662.42000 NULL 0d 22235.00000 NULL 0e 94962.51000 NULL 0f 204663.75000 f 771g 45507.25000 NULL 0Please help me find my flaw in the logic! Thanks!! |
|
SwePeso
Patron Saint of Lost Yaks
30421 Posts |
Posted - 2006-11-02 : 01:30:17
|
For the first, you are INNER JOINing more columns, thus creating more combinations of total selected rows.Second, try to rewrite COUNT() as COUNT(DISTINCT...).It is hard to tell what is wrong as long as we don't have access to source/sample data.Peter LarssonHelsingborg, Sweden |
 |
|
|
|
|