我有以下查询,我想要做的是添加一个列,以确定这对于不同的客户的订单。 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;
答案 0 :(得分:0)
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
对于每个订单都是相同的。