首页 诗词 字典 板报 句子 名言 友答 励志 学校 网站地图
当前位置: 首页 > 教程频道 > 数据库 > SQL Server >

请问句sql,数据格式(年月日,省份代码,城市代码,客户代码,购进量),求3-12月任一月份购进大于0但1、2月无购进且省份代码为1的客户数,内附测试数据,多谢

2012-03-16 
请教句sql,数据格式(年月日,省份代码,城市代码,客户代码,购进量),求3-12月任一月份购进大于0但1、2月无购进

请教句sql,数据格式(年月日,省份代码,城市代码,客户代码,购进量),求3-12月任一月份购进大于0但1、2月无购进且省份代码为1的客户数,内附测试数据,谢谢
年月日省份代码城市代码客户代码购进量
201001011111110
201001011111110
201001011111121
201002011111110
201002011111110
201002011111121
201003011111112
201003011111110
201003011111121
201004011111110
201004011111110
201004011111121
201005011111110
201005011111110
201005011111121
201006011111110
201006011111110
201006011111121
201007011111110
201007011111110
201007011111121
201008011111110
201008011111110
201008011111121
201009011111110
201009011111110
201009011111121
201010011111110
201010011111110
201010011111121
201011011111110
201011011111110
201011011111121
201012011111110
201012011111110
201012011111121
201001012212110
201001012212110
201001012212121
201002012212110
201002012212110
201002012212121
201003012212112
201003012212110
201003012212121
201004012212110
201004012212110
201004012212121
201005012212110
201005012212110
201005012212121
201006012212110
201006012212110
201006012212121
201007012212110
201007012212110
201007012212121
201008012212110
201008012212110
201008012212121
201009012212110
201009012212110
201009012212121
201010012212110
201010012212110
201010012212121
201011012212110
201011012212110
201011012212121
201012012212110
201012012212110
201012012212121




想要得到的结果:
格式:(省份代码,城市代码,客户数)
结果:(1,11,1)

[解决办法]

SQL code
if object_id('tempdb.dbo.#tb') is not null drop table #tbgocreate 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 城市代码=t.城市代码 and 客户代码=t.客户代码 and month(年月日)<=2 and 购进量>0)group by 省份代码,城市代码省份代码        城市代码        N----------- ----------- -----------1           11          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*/ 

热点排行
Bad Request.