如何从相同的表中为各个ID更新Opportunity_Record_Type,Opportunity_Division和Fiscal_Period? PK是Opportunity_ID。
+--------------------+-------------------------+----------------------+---------------+-----------------------+--------------------------+
| Opportunity_ID | Opportunity_Record_Type | Opportunity_Division | Fiscal_Period | Script_Execution_Week | Script_Execution_Quarter |
+--------------------+-------------------------+----------------------+---------------+-----------------------+--------------------------+
| LKASDJKLkasdkl23k4 | NULL | NULL | NULL | 2017-21 | 2017-Q2 |
| LKASDJKLkasdkl23k4 | NULL | NULL | NULL | 2017-22 | 2017-Q2 |
| LKASDJKLkasdkl23k4 | NULL | NULL | NULL | 2017-23 | 2017-Q2 |
| LKASDJKLkasdkl23k4 | NULL | NULL | NULL | 2017-24 | 2017-Q2 |
| LKASDJKLkasdkl23k4 | NULL | NULL | NULL | 2017-25 | 2017-Q2 |
| LKASDJKLkasdkl23k4 | Services | SBS | 2017-Q2 | 2017-26 | 2017-Q3 |
| LKASDJKLkasdkl23k4 | Services | SBS | 2017-Q2 | 2017-27 | 2017-Q3 |
| LKASDJKLkasdkl23k4 | Services | SBS | 2017-Q2 | 2017-28 | 2017-Q3 |
| LKASDJKLkasdkl23k4 | NULL | NULL | NULL | 2017-7 | 2017-Q1 |
| LKASDJKLkasdkl23k4 | NULL | NULL | NULL | 2017-8 | 2017-Q1 |
| LKASDJKLkasdkl23k4 | NULL | NULL | NULL | 2017-9 | 2017-Q1 |
| SDFSDKLJ2ELKMLKASM | NULL | NULL | NULL | 2017-20 | 2017-Q2 |
| SDFSDKLJ2ELKMLKASM | NULL | NULL | NULL | 2017-21 | 2017-Q2 |
| SDFSDKLJ2ELKMLKASM | NULL | NULL | NULL | 2017-22 | 2017-Q2 |
| SDFSDKLJ2ELKMLKASM | NULL | NULL | NULL | 2017-23 | 2017-Q2 |
| SDFSDKLJ2ELKMLKASM | NULL | NULL | NULL | 2017-24 | 2017-Q2 |
| SDFSDKLJ2ELKMLKASM | NULL | NULL | NULL | 2017-25 | 2017-Q2 |
| SDFSDKLJ2ELKMLKASM | Mobile | Mobile | 2017-Q2 | 2017-26 | 2017-Q3 |
| SDFSDKLJ2ELKMLKASM | Mobile | Mobile | 2017-Q2 | 2017-27 | 2017-Q3 |
| SDFSDKLJ2ELKMLKASM | Mobile | Mobile | 2017-Q2 | 2017-28 | 2017-Q3 |
+--------------------+-------------------------+----------------------+---------------+-----------------------+--------------------------+
答案 0 :(得分:1)
答案 1 :(得分:0)
根据您的评论更新。我相信你需要一个内部联接来实现你想要做的事情。请将您的表名放在我写的' YourTableName'的任何地方。您将加入3个不同的子查询,这将为该特定ID提取该列的不同值。
UPDATE test
SET Opportunity_Record_Type = join1.Opportunity_Record_Type,
Opportunity_Division = join2.Opportunity_Division,
Fiscal_Period = join3.Fiscal_Period
FROM 'YourTableName' test
INNER JOIN (
SELECT DISTINCT Opportunity_ID, Opportunity_Record_Type
FROM 'YourTableName'
WHERE Opportunity_Record_Type IS NOT NULL) join1 ON join1.Opportunity_ID = test.Opportunity_ID
INNER JOIN (
SELECT DISTINCT Opportunity_ID, Opportunity_Division
FROM 'YourTableName'
WHERE Opportunity_Division IS NOT NULL) join2 ON join2.Opportunity_ID = test.Opportunity_ID
INNER JOIN (SELECT DISTINCT Opportunity_ID, Fiscal_Period
FROM 'YourTableName'
WHERE Fiscal_Period IS NOT NULL) join3 ON join3.Opportunity_ID = test.Opportunity_ID
答案 2 :(得分:0)
以下是您的示例。最好通过插入获得,当有重复键时更新值。我用虚拟数据填充它,因为我不知道有哪些有效输入。
INSERT INTO table (Opportunity_ID,Opportunity_Record_Type,Opportunity_Division,Fiscal_Period)
VALUES (PK,1,1,1),
(PK,1,1,1),
(PK,1,1,1),
(PK,1,1,1)
ON DUPLICATE KEY UPDATE Opportunity_Record_Type=1,Opportunity_Division=1,...;