| Author |
Topic |
|
arkiboys
Master Smack Fu Yak Hacker
1433 Posts |
Posted - 2011-12-15 : 07:03:20
|
| I am unable to explain why these two queries give two different sum value.Do you see why please?ThanksSELECT SUM(tbl1.BV), BCcy FROM tbl1 INNER JOIN tblMain ON tbl1.PCode = tblMain.BCode WHERE tbl1.PCode='xx' andtbl1.AsOfDate = '23 jan 2005'GROUP BY BCcy------------------------SELECT SUM(BE1.BV), BCcy FROM tbl1 as BE1 INNER JOIN tblMain ON BE1.PCode = tblMain.BCode INNER JOIN tbl1 BE2 ONBE1.PCode = BE2.PCode AND BE1.AsOfDate = BE2.AsOfDateWHERE BE1.PCode='xx' andBE1.AsOfDate = '23 jan 2005'GROUP BY BCcy |
|
|
visakh16
Very Important crosS Applying yaK Herder
52326 Posts |
Posted - 2011-12-15 : 07:13:07
|
| that because of addition joinINNER JOIN tbl1 BE2 ONBE1.PCode = BE2.PCode AND BE1.AsOfDate = BE2.AsOfDatein first case it will return count irrespective of above relationship whereas in second case it will put further check in place to make sure that above relationship holds good------------------------------------------------------------------------------------------------------SQL Server MVPhttp://visakhm.blogspot.com/ |
 |
|
|
arkiboys
Master Smack Fu Yak Hacker
1433 Posts |
Posted - 2011-12-15 : 07:17:53
|
| Do not understand. Can you please elaborate further?Thanks |
 |
|
|
visakh16
Very Important crosS Applying yaK Herder
52326 Posts |
Posted - 2011-12-15 : 07:21:00
|
| are there multiple records in tbl1 with same values for PCode and AsOfDate ?------------------------------------------------------------------------------------------------------SQL Server MVPhttp://visakhm.blogspot.com/ |
 |
|
|
arkiboys
Master Smack Fu Yak Hacker
1433 Posts |
Posted - 2011-12-15 : 09:06:25
|
quote: Originally posted by visakh16 are there multiple records in tbl1 with same values for PCode and AsOfDate ?------------------------------------------------------------------------------------------------------SQL Server MVPhttp://visakhm.blogspot.com/
Yes |
 |
|
|
visakh16
Very Important crosS Applying yaK Herder
52326 Posts |
Posted - 2011-12-15 : 09:18:46
|
| then it will cause it to return more rows as duplicates on joining yield more rows (ie 2 rows with same value for columns on joining returns 2 * 2 =4 rows)------------------------------------------------------------------------------------------------------SQL Server MVPhttp://visakhm.blogspot.com/ |
 |
|
|
visakh16
Very Important crosS Applying yaK Herder
52326 Posts |
Posted - 2011-12-15 : 09:23:20
|
see this example declare @tbl table(id int)insert @tblselect 1 union allselect 2 union allselect 2 union allselect 3 union allselect 3 select *from @tbl t1join @tbl t2on t2.id = t1.id ------------------------------------------------------------------------------------------------------SQL Server MVPhttp://visakhm.blogspot.com/ |
 |
|
|
arkiboys
Master Smack Fu Yak Hacker
1433 Posts |
Posted - 2011-12-15 : 09:24:16
|
| HOw can I make the second query to return the same sum as the first query?Thanks |
 |
|
|
visakh16
Very Important crosS Applying yaK Herder
52326 Posts |
Posted - 2011-12-15 : 09:29:23
|
| depending on your requirement take distinct based on column with duplicate values------------------------------------------------------------------------------------------------------SQL Server MVPhttp://visakhm.blogspot.com/ |
 |
|
|
arkiboys
Master Smack Fu Yak Hacker
1433 Posts |
Posted - 2011-12-15 : 09:30:54
|
| Thank you |
 |
|
|
visakh16
Very Important crosS Applying yaK Herder
52326 Posts |
Posted - 2011-12-15 : 10:06:46
|
| wc------------------------------------------------------------------------------------------------------SQL Server MVPhttp://visakhm.blogspot.com/ |
 |
|
|
|