2016-08-08 47 views
1

Ich habe eine Tabelle namens delitems mit einigen Spalten.Gruppenkonkat in Mysql-Anweisung

Innerhalb meiner SELECT Anweisung Ich möchte ein GROUP_CONCAT verwenden:

+-------------------------------+-------+--------+--------+-----+ 
| COLOR       | tOTAL | Ptotal | Amount | qty | 
+-------------------------------+-------+--------+--------+-----+ 
| BLUE - W = 55,BLUE - W/O = 93 | 148 | 375 | 55500 | 2 | 
+-------------------------------+-------+--------+--------+-----+ 
mysql>select GROUP_CONCAT(color,' = ',qty) as COLOR, SUM(qTY) AS tOTAL, suM(p_cost) as Ptotal, SUM(qty)*SUM(p_cost) as Amount,count(*) qty from delitems where status='3' Group By cont_no; 

Alles funktioniert mit Ausnahme der Amount Spalte in Ordnung. Die Gesamtmenge ist falsch! Hier ist der richtige Wert:

+-----------------+-------+--------+--------+-----+ 
| COLOR   | tOTAL | Ptotal | Amount | qty | 
+-----------------+-------+--------+--------+-----+ 
| BLUE - W = 55 | 55 | 125 | 6875 | 1 | 
| BLUE - W/O = 93 | 93 | 250 | 23250 | 1 | 
+-----------------+-------+--------+--------+-----+ 
mysql>select GROUP_CONCAT(color,' = ',qty) as COLOR, SUM(qTY) AS tOTAL, suM(p_cost) as Ptotal, SUM(qty)*SUM(p_cost) as Amount,count(*) qty from delitems where status='3' Group By color; 

Ich will nur mit dem richtigen Gesamtbetrag

Bitte helfen Sie in einer Zeile angezeigt werden soll.

+0

Was ist das Problem? Können Sie uns Beispieldaten zeigen? –

+0

Hallo Sir! ... die Gesamtmenge bei der ersten Abfrage ist die Summe aus Summe und Ptotal in der Gruppenkonstellation ... bei der zweiten Abfrage hat jede ihre eigene Summe wie BLAU -W-Menge ist 55 * 125 und B: UE - W/O Menge ist 93 * 250 ... alles, was ich brauche, ist die Summe aller Werte in nur einer Zeile anzeigen? – Psd

Antwort

1

Sollten Sie bei Summe benötigen (a * b) nicht Summe (a) * Summe (b)

select 
    GROUP_CONCAT(color,' = ',qty) as COLOR 
, SUM(qTY) AS tOTAL 
, suM(p_cost) as Ptotal 
, SUM(qty*(p_cost) as Amount, count(*) qty 
from delitems 
where status='3' Group By cont_no; 
+0

Wow, das ist großartig ... du hast mein Problem gelöst ... zwei Daumen hoch, Sir !! ... – Psd

+0

Ich mehr Problem, Sir ... Ich werde es posten. – Psd

+0

@Psd den Link kommentieren, wenn Sie das neue Problem gepostet haben .. – scaisEdge