简体   繁体   English

如何连接两个表并以sum的升序或降序显示结果

[英]how to join two table and display the result in ascending or descending order of sum

I having great difficulty to join two table and get the result like following way . 我很难加入两个表并获得如下结果的结果。

table 1
id  |company_name   | fee

1   |company2       |1000
2   |company1       |2000
3   |company1       |4000
4   |company3       |3000


table 2

company_name    | product

company1        |A
company1        |B
company3        |c
company2        |D

And expected result should be like this.. 而预期的结果应该是这样的..

company_name   | product | fee

company1       |A        |6000
company1       |B        |6000
company3       |C        |3000
company2       |D        |2000

Is that possible?. 那可能吗?。

select t1.company_name,t2.product,sum(fee) as fee from table1 t1 join table2 t2 on t1.company_name=t2.company_name group by t1.company_name,t2.product order by sum(fee) asc 选择t1.company_name,t2.product,sum(费用)作为费用来自table1 t1 join table2 t2 on t1.company_name = t2.company_name group by t1.company_name,t2.product order by sum(fee)asc

I have not tested it, but it should be something around these lines. 我没有测试过,但它应该是围绕这些线的东西。 The key here is to use "sum" in combination with a group by clause of both name and product. 这里的关键是将“sum”与名称和产品的group by子句结合使用。

use following sql 使用以下sql

    SELECT T1.company_name,T2.product ,sum(T1.fee) as fee from TABLE1 
    T1 INNER JOIN TABLE2 T2 ON 
T1.company_name=T2.company_name GROUP BY T2.compnat_name,T2.product

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM