Php 根据查询中的特定分组插入到两个表中
我正在编写一个脚本,试图从查询中获取结果:Php 根据查询中的特定分组插入到两个表中,php,sql,db2,Php,Sql,Db2,我正在编写一个脚本,试图从查询中获取结果: SELECT OrdNumber, Detail1, Detail2, Detail3, Date, Quantity, Customer, OrderingCode FROM Orders o INNER JOIN (SELECT os.*, row_number() over
SELECT
OrdNumber,
Detail1,
Detail2,
Detail3,
Date,
Quantity,
Customer,
OrderingCode
FROM Orders o
INNER JOIN (SELECT os.*,
row_number() over (partition by Detail1, Detail2,Detail3 ) as seqnum
FROM orderSheets os
) os
on o.product = os.product and
o.Detail2 = os.Detail2 and
o.Detail3 = os.Detail3
ID | Detail1 | Detail2 | Detail3 | Customer | OrderingCode | Date
----------------------------------------------------------------------------
1001 122 123 12 123 567 12/12/2018
1002 122 123 15 123 567 12/12/2018
1003 516 123 63 123 567 12/12/2018
1004 617 123 14 123 567 12/12/2018
1005 617 123 17 123 567 12/12/2018
1006 745 123 43 123 567 12/12/2018
P_ID | Quantity | Date | orderNumber
-------------------------------------------------
1001 2 12/12/2018 12345
1002 2 12/12/2018 12345
1003 5 12/12/2018 12345
1004 7 12/12/2018 12345
1005 2 12/12/2018 12345
1006 2 12/12/2018 12345
并根据特定条件从中获取必要的数据,并将相关记录插入两个表中
ID | Detail1 | Detail2 | Detail3 | Customer | OrderingCode | Date
----------------------------------------------------------------------------
1001 122 123 12 123 567 12/12/2018
1002 122 123 15 123 567 12/12/2018
1003 516 123 63 123 567 12/12/2018
1004 617 123 14 123 567 12/12/2018
1005 617 123 17 123 567 12/12/2018
1006 745 123 43 123 567 12/12/2018
P_ID | Quantity | Date | orderNumber
-------------------------------------------------
1001 2 12/12/2018 12345
1002 2 12/12/2018 12345
1003 5 12/12/2018 12345
1004 7 12/12/2018 12345
1005 2 12/12/2018 12345
1006 2 12/12/2018 12345
我想根据该订单上该客户的不同Detail1、Detail2和Detail3对每条记录进行分组来插入,然后如果我有多条记录用于同一Detail1/Detail2/Detail3/customer唯一分组,则还要查看最高数量
ID | Detail1 | Detail2 | Detail3 | Customer | OrderingCode | Date
----------------------------------------------------------------------------
1001 122 123 12 123 567 12/12/2018
1002 122 123 15 123 567 12/12/2018
1003 516 123 63 123 567 12/12/2018
1004 617 123 14 123 567 12/12/2018
1005 617 123 17 123 567 12/12/2018
1006 745 123 43 123 567 12/12/2018
P_ID | Quantity | Date | orderNumber
-------------------------------------------------
1001 2 12/12/2018 12345
1002 2 12/12/2018 12345
1003 5 12/12/2018 12345
1004 7 12/12/2018 12345
1005 2 12/12/2018 12345
1006 2 12/12/2018 12345
通过上面的查询,假设我返回以下结果:
OrdNumber | Detail1 | Detail2 | Detail3 | Date | Quantity | Customer | OrderingCode
-----------------------------------------------------------------------------------------
12345 122 123 12 12/12/2018 2 123 567
12345 122 123 15 12/12/2018 2 123 567
12345 516 123 63 12/12/2018 5 123 567
12345 617 123 14 12/12/2018 7 123 567
12345 617 123 14 12/12/2018 4 123 567
12345 617 123 17 12/12/2018 2 123 567
12345 745 123 43 12/12/2018 2 123 567
ID | Detail1 | Detail2 | Detail3 | Customer | OrderingCode | Date
----------------------------------------------------------------------------
1001 122 123 12 123 567 12/12/2018
1002 122 123 15 123 567 12/12/2018
1003 516 123 63 123 567 12/12/2018
1004 617 123 14 123 567 12/12/2018
1005 617 123 17 123 567 12/12/2018
1006 745 123 43 123 567 12/12/2018
P_ID | Quantity | Date | orderNumber
-------------------------------------------------
1001 2 12/12/2018 12345
1002 2 12/12/2018 12345
1003 5 12/12/2018 12345
1004 7 12/12/2018 12345
1005 2 12/12/2018 12345
1006 2 12/12/2018 12345
大多数结果都是不同的,但我有两条记录,它们共享该订单上该客户的相同detail1、detail2和detail3(617、123、14),因此在这种情况下,我只想在products表中为这两个结果插入一条记录(不同的详细信息、客户编号、订单编号、日期和orderingCode)和orders表中的一条与外部相关的记录,但我只想插入这两条记录中数量最高的一条(因此在该示例中,我将插入订单号、外键(即产品ID)和7作为数量,因为它是两条记录中数量最多的记录,分别为7和4)
ID | Detail1 | Detail2 | Detail3 | Customer | OrderingCode | Date
----------------------------------------------------------------------------
1001 122 123 12 123 567 12/12/2018
1002 122 123 15 123 567 12/12/2018
1003 516 123 63 123 567 12/12/2018
1004 617 123 14 123 567 12/12/2018
1005 617 123 17 123 567 12/12/2018
1006 745 123 43 123 567 12/12/2018
P_ID | Quantity | Date | orderNumber
-------------------------------------------------
1001 2 12/12/2018 12345
1002 2 12/12/2018 12345
1003 5 12/12/2018 12345
1004 7 12/12/2018 12345
1005 2 12/12/2018 12345
1006 2 12/12/2018 12345
因此,根据上面的结果集,我希望生成两个表的插入,如下所示:
ID | Detail1 | Detail2 | Detail3 | Customer | OrderingCode | Date
----------------------------------------------------------------------------
1001 122 123 12 123 567 12/12/2018
1002 122 123 15 123 567 12/12/2018
1003 516 123 63 123 567 12/12/2018
1004 617 123 14 123 567 12/12/2018
1005 617 123 17 123 567 12/12/2018
1006 745 123 43 123 567 12/12/2018
P_ID | Quantity | Date | orderNumber
-------------------------------------------------
1001 2 12/12/2018 12345
1002 2 12/12/2018 12345
1003 5 12/12/2018 12345
1004 7 12/12/2018 12345
1005 2 12/12/2018 12345
1006 2 12/12/2018 12345
产品
ID | Detail1 | Detail2 | Detail3 | Customer | OrderingCode | Date
----------------------------------------------------------------------------
1001 122 123 12 123 567 12/12/2018
1002 122 123 15 123 567 12/12/2018
1003 516 123 63 123 567 12/12/2018
1004 617 123 14 123 567 12/12/2018
1005 617 123 17 123 567 12/12/2018
1006 745 123 43 123 567 12/12/2018
P_ID | Quantity | Date | orderNumber
-------------------------------------------------
1001 2 12/12/2018 12345
1002 2 12/12/2018 12345
1003 5 12/12/2018 12345
1004 7 12/12/2018 12345
1005 2 12/12/2018 12345
1006 2 12/12/2018 12345
命令
ID | Detail1 | Detail2 | Detail3 | Customer | OrderingCode | Date
----------------------------------------------------------------------------
1001 122 123 12 123 567 12/12/2018
1002 122 123 15 123 567 12/12/2018
1003 516 123 63 123 567 12/12/2018
1004 617 123 14 123 567 12/12/2018
1005 617 123 17 123 567 12/12/2018
1006 745 123 43 123 567 12/12/2018
P_ID | Quantity | Date | orderNumber
-------------------------------------------------
1001 2 12/12/2018 12345
1002 2 12/12/2018 12345
1003 5 12/12/2018 12345
1004 7 12/12/2018 12345
1005 2 12/12/2018 12345
1006 2 12/12/2018 12345
有点复杂,但请按原样尝试:
ID | Detail1 | Detail2 | Detail3 | Customer | OrderingCode | Date
----------------------------------------------------------------------------
1001 122 123 12 123 567 12/12/2018
1002 122 123 15 123 567 12/12/2018
1003 516 123 63 123 567 12/12/2018
1004 617 123 14 123 567 12/12/2018
1005 617 123 17 123 567 12/12/2018
1006 745 123 43 123 567 12/12/2018
P_ID | Quantity | Date | orderNumber
-------------------------------------------------
1001 2 12/12/2018 12345
1002 2 12/12/2018 12345
1003 5 12/12/2018 12345
1004 7 12/12/2018 12345
1005 2 12/12/2018 12345
1006 2 12/12/2018 12345
create table Products (
ID int not null generated always as identity primary key
, Detail1 int not null
, Detail2 int not null
, Detail3 int not null
, Customer int not null
, OrderingCode int not null
, Date date not null
) in userspace1;
create table Orders(
P_ID int not null
, Quantity int not null
, Date date not null
, orderNumber int not null
, constraint orders_fk foreign key (p_id) references Products (id)
) in userspace1;
with r (OrdNumber, Detail1, Detail2, Detail3, Date, Quantity, Customer, OrderingCode) as (values
(12345, 122, 123, 12, '12/12/2018', 2, 123, 567)
, (12345, 122, 123, 15, '12/12/2018', 2, 123, 567)
, (12345, 516, 123, 63, '12/12/2018', 5, 123, 567)
, (12345, 617, 123, 14, '12/12/2018', 7, 123, 567)
, (12345, 617, 123, 14, '12/12/2018', 4, 123, 567)
, (12345, 617, 123, 17, '12/12/2018', 2, 123, 567)
, (12345, 745, 123, 43, '12/12/2018', 2, 123, 567)
)
, p as (
select ID, Quantity, Date, ordNumber
from new table (
insert into products (Detail1, Detail2, Detail3, Customer, OrderingCode, Date)
include (OrdNumber int, Quantity int)
select Detail1, Detail2, Detail3, Customer, OrderingCode, Date, OrdNumber, Quantity
from (
select Detail1, Detail2, Detail3, Customer, OrderingCode, Date, OrdNumber, Quantity
, rownumber() over (partition by Detail1, Detail2, Detail3 order by Quantity desc) rn_
from r
) where rn_=1
)
)
select count(1)
from new table (
insert into Orders (P_ID, Quantity, Date, orderNumber)
select ID, Quantity, Date, ordNumber
from p
);
在第一个WITH
子句(引用为r
)中,有一个查询结果集。
然后使用数据更改操作中的耦合选择
ID | Detail1 | Detail2 | Detail3 | Customer | OrderingCode | Date
----------------------------------------------------------------------------
1001 122 123 12 123 567 12/12/2018
1002 122 123 15 123 567 12/12/2018
1003 516 123 63 123 567 12/12/2018
1004 617 123 14 123 567 12/12/2018
1005 617 123 17 123 567 12/12/2018
1006 745 123 43 123 567 12/12/2018
P_ID | Quantity | Date | orderNumber
-------------------------------------------------
1001 2 12/12/2018 12345
1002 2 12/12/2018 12345
1003 5 12/12/2018 12345
1004 7 12/12/2018 12345
1005 2 12/12/2018 12345
1006 2 12/12/2018 12345
第一个(引用为p
)将所需的行插入产品
,并为这些行生成ID
s
ID | Detail1 | Detail2 | Detail3 | Customer | OrderingCode | Date
----------------------------------------------------------------------------
1001 122 123 12 123 567 12/12/2018
1002 122 123 15 123 567 12/12/2018
1003 516 123 63 123 567 12/12/2018
1004 617 123 14 123 567 12/12/2018
1005 617 123 17 123 567 12/12/2018
1006 745 123 43 123 567 12/12/2018
P_ID | Quantity | Date | orderNumber
-------------------------------------------------
1001 2 12/12/2018 12345
1002 2 12/12/2018 12345
1003 5 12/12/2018 12345
1004 7 12/12/2018 12345
1005 2 12/12/2018 12345
1006 2 12/12/2018 12345
第二个(最新的)使用生成的ID和其他需要的字段将行插入到订单中。太好了。问题是什么?如何基于该示例编写插入,以便插入所需的数据,如果有多条记录具有相同的客户,detail1、detail2、detail3,那么我只能为其插入一条记录,并使用每个记录中的最高数量?我认为max(quantity)
和按客户分组,detail1、detail2,detail3
可能是查询的一部分。插入行的ID/P\u ID
值来自何处?products表中的ID是作为外键用于P\u ID的主键谢谢,我将很快尝试此操作。但我是否需要将MAX添加到数量中?如果对任何记录进行分组,我只需要最高数量使用的子选择,其中rn_u=1
子句为每个Detail1、Detail2、Detail3
组留下具有最高数量的唯一行。
ID | Detail1 | Detail2 | Detail3 | Customer | OrderingCode | Date
----------------------------------------------------------------------------
1001 122 123 12 123 567 12/12/2018
1002 122 123 15 123 567 12/12/2018
1003 516 123 63 123 567 12/12/2018
1004 617 123 14 123 567 12/12/2018
1005 617 123 17 123 567 12/12/2018
1006 745 123 43 123 567 12/12/2018
P_ID | Quantity | Date | orderNumber
-------------------------------------------------
1001 2 12/12/2018 12345
1002 2 12/12/2018 12345
1003 5 12/12/2018 12345
1004 7 12/12/2018 12345
1005 2 12/12/2018 12345
1006 2 12/12/2018 12345