|
|
发表于 7-12-2010 12:00 PM
|
显示全部楼层
本帖最后由 lebellebebe 于 7-12-2010 12:17 PM 编辑
- create table sales
- (xinvoiceno int
- ,xuser_id varchar(20)
- ,xyear int
- ,xmonth int
- ,xRM int
- ,constraint pk_sales primary key clustered(xinvoiceno)
- )
复制代码
- insert sales
- (xinvoiceno,xuser_id,xyear,xmonth,xRM)
- values
- (1001, 'A21012', 2009, 5, 100)
- ,(1002, 'A10099', 2009, 6, 120)
- ,(1003, 'A34300', 2010, 5, 300)
- ,(1004, 'A30002', 2010, 10, 400)
- ,(1005, 'A33302', 2007, 10, 400) -- final result return this &
- ,(1006, 'B8865J', 2008, 12, 400) -- this
- ,(1007, 'B8865J', 2010, 7, 1000)
- ,(1008, 'A34300', 2006, 9, 1500)
复制代码
SQL query (check syntax, not entirely correct)
- select *
- from sales s
- where (s.xmonth <= datepart(mm, (dateadd(month, -24, getdate()))) and
- s.xyear <= datepart(yyyy, (dateadd(year, -2, getdate())))) and
- not exists
- (select *
- from sales s2
- where s2.xuser_id = s.xuser_id and
- (s2.xmonth <= datepart(mm, (dateadd(month, -6, getdate())))) and
- (s2.xyear = datepart(yyyy, getdate())))
复制代码
|
|