Author |
Topic |
ravininave
Posting Yak Master
111 Posts |
Posted - 2015-05-05 : 16:04:16
|
Hello, I'm trying this querySELECT P.AppMstID, A.AppMstName, SUM(COALESCE(P.AMOUNT,0)) AS PAYOUT, SUM(COALESCE(E.AMOUNT ,0)) AS PAID FROM PayReports PINNER JOIN AppMast A ON P.AppMstID= A.AppMstIDLEFT OUTER JOIN IncExpMast as E ON P.AppMstID= E.AppMstIDGROUP BY P.AppMstID, A.AppMstName But I'm getting wrong sum in Paid Column. Why?VB6/ASP.NET------------------------http://www.nehasoftec.com |
|
tkizer
Almighty SQL Goddess
38200 Posts |
Posted - 2015-05-05 : 16:12:49
|
Please provide sample data and expected output.Tara KizerSQL Server MVP since 2007http://weblogs.sqlteam.com/tarad/ |
|
|
ravininave
Posting Yak Master
111 Posts |
Posted - 2015-05-05 : 16:20:59
|
Payreports TableAppMstID Amount1 200.001 96.001 200.001 96.001 200.00AppMastAppMstID AppMstName1 RajkumarIncExpMastAppMstID Amount1 1000But the output I'm getting isAppMstID AppMstName PAYOUT PAID1 Rajkumar 792 5000Paid should be 1000 |
|
|
tkizer
Almighty SQL Goddess
38200 Posts |
Posted - 2015-05-05 : 16:27:19
|
Show us the data from this:SELECT P.AppMstID, A.AppMstName, P.AMOUNT, E.AMOUNT FROM PayReports PINNER JOIN AppMast A ON P.AppMstID= A.AppMstIDLEFT OUTER JOIN IncExpMast as E ON P.AppMstID= E.AppMstIDWHERE P.AppMstID= 1Tara KizerSQL Server MVP since 2007http://weblogs.sqlteam.com/tarad/ |
|
|
ravininave
Posting Yak Master
111 Posts |
Posted - 2015-05-05 : 16:29:18
|
AppMstID AppMstName AMOUNT AMOUNT1 Rajkumar 200.00 10001 Rajkumar 96.00 10001 Rajkumar 200.00 10001 Rajkumar 96.00 10001 Rajkumar 200.00 1000 |
|
|
tkizer
Almighty SQL Goddess
38200 Posts |
Posted - 2015-05-05 : 16:39:27
|
quote: Originally posted by ravininave AppMstID AppMstName AMOUNT AMOUNT1 Rajkumar 200.00 10001 Rajkumar 96.00 10001 Rajkumar 200.00 10001 Rajkumar 96.00 10001 Rajkumar 200.00 1000
That's why. You have multiple rows in IncExpMast that match, so it's totaling up the 1000 5 times.Tara KizerSQL Server MVP since 2007http://weblogs.sqlteam.com/tarad/ |
|
|
ravininave
Posting Yak Master
111 Posts |
Posted - 2015-05-05 : 16:42:13
|
I've multiple rows (5) in Payreports and only 1 row in IncExpMastSo, It's showing total upto 5 times. Then what is the solution.? |
|
|
tkizer
Almighty SQL Goddess
38200 Posts |
Posted - 2015-05-05 : 16:48:44
|
Sorry that's what I meant. WITH E (AppMstID, PAID)AS(SELECT AppMstID, SUM(COALESCE(AMOUNT,0)) AS PAID FROM IncExpMastGROUP BY AppMstID)SELECT P.AppMstID, A.AppMstName, SUM(COALESCE(P.AMOUNT,0)) AS PAYOUT, E.PAID FROM PayReports PINNER JOIN AppMast A ON P.AppMstID= A.AppMstIDLEFT OUTER JOIN E ON P.AppMstID= E.AppMstIDGROUP BY P.AppMstID, A.AppMstName, PAIDTara KizerSQL Server MVP since 2007http://weblogs.sqlteam.com/tarad/ |
|
|
ravininave
Posting Yak Master
111 Posts |
Posted - 2015-05-05 : 16:54:01
|
Yes, it works now. Thanking You! |
|
|
tkizer
Almighty SQL Goddess
38200 Posts |
|
|