INSERT INTO OrderNew (
SalesOrderID,
OrderDate,
DueDate,
ShipDate,
OnlineOrderFlag,
CustomerID,
CreditCardID,
SubTotal,
TaxAmt,
Freight,
TotalDue,
OrderInfo.salesorderdetail,
OrderInfo.orderqty ,
OrderInfo.productid,
OrderInfo.unitprice,
OrderInfo.unitpricediscount,
OrderInfo.linetotal
)
SELECT h."SalesOrderID" , h."OrderDate", h."DueDate", h."ShipDate", h."OnlineOrderFlag", h."CustomerID", h."CreditCardID", h."SubTotal", h."TaxAmt", h."Freight", h."TotalDue", d."SalesOrderDetailID", d."OrderQty", d."ProductID", d."UnitPrice", d."UnitPriceDiscount", d."LineTotal"
FROM "SalesOrderHeader" h
INNER JOIN "SalesOrderDetail" d ON d."SalesOrderID" = h."SalesOrderID"
WHERE d."SalesOrderID" = h."SalesOrderID" ;
这是我用来从其他一些表导入数据的代码。 但是,我希望最后一列像嵌套表。 这些是我想成为一个的表。因此,对于表格中的一个销售订单ID" SalesOrderHeader"表格上有几个或多个具有相同销售订单ID的记录" SalesOrderDetail"。那么,我该怎么做呢?使用函数?
我不知道我是否可以理解。但我可以提供更多信息。
答案 0 :(得分:0)
您可以创建两种类型,以便将新表缩小,该表将是SalesOrderHeader和SalesOrderDetail的组合。
CREATE TYPE Shipping AS(OrderDate Date,DueDate Date,ShipDate Date);
CREATE TYPE ordersdetails AS (details text);
然后你创建表。
CREATE TABLE ordernew ( salesorderid serial NOT NULL, shippinginfo shipping, onlineorderflag integer, customerid serial NOT NULL, creditcardid integer, subtotal real, taxamt real, freight real, totaldue real, orderinfo ordersdetails[]);
对于数据,您只需复制其他两个表中的数据,但编写正确的查询以便不创建重复记录。所以,代码如下。
INSERT INTO OrderNew (
SalesOrderID,
OrderDate,
DueDate,
ShipDate,
OnlineOrderFlag,
CustomerID,CreditCardID,
SubTotal,
TaxAmt,
Freight,
TotalDue,
OrderInfo.salesorderdetail,
OrderInfo.orderqty ,
OrderInfo.productid,
OrderInfo.unitprice,
OrderInfo.unitpricediscount,
OrderInfo.linetotal
)
SELECT h."SalesOrderID" , h."OrderDate", h."DueDate", h."ShipDate", h."OnlineOrderFlag",
h."CustomerID", h."CreditCardID", h."SubTotal", h."TaxAmt", h."Freight", h."TotalDue",
d."SalesOrderDetailID", d."OrderQty", d."ProductID", d."UnitPrice", d."UnitPriceDiscount", d."LineTotal"
FROM "SalesOrderHeader" h
INNER JOIN "SalesOrderDetail" d ON d."SalesOrderID" = h."SalesOrderID"
WHERE d."SalesOrderID" = h."SalesOrderID" ;
要简单地查看最近插入的数据,请运行以下查询:
SELECT orderinfo FROM ordernew;