MYSQL - Aus zwei Zeilen eine

icon24

Grünschnabel
Hallo zusammen,

ich habe folgende Mysql-Abfrage generiert

1.PNG

Nun würde ich gerne aus den beiden Zeilen pro ID eine einzelne machen.
Also wenn das Produkt #Truchas ist bzw ISSCALE = 0 soll eine neue Spalte Anzahl eingefügt werden und dort die Unidades eingetragen werden.
Ich habe mal versucht das hier grafisch darzustellen
2.PNG

Meine Abfrage sieht bisher so aus:


Code:
SELECT        
                people.NAME AS Cajero, 
                receipts.DATENEW AS Fecha,
                tickets.TICKETID AS NoTicket, 
		tickets.TICKETTYPE AS TicketTipo,
 		products.NAME AS Producto,
		products.isscale AS ISSCALE,
                SUM(ticketlines.UNITS) AS Unidades,
                SUM(ticketlines.UNITS*ticketlines.PRICE) AS SinIVA, 
                taxes.RATE AS Impuesto, 
                SUM(ticketlines.UNITS*ticketlines.PRICE*taxes.RATE) AS IVA, 
                SUM(((ticketlines.UNITS*ticketlines.PRICE)+(ticketlines.UNITS*ticketlines.PRICE*taxes.RATE))) AS Total, 
                IF (customers.SEARCHKEY IS NULL,999999999,customers.SEARCHKEY) AS RUC, 
                IF (customers.NAME IS NULL,'Consumidor Final',customers.NAME) AS Nombre, 
                IF (customers.ADDRESS IS NULL,'-',customers.ADDRESS) AS Direccion, 
                IF (customers.PHONE IS NULL,'-',customers.PHONE)  AS Telefono,
                

                FROM 

		customers 
			RIGHT JOIN ((taxes INNER JOIN (people INNER JOIN (products RIGHT JOIN ((tickets INNER JOIN receipts ON tickets.ID = receipts.ID) INNER JOIN ticketlines ON tickets.ID = ticketlines.TICKET) ON products.ID = ticketlines.PRODUCT) ON people.ID = tickets.PERSON) ON taxes.ID = ticketlines.TAXID) INNER JOIN closedcash ON receipts.MONEY = closedcash.MONEY) ON customers.ID = tickets.CUSTOMER
               
		Where receipts.DATENEW Between '2012-05-13' and '2012-05-14' 
                GROUP BY tickets.TICKETID, products.NAME, customers.SEARCHKEY
                ORDER BY tickets.TICKETID  ASC

Ich hoffe ihr habt verstanden was ich machen möchte und könnt mir eine Hilfestellung geben.

Viele Grüße Nico
 
ein GROUP BY und dann mit IF den Wert zuordnen
SQL:
SELECT
    t.cajero,
    t.fecha,
    t.noticket,
    SUM(IF(producto = '#TRUCHAS', t.unidades, 0)) AS anzahl,
    SUM(IF(producto = 'TRUCHA LAGUNA', t.unidades, 0)) AS gewicht
FROM
    my_source t
GROUP BY
    t.cajero,
    t.fecha,
    t.noticket

Oder mit einem JOIN auf sich selber
SQL:
SELECT
    a.cajero,
    a.fecha,
    a.noticket,
    a.unidades AS anzahl
    b.unidades AS gewicht
FROM
    (SELECT * FROM my_table WHERE t.producto = '#TRUCHAS') a,
    (SELECT * FROM my_table WHERE t.producto = 'TRUCHA LAGUNA') b
WHERE
    a.noticket = b.noticket
 
Zuletzt bearbeitet von einem Moderator:

Neue Beiträge

Zurück