使用sum(cloumnc)选择不同的columna和columnb

qnzebej0  于 2021-06-18  发布在  Mysql
关注(0)|答案(4)|浏览(259)

我有一张这样的table:

importer     exporter     quantity    
A            D           0.9
A            B           0.9
A            B           0.1
B            E           9.4
B            E           8.9
B            D           9.4
C            P           9.0
C            V           1.0
C            P           0.9

我想找到不同的columna和columnb的总和(columnc),表是OrderbySum(columnc)desc。

importer     exporter     quantity
B            E           18.3
C            P           9.9
B            D           9.4    
A            B           1.0
C            V           1.0
A            D           0.9

当我试着

SELECT DISTINCT
 IMPORTER, EXPORTER, QUANTITY
 FROM Tablename;

mysql显示的表不是不同的columna和columnb,实际上它显示的是重复的columna和columnb,columnc没有相加。

nhaq1z21

nhaq1z211#

像下面这样试试

SELECT 
 IMPORTER, EXPORTER, sum(QUANTITY)
 FROM Tablename group by IMPORTER, EXPORTER
cedebl8k

cedebl8k2#

它是由以下各项组成的基本组:

SELECT 
 IMPORTER, EXPORTER, SUM(QUANTITY) AS SUMQUANTITY
 FROM Tablename
 GROUP BY IMPORTER, EXPORTER
 ORDER BY SUMQUANTITY DESC;
fsi0uk1n

fsi0uk1n3#

试试这个:

SELECT   importer,
         exporter,
         SUM(quantity) AS sum_quantity
FROM     tablename
GROUP BY importer,
         exporter
ORDER BY sum_quantity DESC;
icomxhvb

icomxhvb4#

正如@gordonlinoff所暗示的,你需要的是 GROUP BY 查询

SELECT 
    IMPORTER, 
    EXPORTER, 
    SUM(QUANTITY)
FROM Tablename
GROUP BY
    IMPORTER, 
    EXPORTER
ORDER BY 
    SUM(QUANTITY) DESC;

相关问题