2017-05-15 62 views
0
INSERT INTO CLEANUP.DBO.TMP_CLAIM_PBI33562_2 (NCLAIMID, SATTYFILENO, DTLASTPMT, ACTUALPMTDATE, SWORKSTATIONID, DTCHANGEDON, NCHANGEDBY, NAPPLICATIONID) 
; WITH LST_CTE AS 
(
    SELECT 
     NCLAIMID, MAX(DTTRANSACTION) AS ACTUALPMTDATE 
    FROM 
     CLAIMREGISTRY 
    WHERE 
     NTRANLINKID IS NULL 
     AND NTRANSACTIONCODEID IN (1,13) 
    GROUP BY 
     NCLAIMID 
) 

SELECT 
    C.NCLAIMID, C.SATTYFILENO, C.DTLASTPMT, 
    L.ACTUALPMTDATE, C.SWORKSTATIONID, C.DTCHANGEDON, 
    C.NCHANGEDBY, C.NAPPLICATIONID 
FROM 
    CLAIM AS C 
JOIN 
    LST_CTE AS L ON L.NCLAIMID = C.NCLAIMID 
WHERE 
    CONVERT(VARCHAR, C.DTLASTPMT, 112) <> CONVERT(VARCHAR, L.ACTUALPMTDATE, 112) 
+0

你什麼錯誤? – litelite

+0

把你嵌在SELECT語句前,但CTE聲明 – Adam

回答

1

嘗試聲明你cte第一:

;with LST_CTE as (
    select NCLAIMID 
    , MAX(DTTRANSACTION) as ACTUALPMTDATE 
    from CLAIMREGISTRY 
    where NTRANLINKID is null and NTRANSACTIONCODEID in (1, 13) 
    group by NCLAIMID 
) 
insert into CLEANUP.DBO.TMP_CLAIM_PBI33562_2 (
NCLAIMID 
, SATTYFILENO 
, DTLASTPMT 
, ACTUALPMTDATE 
, SWORKSTATIONID 
, DTCHANGEDON 
, NCHANGEDBY 
, NAPPLICATIONID 
) 
select C.NCLAIMID 
, C.SATTYFILENO 
, C.DTLASTPMT 
, L.ACTUALPMTDATE 
, c.SWORKSTATIONID 
, c.DTCHANGEDON 
, c.NCHANGEDBY 
, c.NAPPLICATIONID 
from CLAIM as C 
    inner join LST_CTE as L 
    on L.NCLAIMID = C.NCLAIMID 
where CONVERT(varchar(30), C.DTLASTPMT, 112) <> CONVERT(varchar(30), L.ACTUALPMTDATE, 112) 

而且,如果DTLASTPMTACTUALPMTDATEdatedatetime數據類型,你不必convert()他們比較它們。

+0

感謝戈登後,它的工作原理。 –

2

with那張insert之前。 。 。並且在一份聲明中中間沒有分號:

WITH LST_CTE AS (
    SELECT NCLAIMID,MAX(DTTRANSACTION) AS ACTUALPMTDATE FROM CLAIMREGISTRY 
    WHERE NTRANLINKID IS NULL 
    AND NTRANSACTIONCODEID IN (1,13) 
    GROUP BY NCLAIMID 
) 
insert into CLEANUP.DBO.TMP_CLAIM_PBI33562_2 (NCLAIMID, SATTYFILENO,DTLASTPMT,ACTUALPMTDATE,SWORKSTATIONID,DTCHANGEDON,NCHANGEDBY,NAPPLICATIONID) 
    SELECT C.NCLAIMID, C.SATTYFILENO,C.DTLASTPMT,L.ACTUALPMTDATE,c.SWORKSTATIONID,c.DTCHANGEDON,c.NCHANGEDBY,c.NAPPLICATIONID 
    FROM CLAIM C JOIN 
     LST_CTE L 
     ON L.NCLAIMID = C.NCLAIMID 
    WHERE CONVERT(VARCHAR(255), C.DTLASTPMT, 112) <> CONVERT(VARCHAR(255), L.ACTUALPMTDATE, 112); 

我也不喜歡WHERE條款。如果你想比較公正的日期,然後比較日期:

WHERE CAST(C.DTLASTPMT as DATE) = CAST(L.ACTUALPMTDATE as DATE) 

爲什麼轉換爲字符串來比較日期?