sqlserver里做环比,使用LAG函数,求出来的环比上期金额不对。 代码如下 SELECT D.日期, SUM(D.金额)AS 金额,C.SUPPLIER_NAME AS 供应商, LAG(SUM(D.金额),1) OVER (PARTITION BY C.SUPPLIER_NAME ORDER BY D.[日期] ) AS 上期金额 FROM ( SELECT CONVERT(VARCHAR(7),A.DOC_DATE,23) as 日期,SUM(B.AMT_FC) AS 金额,SETTLEMENT_SUPPLIER_ID FROM PAYABLE_DOC A LEFT JOIN PAYABLE_DOC_D B ON A.PAYABLE_DOC_ID=B.PAYABLE_DOC_ID where A.DOC_DATE>=DATEADD(year,-2,GETDATE()) GROUP BY A.DOC_DATE,DOC_DATE,SETTLEMENT_SUPPLIER_ID) AS D left join SUPPLIER C ON D.SETTLEMENT_SUPPLIER_ID=C.SUPPLIER_BUSINESS_ID WHERE C.SUPPLIER_NAME like '%肯纳司太%' GROUP BY D.日期,C.SUPPLIER_NAME |