简体   繁体   English

联接两个表和数据透视表作为存储过程-MySQL

[英]Join two tables plus pivot table as a stored-procedure - MySQL

I'm trying to make a order procedure on MySQL to make a pivot table as a result, but it is really complicated and I've no clue how to do it, I don't know if there is another way to do it more easy. 我正在尝试在MySQL上执行订购程序以创建数据透视表,但是它确实很复杂,我不知道如何执行此操作,我不知道是否还有另一种方法可以执行更多操作简单。

I need to get how many products has been bought in specifics addresses, result of GROUP_CONCAT(ship_addr + ship_zip + ship_contry). 我需要获取在GROUP_CONCAT(ship_addr + ship_zip + ship_contry)的特定地址中购买了多少产品。

Order table - Containing the order address. 订单表 -包含订单地址。

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

Order positions table 订单头寸表

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

The expected result, column headers are product_id: 预期结果,列标题为product_id:

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

I can't copy the store procedures I tried because they are completely failures. 我无法复制尝试的存储过程,因为它们完全是失败的。

Hi I come up with this solution just one part is missing to replace null with zero . 嗨,我想出了这个解决方案,只是缺少了一个部分,无法将null替换为零。 i will try when I hot more time but the result is as per your expectation. 我会花更多的时间尝试尝试,但是结果符合您的期望。 Attached the out put as well . 以及附带的输出。 here it is worth to mention that the pivoted column needs ie(product_id) to be added statically . 这里值得一提的是,枢轴列需要ie(product_id)进行静态添加。 If you want then you can created a dynamic query to accommodated these pivoted columns as well . 如果需要,您还可以创建一个动态查询来容纳这些透视列。 Since it is taged with sql-server my answer is from SQL Server syntax . 由于它是用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

在此处输入图片说明

    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

A way to do this is using group aggregation - note if you have more products you may need to invoke dynamic sql. 一种方法是使用组聚合-请注意,如果您有更多产品,则可能需要调用动态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

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM