统计订单销售历史表中每个店铺中每种商品销售数量最多的用户的信息

类似查询订单销售历史表中每个店铺中每个商品销售数量最多的用户的信息(多表查询:查询的数据包含用户表,销售订单历史表,商品表,店铺表等表中的信息)
//method1 先根据条件查询出记录,然后用查出来的记录,去联合查询需要查询信息的表
select * from
( select t.sale_qty,t.item_id,t.store_id ,t.last_modify_user from sd_sales_order_his t
where t.sale_qty =(
select max(sd_sales_order_his.sale_qty) from sd_sales_order_his
where sd_sales_order_his.item_id = t.item_id and sd_sales_order_his.store_id=t.store_id
)) t
left join tbl_user e on e.user_id = t.last_modify_user
left join sd_store c on t."store_id" = c."store_id"
left join sd_item d on t."item_id" = d."item_id"

//method2 先把需要查询的几个表关联成一个临时的表,里面存放需要查询的所有字段,然后再根据条件过滤查询记录
select * from (select sd_sales_order_his.sale_qty,d.item_id,d.item_code,e.user_name,c.store_id from sd_sales_order_his
left join sd_store c on sd_sales_order_his."store_id" = c."store_id"
left join sd_item d on sd_sales_order_his."item_id" = d."item_id"
left join tbl_user e on e.user_id = sd_sales_order_his.last_modify_user) temp
where temp.sale_qty=
(select max(sd_sales_order_his.sale_qty) from sd_sales_order_his
where item_id = temp.item_id and store_id=temp.store_id)

//method3 先把需要查询的几个表关联成一个临时的表,里面存放需要查询的所有字段,然后再根据条件过滤查询记录
select * from (select sd_sales_order_his.sale_qty,d.item_id,d.item_code,e.user_name,c.store_id from sd_sales_order_his
left join sd_store c on sd_sales_order_his."store_id" = c."store_id"
left join sd_item d on sd_sales_order_his."item_id" = d."item_id"
left join tbl_user e on e.user_id = sd_sales_order_his.last_modify_user) temp
where not exists
(select 1 from sd_sales_order_his
where item_id = temp.item_id and store_id=temp.store_id and sale_qty>temp.sale_qty)

//类似查询订单销售历史表中每个店铺中每个商品销售数量最多的用户的信息(单表查询:查询的数据只包含用户表,不包括销售订单历史表,商品表,店铺表等表中的信息)
select * from tbl_user where tbl_user.user_id in
( select last_modify_user from sd_sales_order_his t where sale_qty =(
select max(sd_sales_order_his.sale_qty) from sd_sales_order_his
where sd_sales_order_his.item_id = t.item_id and sd_sales_order_his.store_id=t.store_id
))

你可能感兴趣的:(统计订单销售历史表中每个店铺中每种商品销售数量最多的用户的信息)