Home > Enterprise >  I have a SUM function in my SELECT but don't want it to show
I have a SUM function in my SELECT but don't want it to show

Time:01-28

I want to see the companies which deliver the most sold items in my DB.

Currently i have this Code:

SELECT L."Firma", SUM("B"."Anzahl")
FROM "Bestelldetails" B, "Artikel" A, "Lieferanten" L
WHERE L."Lieferanten-Nr" = A."Lieferanten-Nr"
AND A."Artikel-Nr" = B."Artikel-Nr"
GROUP BY L."Firma"
ORDER BY 2 DESC

The Output that i get:

Firma 2
Company 1 2756
Company 2 2377
Company 3 2063
...many more... ..XXX..

The Output that i want:

Firma
Company 1
Company 2
Company 3
...many more...

Code with output as image

But i don't want the row with the number 2 to show. I just want the Company-Names to show. How would i do so?

CodePudding user response:

Simply ORDER BY the SUM():

SELECT L."Firma"
FROM "Bestelldetails" B
JOIN "Artikel" A
  ON A."Artikel-Nr" = B."Artikel-Nr"
JOIN "Lieferanten" L
  ON L."Lieferanten-Nr" = A."Lieferanten-Nr"
GROUP BY L."Firma"
ORDER BY SUM("B"."Anzahl") DESC

(Now using proper, explicit JOIN syntax.)

CodePudding user response:

select Firma from 
(
SELECT L."Firma", SUM("B"."Anzahl")
FROM "Bestelldetails" B, "Artikel" A, "Lieferanten" L
WHERE L."Lieferanten-Nr" = A."Lieferanten-Nr"
AND A."Artikel-Nr" = B."Artikel-Nr"
GROUP BY L."Firma"
ORDER BY 2 DESC
);
  •  Tags:  
  • Related