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

时间:2017-02-10 11:02:16

标签: mysql stored-procedures mysql-workbench

我正在尝试在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 |
+-----------------+-------+-------+-------+

我无法复制我尝试的商店程序,因为它们完全失败了。

3 个答案:

答案 0 :(得分:1)

嗨,我想出了这个解决方案,只有一部分缺失,用零替换null。我会在我趁热时间的时候尝试,但结果是按照你的期望。附上了输出。这里值得一提的是,旋转列需要静态添加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

enter image description here

答案 1 :(得分:1)

    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

答案 2 :(得分:1)

执行此操作的方法是使用组聚合 - 请注意,如果您有更多产品,则可能需要调用动态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