加入两个表加上透视表作为存储过程 - MySQL的

问题描述:

我努力让自己在一个MySQL的命令程序,使数据透视表的结果,但它确实是复杂的,我已经不知道该怎么办它,我不知道是否有另一种方式更容易做到这一点。加入两个表加上透视表作为存储过程 - MySQL的

我需要多少产品已在具体地址,GROUP_CONCAT(ship_addr + ship_zip + ship_contry)的结果被人买走了。

Order表 - 包含订单地址。

+----+----------------+----------+--------------+ 
| id | ship_addr | ship_zip | ship_country | 
+----+----------------+----------+--------------+ 
| 1 | Addr1   | 123  |   DE | 
| 2 | Addr2   | 321  |   DE | 
| 3 | Addr1   | 123  |   DE | 
| 4 | Addr2   | 321  |   DE | 
| 5 | Addr3   | 543  |   DE | 
+----+----------------+----------+--------------+ 

顺序位置表

+----+---------------+------------+--------------+ 
| id | order_id | product_id | quantity  | 
+----+----------------+-----------+--------------+ 
| 1 | 1    | 1   |   5 | 
| 2 | 1    | 2   |   10 | 
| 3 | 2    | 3   |   5 | 
| 4 | 3    | 1   |   5 | 
| 5 | 4    | 1   |   10 | 
| 6 | 5    | 1   |   10 | 
+----+----------------+----------+---------------+ 

预期的结果,列标题的product_id:

+-----------------+-------+-------+-------+ 
| Address  | 1 | 2 | 3 | 
+-----------------+-------+-------+-------+ 
| Addr1, 123, DE | 10 | 10 |  0 | 
| Addr2, 321, DE | 10 | 0 |  5 | 
| Addr3, 543, DE | 10 | 0 |  0 | 
+-----------------+-------+-------+-------+ 

我不能复制的存储过程我想是因为他们是完全失败。

+0

如果有人可以编辑标题,我将不胜感激,因为我不知道如何更好地解释它。 :) – Troyer

您好我想出了这个解决方案只是其中的一部分是缺少有0-4替换空。我会尝试当我更热时间,但结果是根据您的期望。附上了出来。这里值得一提的是,pivoted column需要静态添加ie(product_id)。如果你想,那么你可以创建一个动态查询来适应这些旋转的列。 因为它与SQL服务器的功能标签我的回答是从SQL Server语法。

 declare @order as table (
     id int , ship_addr varchar(100),ship_zip varchar(100) , ship_country varchar(100) 
     ) 

     insert into @order (id,ship_addr,ship_zip,ship_country) values (1,'Add1','123','DE') 
     insert into @order (id,ship_addr,ship_zip,ship_country) values (2,'Add2','321','DE') 
     insert into @order (id,ship_addr,ship_zip,ship_country) values (3,'Add1','123','DE') 
     insert into @order (id,ship_addr,ship_zip,ship_country) values (4,'Add2','321','DE') 
     insert into @order (id,ship_addr,ship_zip,ship_country) values (5,'Add3','543','DE') 

     declare @orderposition as table (
     id int , order_id int ,product_id int , quantity int 
     ) 

     insert into @orderposition (id,order_id,product_id,quantity) values (1,1,1,5) 
     insert into @orderposition (id,order_id,product_id,quantity) values (2,1,2,10) 
     insert into @orderposition (id,order_id,product_id,quantity) values (3,2,3,5) 
     insert into @orderposition (id,order_id,product_id,quantity) values (4,3,1,5) 
     insert into @orderposition (id,order_id,product_id,quantity) values (5,4,1,10) 
     insert into @orderposition (id,order_id,product_id,quantity) values (6,5,1,10) 

     select pvt.fulladd as Address , ISNULL([1],0) as [1] , ISNULL([2],0) as [2] , ISNULL([3],0) as [3] from 
     (select fulladd, product_id , sum(isnull(quantity,0)) as qty from (
     select (o.ship_addr +' , ' + o.ship_zip +' ,' + o.ship_country) as fulladd , o.id from @order o 
     ) as o 
     inner join @orderposition p on p.order_id= o.id 
     group by fulladd, p.product_id 
     ) as final 



     pivot 
     (
      max(qty) for product_id in ([1],[2],[3]) 

     ) as pvt 

enter image description here

+0

嗨我更新了空检查,以及请检查我更新的答案。我希望这是你想要的东西:) –

+0

可能是一个很好的答案为SQL Server,但MySQL不支持表变量,或转动。 –

with cte as (
    select t1.product_id, sum(t1.quantity) as quantity, t2.addr from order_positions t1 
    inner join (select id, ship_addr+', '+convert(varchar,ship_zip)+', '+ship_country as addr from order) t2 
    on t2.id = t1.order_id group by t2.addr, t1.product_id) 
    select addr, isnull([1],0), isnull([2],0),isnull([3],0) from cte 
    pivot (sum(quantity) for [product_id] in ([1],[2],[3])) as pivottable 
+0

你能解释一下吗? – Troyer

+0

第三行的select语句为您提供您需要的完整地址。所以用普通表格表达式,按地址和产品ID分组计算数量总和。最后一条语句只是以您的格式获取结果为目的。我希望这个评论清楚你。 –

+0

可能是一个很好的答案为SQL服务器,但MySQL不支持与透视语句。 –

做到这一点的方法是使用组聚集 - 请注意,如果你有你可能需要调用动态sql更多的产品。

select concat(ship_addr,char(44),ship_zip,char(44),ship_country) address, 
      sum(case when product_id = 1 then quantity else 0 end) as p1, 
      sum(case when product_id = 2 then quantity else 0 end) as p2, 
      sum(case when product_id = 3 then quantity else 0 end) as p3 
    from orderaddress oa 
    join orderposition op 
    where op.order_id = oa.id 
    group by ship_addr,ship_zip,ship_country 
+0

问题是我需要采取一个动态的方式product_id。 – Troyer