将UPDATE语句转换为SELECT语句

时间:2015-11-05 09:21:43

标签: netezza

示例代码:

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 个答案:

答案 0 :(得分: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 
;