|
[code=sql]USE tempdb
GO
IF OBJECT_ID('TEST') IS NOT NULL
DROP TABLE TEST;
GO
CREATE TABLE TEST
(
months DATE,
customer NVARCHAR(8),
sales INT
);
GO
INSERT INTO TEST VALUES('2013-7-1','ABC',1180);
INSERT INTO TEST VALUES('2013-8-1','ABC',0);
INSERT INTO TEST VALUES('2013-9-1','ABC',1180);
INSERT INTO TEST VALUES('2013-10-1','ABC',0);
INSERT INTO TEST VALUES('2013-11-1','ABC',1180);
INSERT INTO TEST VALUES('2013-12-1','ABC',0);
GO
----------------------------------------
SELECT A.*,CASE WHEN A.sales+B.sales+C.sales>=1180*2 THEN 1 ELSE 0 END AS type
FROM TEST AS A
LEFT JOIN TEST AS B
ON MONTH(A.months)=MONTH(B.months)-1
LEFT JOIN TEST AS C
ON MONTH(A.months)=MONTH(C.months)-2
----------------------------------------
/*
months customer sales type
---------- -------- ----------- -----------
2013-07-01 ABC 1180 1
2013-08-01 ABC 0 0
2013-09-01 ABC 1180 1
2013-10-01 ABC 0 0
2013-11-01 ABC 1180 0
2013-12-01 ABC 0 0
(6 行受影响)
*/
----------------------------------------
--请换成access支持的语法,例如:case when 换成 iif[/code] |
|