0
SELECT mem_id, sum(commission)
FROM `member_commission`
group by mem_id

the first sql command results in this

SELECT mem_id, sum(crowdfund)
FROM `crowdfund`
group by mem_id

the first sql command results in this

i want to combine both of these commands in one and then group them by mem_id

+

both of my table (member_commission, crowd fund) have a (datetime stamp - which records date and time) column which is time column present in membecommission and distrbutedmonth in crowdfund.... and i just want the result of the present month only...

the datetime column records date and time in this format : 2018-08-22 22:02:09

E_net4
  • 27,810
  • 13
  • 101
  • 139
user10633681
  • 47
  • 1
  • 5
  • 3
    Please tag your question with the database you are using. – Gordon Linoff Nov 10 '18 at 17:14
  • Hi, welcome to SO. Can you sketch the desired output? It sounds like you'd like a [full outer join](https://www.w3schools.com/sql/sql_join_full.asp) on the results of [subqueries]() or a [cte](https://stackoverflow.com/questions/4740748/when-to-use-common-table-expression-cte), depending on your output and database engine – wibeasley Nov 10 '18 at 17:35

1 Answers1

0

One method uses union all and group by:

select mem_id, sum(commission), sum(crowdfund)
from ((select mc.mem_id, mc.commission, 0 as crowdfund
       from member_commission mc
      ) union all
      (select cf.mem_id, 0, cf.crowdfund
       from crowdfund cf
      )
     ) x
group by mem_id;

This is tricky because members could be in either table -- and presumably you still want them in the result set.

Gordon Linoff
  • 1,242,037
  • 58
  • 646
  • 786
  • I didn't quite get what you did there but the results look like what I wanted!!! so thank you – user10633681 Nov 10 '18 at 17:18
  • @user10633681 . . . Is there a reason why you unaccepted the answer? As to how it works, run the subquery and see what the results are. – Gordon Linoff Nov 10 '18 at 17:40
  • yeah! i am just editing the question a little bit... the thing is both of my table (member_commission, crowd fund) have a datetime stamp which is time, distrbutedmonth respectively.... and i just want the result of the present month only... can you please please change your answer according to that?????? – user10633681 Nov 10 '18 at 17:47
  • @user10633681 . . . You should (1) undo the edit; (2) ask another question with appropriate sample data and explanation. You have answers to the first question. It is rude to change the question in a way that answers are invalidated. – Gordon Linoff Nov 10 '18 at 18:03
  • i thought same... but SO didnt allowed me to add another question so frequently... so i had to do it because it is urgent.... plz help me ... and i will accept your answer... for more explanation i am editing the question – user10633681 Nov 10 '18 at 18:09