views:

82

answers:

2

Table a (Author Table)

author_id
author_name

Table b (Post Table)

post_id
author_id

Table c (Earning Table)

post_id (post id is not unique)
post_earning

I wanted to generate a report consist of earnings per author.

author_id
author_name
total_earning (sum of earnings of all the posts by author)

The SQL Query used:

SELECT
   a.author_id,
   a.author_name,
   sum(post_earnings) as total_earnings
FROM TableA a
Inner Join TableB b on b.author_id = a.author_id
Inner Join TableC c on c.post_id = b.post_id
Group By 
   a.author_id,
   a.author_name

The Result I got is this :

ID  user_login  total_earnings
2   Redstar 13.99
7   Redleaf 980.18
10  topnhotnews 80.43
11  zmmishad    39.27
13  rashel  1248.34
14  coolsaint   1.66
16  hotnazmul   9.83
17  rubel   0.14
21  mahfuz1986  1.09
48  ripon   12.96
60  KHK 27.81

the sum of the total earning is actually 2863.22. But if i add all the value of the result table I get 2415. Where is the problem? The sample tables used can be downloaded from the links in first comment.

+3  A: 
SELECT
   a.author_id,
   a.author_name,
   sum(post_earnings) as total_earnings
FROM TableA a
Inner Join TableB b on b.author_id = a.author_id
Inner Join TableC c on c.post_id = b.post_id
Group By 
   a.author_id,
   a.author_name
John Hartsock
unfortunately the sum of the total earning is actually 2863 but the above query gives sum of individual earning = 2415 here are the tables i am using http://www.newsnidea.com/downloads/wp_users.csv http://www.newsnidea.com/downloads/wp_posts.csv http://www.newsnidea.com/downloads/earning.csv
coolsaint
@user478879 I would need to see more information about the tables specified. If you post the DDL for the tables and perhaps a sample set of data, I could probably assist you further.
John Hartsock
@john i have put the tables in the previous comment.
coolsaint
@user478879.. better check your tables.. I just imported the csv files and ran the same query above. I got 2863.22 for each author.
John Hartsock
This is the result I am getting ID user_login total_earnings2 Redstar 13.997 Redleaf 980.1810 topnhotnews 80.4311 zmmishad 39.2713 rashel 1248.3414 coolsaint 1.6616 hotnazmul 9.8317 rubel 0.1421 mahfuz1986 1.0948 ripon 12.9660 KHK 27.81 are you getting the same?
coolsaint
+1  A: 

Are you sure that you have a right data in tables. Maybe a author is missing in TableA or some post in posts table.

Yasen Zhelev
You are right I had some posts absent in the post Table. A sql guru in sitepoint forum figured it out. Thanks for your response.
coolsaint