2017-04-22 5 views
1

Ich habe die folgende Abfrage und was ich tun möchte, ist eine Spalte hinzufügen, die identifizieren, welche Reihenfolge dies für einen bestimmten Kunden ist. entity_id ist eindeutig pro Bestellung, also möchte ich wirklich nur die eindeutige Entity_id für jeden Kunden, der von created_at bestellt wurde, vergeben. Das Problem ist, dass es für jedes Element in einer Reihenfolge eine Zeile gibt, so dass es mit meiner aktuellen Abfrage falsch nummeriert ist.MySQL Zeilennummerierung nicht erwünschte Ergebnisse

Dies ist die aktuellen Ergebnisse

Order Date   ,OrderID, SKU ,Qty  ,Customer Email ,First,last ,seqnum 
'2015-04-01 14:48:13','19406','201','2.0000','[email protected]','joe','vers','1' 
'2015-04-01 14:48:13','19406','201','2.0000','[email protected]','joe','vers','2' 
'2015-04-01 14:48:13','19406','201','2.0000','[email protected]','joe','vers','3' 
'2015-04-01 14:48:13','19406','201','2.0000','[email protected]','joe','vers','4' 
'2015-04-01 14:48:13','19406','201','2.0000','[email protected]','joe','vers','5' 
'2015-04-01 14:48:13','19406','103','1.0000','[email protected]','joe','vers','6' 
'2015-04-01 14:48:13','19406','102','1.0000','[email protected]','joe','vers','7' 
'2015-04-01 14:48:13','19406','102','1.0000','[email protected]','joe','vers','8' 
'2015-04-01 14:48:13','19406','102','1.0000','[email protected]','joe','vers','9' 
'2015-04-01 14:48:13','19406','201','1.0000','[email protected]','joe','vers','10' 
'2015-04-01 14:48:13','19406','102','1.0000','[email protected]','joe','vers','11' 
'2015-04-01 14:48:13','19406','105','1.0000','[email protected]','joe','vers','12' 
'2015-04-13 17:45:15','20537','105','1.0000','[email protected]','joe','vers','13' 
'2015-04-13 17:45:15','20537','102','1.0000','[email protected]','joe','vers','14' 
'2015-04-13 17:45:15','20537','201','1.0000','[email protected]','joe','vers','15' 
'2015-04-13 17:45:15','20537','201','1.0000','[email protected]','joe','vers','16' 
'2015-04-29 14:42:28','22212','102','1.0000','[email protected]','joe','vers','17' 
'2015-05-11 17:11:22','23301','102','1.0000','[email protected]','joe','vers','18' 

Dies ist die gewünschten Ergebnisse

Order Date   ,OrderID, SKU ,Qty  ,Customer Email ,First,last ,seqnum 
'2015-04-01 14:48:13','19406','201','2.0000','[email protected]','joe','vers','1' 
'2015-04-01 14:48:13','19406','201','2.0000','[email protected]','joe','vers','1' 
'2015-04-01 14:48:13','19406','201','2.0000','[email protected]','joe','vers','1' 
'2015-04-01 14:48:13','19406','201','2.0000','[email protected]','joe','vers','1' 
'2015-04-01 14:48:13','19406','201','2.0000','[email protected]','joe','vers','1' 
'2015-04-01 14:48:13','19406','103','1.0000','[email protected]','joe','vers','1' 
'2015-04-01 14:48:13','19406','102','1.0000','[email protected]','joe','vers','1' 
'2015-04-01 14:48:13','19406','102','1.0000','[email protected]','joe','vers','1' 
'2015-04-01 14:48:13','19406','102','1.0000','[email protected]','joe','vers','1' 
'2015-04-01 14:48:13','19406','201','1.0000','[email protected]','joe','vers','1' 
'2015-04-01 14:48:13','19406','102','1.0000','[email protected]','joe','vers','1' 
'2015-04-01 14:48:13','19406','105','1.0000','[email protected]','joe','vers','1' 
'2015-04-13 17:45:15','20537','105','1.0000','[email protected]','joe','vers','2' 
'2015-04-13 17:45:15','20537','102','1.0000','[email protected]','joe','vers','2' 
'2015-04-13 17:45:15','20537','201','1.0000','[email protected]','joe','vers','2' 
'2015-04-13 17:45:15','20537','201','1.0000','[email protected]','joe','vers','2' 
'2015-04-29 14:42:28','22212','102','1.0000','[email protected]','joe','vers','3' 
'2015-05-11 17:11:22','23301','102','1.0000','[email protected]','joe','vers','4' 

Dies ist die Abfrage:

select sfo.created_at AS "Order Date", sfo.entity_id AS "Order ID", 
     left(sfoi.sku,3) AS "SKU", sfoi.qty_ordered, 
     sfo.customer_email AS "Customer Email",  
     sfo.customer_firstname AS "Customer Firstname",  
     sfo.Customer_lastname AS "Customer Lastname", 
     (@rn := if(@e = sfo.customer_email, @rn + 1, 
        if(@e := sfo.customer_email, 1, 1) 
       ) 
     ) as seqnum 
from sales_flat_order sfo join 
    sales_flat_order_item sfoi 
    on sfoi.order_id = sfo.entity_id join 
    sales_flat_order_address sfoa   
    on sfoa.entity_id = sfo.billing_address_id cross join 
    (select @rn := 0, @e := '') params 
order by sfo.customer_email, created_at; 

Antwort

0

Ah, ich verstehe. Die Bestellungen sind Zahlen basierend auf der E-Mail. Okay.

select sfo.created_at AS "Order Date", sfo.entity_id AS "Order ID", 
     left(sfoi.sku,3) AS "SKU", sfoi.qty_ordered, 
     sfo.customer_email AS "Customer Email",  
     sfo.customer_firstname AS "Customer Firstname",  
     sfo.Customer_lastname AS "Customer Lastname", 
     (@rn := if(@e = sfo.customer_email, 
        if(@o = sfo.entity_id, @rn, 
        if(@o := sfo.entity_id, @rn + 1, @rn + 1 
         ) 
        ), 
        if(@e := sfo.customer_email, 
        if(@o := sfo.entity_id, 1, 1), 
        if(@o := sfo.entity_id, 1, 1) 
        ) 
       ) 
     ) as seqnum 
from sales_flat_order sfo join 
    sales_flat_order_item sfoi 
    on sfoi.order_id = sfo.entity_id join 
    sales_flat_order_address sfoa   
    on sfoa.entity_id = sfo.billing_address_id cross join 
    (select @rn := 0, @e := '', @o := -1) params 
order by sfo.customer_email, created_at, orderid; 

Dies setzt voraus, dass created_at das gleiche für jede Ordnung ist.

+0

Vielen Dank, das funktioniert gut, wenn Sie Zeit bekommen, würde ich gerne wissen, wie dies funktioniert, da ich nicht mit MySql vertraut bin. – llerdal

+0

Sie müssen lernen, wie Variablen verwendet werden. Dies ist ein ziemlich kompliziertes Beispiel für den Einstieg. –