【问题标题】:Join two tables plus pivot table as a stored-procedure - MySQL将两个表和数据透视表连接为存储过程 - MySQL
【发布时间】:2017-02-10 11:02:15
【问题描述】:

我正在尝试在 MySQL 上创建一个订单程序来创建一个数据透视表,但这真的很复杂,我不知道该怎么做,我不知道是否有其他方法做起来更容易。

我需要获取在特定地址购买了多少产品,GROUP_CONCAT(ship_addr + ship_zip + ship_contry) 的结果。

订单表 - 包含订单地址。

+----+----------------+----------+--------------+
| 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 |
+-----------------+-------+-------+-------+

我无法复制我尝试过的存储过程,因为它们完全失败了。

【问题讨论】:

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

标签: mysql stored-procedures mysql-workbench


【解决方案1】:

您好,我想出了这个解决方案,只是缺少一个部分来将 null 替换为 zero 。当我有更多时间时,我会尝试,但结果符合您的期望。附上输出为好。这里值得一提的是,旋转列需要静态添加 ie(product_id)。如果您愿意,您也可以创建一个动态查询来容纳这些透视列。 因为它是用 sql-server 标记的,所以我的答案来自 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

【讨论】:

  • 您好,我也更新了空检查,请检查我更新的答案。我希望这是你想要的:)
  • 对于 sql server 可能是一个很好的答案,但 mysql 不支持表变量或数据透视表。
【解决方案2】:
    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

【讨论】:

  • 你能解释一下吗?
  • 第三行的 select 语句为您提供了所需的完整地址。因此,使用公用表表达式,按地址和产品 ID 分组计算数量的总和。最后一条语句只是以您的格式获取结果的关键。我希望这条评论能让你明白。
  • 对于 sql server 可能是一个很好的答案,但 mysql 不支持 with 或 pivot 语句。
【解决方案3】:

一种方法是使用组聚合 - 请注意,如果您有更多产品,您可能需要调用动态 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

【讨论】:

  • 问题是我需要以动态方式获取product_id。
猜你喜欢
  • 1970-01-01
  • 2018-10-29
  • 2019-02-19
  • 1970-01-01
  • 1970-01-01
  • 2013-06-09
  • 2020-06-20
  • 1970-01-01
  • 1970-01-01
相关资源
最近更新 更多