简体   繁体   English

如何从单行创建多行(SQL Server 2008)

[英]How can I create multiple rows from a single row (sql server 2008)

In our organisation we have a central purchasing company (CPC) who then sells to our retail company (Company_X) via an intercompany PO, who then sells on to the customer. 在我们的组织中,我们有一个中央采购公司(CPC),然后通过公司间采购订单将其出售给我们的零售公司(Company_X),然后再将其销售给客户。 What I need to do is link our retail sale back to the original purchase order. 我需要做的是将我们的零售链接回原始的采购订单。

For example I have a table which contains the following (and a multitude of other columns): 例如,我有一个表,其中包含以下内容(以及许多其他列):

Company_X_Sales : Company_X_Sales

InterCO_PO_no  Sales_Order_No  Part_No  Qty
-------------  --------------  -------  ---
12345          98765           ABCD     10

I then have a table which has the following: 然后,我有一个具有以下内容的表:

CPC_Sales : CPC_Sales

PO_Number  InterCO_SO_No  Part_No  Qty
---------  -------------  -------  ---
00015      12345          ABCD     5  
00012      12345          ABCD     2  
00009      12345          ABCD     4   
00007      12345          ABCD     3 

So you can see that the final sale of 10 items was made up of parts which came from more than 1 external POs in the central company. 因此,您可以看到最终的10件商品的销售是由来自中央公司1个以上外部PO的零件组成的。

What I need to be able to do is replicate the rows in Company_X_Sales , include the Original PO Number and set the quantities as in CPC_Sales . 我需要做的是复制Company_X_Sales的行,包括原始PO编号,并将数量设置为CPC_Sales

I need to end up with something like this: 我需要结束这样的事情:

Company_X_Sales_EXTD : Company_X_Sales_EXTD

PO_Number  InterCO_PO_no  Sales_Order_No  Part_No  Qty
---------  -------------  --------------  -------  ---
00007      12345          98765           ABCD     3
00009      12345          98765           ABCD     4
00012      12345          98765           ABCD     2
00015      12345          98765           ABCD     1

I have to use the Company_X_Sales as my driving table - the CPC_Sales is simply as a lookup to derive the original PO Number. 我必须使用Company_X_Sales作为我的驾驶表CPC_Sales只是作为查找以获取原始PO编号的查找。

Hoping you can help I am working through the weekend on this as it is part of a piece of work which has a very aggressive timescale. 希望您能帮我整个周末工作,因为这是一项工作量很大的工作的一部分。

I do not mind if the solution requires more than one pass of the table or creation of views if needed. 我不介意该解决方案是否需要多次通过表格或根据需要创建视图。 I am just really really struggling. 我真的很挣扎。

I'm a little confused by your question, but it sounds like you're trying to make your Company_X_Sales table have 3 rows instead of 1, just with varying quantities? 您的问题让我有些困惑,但是听起来您正在尝试使Company_X_Sales表具有3行而不是1行,而行数却有所不同? If so, something like this should work: 如果是这样,类似这样的事情应该起作用:

SELECT S.PO_Number, C.InterCO_PO_no, C.Sales_Order_No, C.Part_No, S.Qty
FROM Company_X_Sales C
   JOIN CPC_Sales S ON C.InterCO_PO_no = S.InterCO_SO_No

Here is the SQL Fiddle . 这是SQL Fiddle

That will give you the 4 rows with the correct quantities. 这将为您提供正确数量的4行。 Then you can delete and reinsert accordingly. 然后,您可以相应地删除并重新插入。

To get those rows into the table, you have a few options, but something like this should work: 要将这些行放入表中,您可以有一些选择,但是这样的方法应该起作用:

--Flag the rows for deletion
UPDATE Company_X_Sales SET Qty = -1 -- Or some arbitrary value that does not exist in the table

--Insert new correct rows
INSERT INTO Company_X_Sales 
SELECT C.InterCO_PO_no, C.Sales_Order_No, C.Part_No, S.Qty
FROM Company_X_Sales C
   JOIN CPC_Sales S ON C.InterCO_PO_no = S.InterCO_SO_No

--Cleanup flagged rows for deletion
DELETE FROM Company_X_Sales  WHERE Qty = -1

Good luck. 祝好运。

select [PO_Number],[InterCO_SO_No], Company_X_Sales.Sales_Order_No, [Part_No],[Qty] from CPC_Sales inner join Company_X_Sales on Company_X_Sales.InterCO_PO_no = CPC_Sales.InterCO_SO_no

在两个表上进行简单的内部联接将为您提供所需的结果恕我直言。

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

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