2015-11-05 173 views
1

示例代碼:轉換UPDATE語句的SELECT語句

UPDATE 
TEMP_TABLE_NTI_D_test1 TEMP_NTID 
SET 
TEMP_NTID.WEEKLY_FLAG='Y', 
TEMP_NTID.PROGRAM_COUNT=0, 
FROM 
(
    SELECT 
* 
FROM 
(
    SELECT 
     st_date, 
     NET_CODE, 
     PR_CODE, 
     ST_HR, 
     ST_MIN, 
     sId, 
     DURATION, 
     REC_FLAG, 
     ROW_NUMBER() OVER(PARTITION BY NET_CODE,PR_CODE, 
    REC_FLAG,st_date,sId 
    ORDER BY ST_HR,ST_MIN) ROW_NUM 
    FROM 
     TEMP_TABLE_NTI_D_test1 
    WHERE 
    BRK_IND = 0 
    AND GAP_IND = 'Y' 
    AND COM_IND != 'Y') AB 
WHERE 
ROW_NUM > 1) TEMP_TABLE_GAPPED_ZEROES 
WHERE 
TEMP_NTID.NET_CODE = TEMP_TABLE_GAPPED_ZEROES.NET_CODE 
AND TEMP_NTID.PR_CODE = TEMP_TABLE_GAPPED_ZEROES.PR_CODE 
AND TEMP_NTID.ST_HR = TEMP_TABLE_GAPPED_ZEROES.ST_HR 
AND TEMP_NTID.ST_MIN = TEMP_TABLE_GAPPED_ZEROES.ST_MIN 
AND TEMP_NTID.DURATION = TEMP_TABLE_GAPPED_ZEROES.DURATION 
AND TEMP_NTID.REC_FLAG = TEMP_TABLE_GAPPED_ZEROES.REC_FLAG 
AND TEMP_NTID.st_date = TEMP_TABLE_GAPPED_ZEROES.st_date 
; 

不是更新該列的我想收到的SELECT語句給每個行的讀數(如果它是一個將被更新UPDATE語句)。

回答

1

這是從更新中最直接的翻譯。

select 
    TEMP_NTID.* --Select the table's records instead of update. 
FROM 
(
    SELECT 
* 
FROM 
(
    SELECT 
     st_date, 
     NET_CODE, 
     PR_CODE, 
     ST_HR, 
     ST_MIN, 
     sId, 
     DURATION, 
     REC_FLAG, 
     ROW_NUMBER() OVER(PARTITION BY NET_CODE,PR_CODE, 
    REC_FLAG,st_date,sId 
    ORDER BY ST_HR,ST_MIN) ROW_NUM 
    FROM 
     TEMP_TABLE_NTI_D_test1 
    WHERE 
    BRK_IND = 0 
    AND GAP_IND = 'Y' 
    AND COM_IND != 'Y') AB 
WHERE 
ROW_NUM > 1) TEMP_TABLE_GAPPED_ZEROES 
,TEMP_TABLE_NTI_D_test1 TEMP_NTID --Move the updated table here. 
WHERE 
TEMP_NTID.NET_CODE = TEMP_TABLE_GAPPED_ZEROES.NET_CODE 
AND TEMP_NTID.PR_CODE = TEMP_TABLE_GAPPED_ZEROES.PR_CODE 
AND TEMP_NTID.ST_HR = TEMP_TABLE_GAPPED_ZEROES.ST_HR 
AND TEMP_NTID.ST_MIN = TEMP_TABLE_GAPPED_ZEROES.ST_MIN 
AND TEMP_NTID.DURATION = TEMP_TABLE_GAPPED_ZEROES.DURATION 
AND TEMP_NTID.REC_FLAG = TEMP_TABLE_GAPPED_ZEROES.REC_FLAG 
AND TEMP_NTID.st_date = TEMP_TABLE_GAPPED_ZEROES.st_date 
;