[英]MySQL Row Numbering not getting desired results
我有以下查詢,我想要做的是添加一個列,以確定這是一個不同的客戶的訂單。 entity_id對於每個訂單都是唯一的,所以我想要做的就是為created_at訂購的每個客戶編號唯一的entity_id。 問題是訂單中的每個項目都有一行,因此我的當前查詢編號錯誤。
這是目前的結果
Order Date ,OrderID, SKU ,Qty ,Customer Email ,First,last ,seqnum
'2015-04-01 14:48:13','19406','201','2.0000','test@google.com','joe','vers','1'
'2015-04-01 14:48:13','19406','201','2.0000','test@google.com','joe','vers','2'
'2015-04-01 14:48:13','19406','201','2.0000','test@google.com','joe','vers','3'
'2015-04-01 14:48:13','19406','201','2.0000','test@google.com','joe','vers','4'
'2015-04-01 14:48:13','19406','201','2.0000','test@google.com','joe','vers','5'
'2015-04-01 14:48:13','19406','103','1.0000','test@google.com','joe','vers','6'
'2015-04-01 14:48:13','19406','102','1.0000','test@google.com','joe','vers','7'
'2015-04-01 14:48:13','19406','102','1.0000','test@google.com','joe','vers','8'
'2015-04-01 14:48:13','19406','102','1.0000','test@google.com','joe','vers','9'
'2015-04-01 14:48:13','19406','201','1.0000','test@google.com','joe','vers','10'
'2015-04-01 14:48:13','19406','102','1.0000','test@google.com','joe','vers','11'
'2015-04-01 14:48:13','19406','105','1.0000','test@google.com','joe','vers','12'
'2015-04-13 17:45:15','20537','105','1.0000','test@google.com','joe','vers','13'
'2015-04-13 17:45:15','20537','102','1.0000','test@google.com','joe','vers','14'
'2015-04-13 17:45:15','20537','201','1.0000','test@google.com','joe','vers','15'
'2015-04-13 17:45:15','20537','201','1.0000','test@google.com','joe','vers','16'
'2015-04-29 14:42:28','22212','102','1.0000','test@google.com','joe','vers','17'
'2015-05-11 17:11:22','23301','102','1.0000','test@google.com','joe','vers','18'
這是期望的結果
Order Date ,OrderID, SKU ,Qty ,Customer Email ,First,last ,seqnum
'2015-04-01 14:48:13','19406','201','2.0000','test@google.com','joe','vers','1'
'2015-04-01 14:48:13','19406','201','2.0000','test@google.com','joe','vers','1'
'2015-04-01 14:48:13','19406','201','2.0000','test@google.com','joe','vers','1'
'2015-04-01 14:48:13','19406','201','2.0000','test@google.com','joe','vers','1'
'2015-04-01 14:48:13','19406','201','2.0000','test@google.com','joe','vers','1'
'2015-04-01 14:48:13','19406','103','1.0000','test@google.com','joe','vers','1'
'2015-04-01 14:48:13','19406','102','1.0000','test@google.com','joe','vers','1'
'2015-04-01 14:48:13','19406','102','1.0000','test@google.com','joe','vers','1'
'2015-04-01 14:48:13','19406','102','1.0000','test@google.com','joe','vers','1'
'2015-04-01 14:48:13','19406','201','1.0000','test@google.com','joe','vers','1'
'2015-04-01 14:48:13','19406','102','1.0000','test@google.com','joe','vers','1'
'2015-04-01 14:48:13','19406','105','1.0000','test@google.com','joe','vers','1'
'2015-04-13 17:45:15','20537','105','1.0000','test@google.com','joe','vers','2'
'2015-04-13 17:45:15','20537','102','1.0000','test@google.com','joe','vers','2'
'2015-04-13 17:45:15','20537','201','1.0000','test@google.com','joe','vers','2'
'2015-04-13 17:45:15','20537','201','1.0000','test@google.com','joe','vers','2'
'2015-04-29 14:42:28','22212','102','1.0000','test@google.com','joe','vers','3'
'2015-05-11 17:11:22','23301','102','1.0000','test@google.com','joe','vers','4'
這是查詢:
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;
啊,我明白了。 訂單是基於電子郵件的數字。 好的。
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;
這假設created_at
對於每個訂單都是相同的。
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.