gpt4 book ai didi

mysql - 连接两个表加上数据透视表作为存储过程 - MySQL

转载 作者:太空宇宙 更新时间:2023-11-03 11:40:28 24 4
gpt4 key购买 nike

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

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

最佳答案

您好,我想出了这个解决方案,只是缺少一个部分来用零替换 null 。当我热更多时间时,我会尝试,但结果符合您的期望。也附上输出。这里值得一提的是,pivoted column需要静态添加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

关于mysql - 连接两个表加上数据透视表作为存储过程 - MySQL,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/42157770/

24 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com