年月日 省份代码 城市代码 客户代码 购进量
20100101 1 11 111 0
20100101 1 11 111 0
20100101 1 11 112 1
20100201 1 11 111 0
20100201 1 11 111 0
20100201 1 11 112 1
20100301 1 11 111 2
20100301 1 11 111 0
20100301 1 11 112 1
20100401 1 11 111 0
20100401 1 11 111 0
20100401 1 11 112 1
20100501 1 11 111 0
20100501 1 11 111 0
20100501 1 11 112 1
20100601 1 11 111 0
20100601 1 11 111 0
20100601 1 11 112 1
20100701 1 11 111 0
20100701 1 11 111 0
20100701 1 11 112 1
20100801 1 11 111 0
20100801 1 11 111 0
20100801 1 11 112 1
20100901 1 11 111 0
20100901 1 11 111 0
20100901 1 11 112 1
20101001 1 11 111 0
20101001 1 11 111 0
20101001 1 11 112 1
20101101 1 11 111 0
20101101 1 11 111 0
20101101 1 11 112 1
20101201 1 11 111 0
20101201 1 11 111 0
20101201 1 11 112 1
------解决方案--------------------
- SQL code
--> 测试数据: #tbif object_id('tempdb.dbo.#tb') is not null drop table #tbcreate table #tb (年月日 datetime,省份代码 int,城市代码 int,客户代码 int,购进量 int)insert into #tbselect '20100101',1,11,111,0 union allselect '20100101',1,11,111,0 union allselect '20100101',1,11,112,1 union allselect '20100201',1,11,111,0 union allselect '20100201',1,11,111,0 union allselect '20100201',1,11,112,1 union allselect '20100301',1,11,111,2 union allselect '20100301',1,11,111,0 union allselect '20100301',1,11,112,1 union allselect '20100401',1,11,111,0 union allselect '20100401',1,11,111,0 union allselect '20100401',1,11,112,1 union allselect '20100501',1,11,111,0 union allselect '20100501',1,11,111,0 union allselect '20100501',1,11,112,1 union allselect '20100601',1,11,111,0 union allselect '20100601',1,11,111,0 union allselect '20100601',1,11,112,1 union allselect '20100701',1,11,111,0 union allselect '20100701',1,11,111,0 union allselect '20100701',1,11,112,1 union allselect '20100801',1,11,111,0 union allselect '20100801',1,11,111,0 union allselect '20100801',1,11,112,1 union allselect '20100901',1,11,111,0 union allselect '20100901',1,11,111,0 union allselect '20100901',1,11,112,1 union allselect '20101001',1,11,111,0 union allselect '20101001',1,11,111,0 union allselect '20101001',1,11,112,1 union allselect '20101101',1,11,111,0 union allselect '20101101',1,11,111,0 union allselect '20101101',1,11,112,1 union allselect '20101201',1,11,111,0 union allselect '20101201',1,11,111,0 union allselect '20101201',1,11,112,1select count(distinct 客户代码 ) as Nfrom #tb twhere month(年月日)>2 and 购进量>0 and not exists(select 1 from #tb where 客户代码=t.客户代码 and month(年月日)<=2 and 购进量>0)N-----------1(1 行受影响)
------解决方案--------------------
- SQL code
create table tb (年月日 datetime,省份代码 int,城市代码 int,客户代码 int,购进量 int)insert into tbselect '20100101',1,11,111,0 union allselect '20100101',1,11,111,0 union allselect '20100101',1,11,112,1 union allselect '20100201',1,11,111,0 union allselect '20100201',1,11,111,0 union allselect '20100201',1,11,112,1 union allselect '20100301',1,11,111,2 union allselect '20100301',1,11,111,0 union allselect '20100301',1,11,112,1 union allselect '20100401',1,11,111,0 union allselect '20100401',1,11,111,0 union allselect '20100401',1,11,112,1 union allselect '20100501',1,11,111,0 union allselect '20100501',1,11,111,0 union allselect '20100501',1,11,112,1 union allselect '20100601',1,11,111,0 union allselect '20100601',1,11,111,0 union allselect '20100601',1,11,112,1 union allselect '20100701',1,11,111,0 union allselect '20100701',1,11,111,0 union allselect '20100701',1,11,112,1 union allselect '20100801',1,11,111,0 union allselect '20100801',1,11,111,0 union allselect '20100801',1,11,112,1 union allselect '20100901',1,11,111,0 union allselect '20100901',1,11,111,0 union allselect '20100901',1,11,112,1 union allselect '20101001',1,11,111,0 union allselect '20101001',1,11,111,0 union allselect '20101001',1,11,112,1 union allselect '20101101',1,11,111,0 union allselect '20101101',1,11,111,0 union allselect '20101101',1,11,112,1 union allselect '20101201',1,11,111,0 union allselect '20101201',1,11,111,0 union allselect '20101201',1,11,112,1select * from tb t where MONTH(年月日) between 3 and 12 and 购进量>0and not exists(select 1 from tb where 客户代码=t.客户代码 and MONTH(年月日)<=2 and 购进量>0)/*年月日 省份代码 城市代码 客户代码 购进量2010-03-01 00:00:00.000 1 11 111 2*/