SQL How to UPDATE Column via subquery with condition

时间:2017-07-12 08:18:36

标签: sql-server

Is it possible to update my a column in my table via subquery? the unique value is [ID] and [Date] How can I do this. So far I tried it like this but failed

UPDATE [dbo].[SCPL_EOD]
SET [CSP] = (SELECT 
  D.CSP FROM
(
  SELECT PDT.CallID,SCP.*
  ,ROW_NUMBER() OVER(PARTITION BY SCP.[MIN],[ACCOUNT_NUMBER] ORDER BY SCP.[DATE_TIME] DESC) as [Row]  FROM
  (
  SELECT *
  FROM [SCPL].[dbo].[SCPL_EOD]
  WHERE [SWITCH_RESULT] = 'ANSWERED'
  AND [Date] BETWEEN @DateFrom AND @DateTo
  ) SCP
  LEFT JOIN
  (
  SELECT 
  SUBSTRING([MIN],2,LEN([MIN])) as [MIN]
  ,CallID
  ,[Date]
  ,ROW_NUMBER() OVER(PARTITION BY [MIN],[PDcampaignBatch] ORDER BY PDTransaction.[SubmittedDateTime] DESC) as [Row] 
  FROM [Telemarketing].[dbo].[PDTransaction]
  WHERE [Date] BETWEEN @DateFrom AND @DateTo
  ) PDT ON SCP.[MIN] = PDT.MIN AND SCP.[Date] = PDT.[DATE] AND PDT.[Row] = 1
  )a 
 LEFT JOIN [Telemarketing].[dbo].[Disposition] d ON a.CallID = d.[Call ID]
  WHERE a.[Row] = 1
) a
WHERE [MIN] = 

1 个答案:

答案 0 :(得分:1)

您从派生表更新的格式不正确,格式为:

var json = [{
  "hello": {
    "world": 1,
    "world2": 2
  },
  "bye": {
    "world": 1,
    "world2": 2
  }
}];

var indexes = [0, "hello", "world2"];
var value = "value";

indexes.slice(0,-1).reduce((obj, index) => obj[index], json)[indexes.pop()] = value;
console.log(json);

但我建议使用临时表,如果这是可能的话,因为它使可读性更容易。