1

I have a table like this:

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

I want to find the distinct columnA and columnB with the sum(columnC) and the table is ORDER BY SUM(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

when I tried

 SELECT DISTINCT
 IMPORTER, EXPORTER, QUANTITY
 FROM Tablename;

The table MYsql shows is not distinct columnA and columnB, in fact it shows duplicated columnA and columnB and the columnC is not added up.

4 Answers4

2

Try this:

SELECT   importer,
         exporter,
         SUM(quantity) AS sum_quantity
FROM     tablename
GROUP BY importer,
         exporter
ORDER BY sum_quantity DESC;
Robert Kock
  • 5,795
  • 1
  • 12
  • 20
0

Try like below

 SELECT 
 IMPORTER, EXPORTER, sum(QUANTITY)
 FROM Tablename group by IMPORTER, EXPORTER
Zaynul Abadin Tuhin
  • 31,407
  • 5
  • 33
  • 63
0

As hinted by @GordonLinoff, what you need is a GROUP BY query

SELECT 
    IMPORTER, 
    EXPORTER, 
    SUM(QUANTITY)
FROM Tablename
GROUP BY
    IMPORTER, 
    EXPORTER
ORDER BY 
    SUM(QUANTITY) DESC;
GMB
  • 216,147
  • 25
  • 84
  • 135
0

It is the basic GROUP BY:

 SELECT 
 IMPORTER, EXPORTER, SUM(QUANTITY) AS SUMQUANTITY
 FROM Tablename
 GROUP BY IMPORTER, EXPORTER
 ORDER BY SUMQUANTITY DESC;
forpas
  • 160,666
  • 10
  • 38
  • 76