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

帮小弟我看看这条sql语句吧,就这么点分了

2012-01-24 
帮我看看这条sql语句吧,就这么点分了我是在改别人写的sql语句,这个语句主要问题是在查询时间的时候出现问

帮我看看这条sql语句吧,就这么点分了
我是在改别人写的sql语句,这个语句主要问题是在查询时间的时候出现问题的,应该输出是全部是2007年以后的,可是为什么输出记录里还有2007年以前的,帮我看看,应该怎么改啊,
语句有点长,我搞不定了,看不懂了,希望给指点以下谢谢了,
select   top   10   *from  
(   select   distinct   tbHatchUnit.Id   as   id,tbUnit.theyear   ,tbUnit.Name,   tbZGCLocation.name   as   ZGCLocation,   tbInvestorType.name   as   investorType,   tbEconomyType.name   as   economyType,   tbHatchUnit.Type,   tbUnit.RegFund  
from   tbUnit  
join   tbHatchUnit   on   tbUnit.Id=tbHatchUnit.RefUnitId  
left   outer   join   tbIncomeInfo   on   tbIncomeInfo.RefUnitId   =tbUnit.Id  
left   outer   join   tbZGCLocation   on   tbHatchUnit.RefZGCLocationId=tbZGCLocation.Id  
left   outer   join   tbInvestorType   on   tbHatchUnit.RefInvestorTypeId=tbInvestorType.Id  
left   outer   join   tbEconomyType   on   tbUnit.RefEconTypeId=tbEconomyType.Id  
left   outer   join   tbHatchLocation   on   tbHatchLocation.RefHatchId=tbHatchUnit.Id  
where   (tbHatchUnit.Id   NOT   IN   (SELECT   tbSciencePark.refHatchId   FROM   tbSciencePark   UNION   (SELECT   tbOverseasPark.refHatchId   FROM   tbOverseasPark)))  
and   tbUnit.theYear> = '2007-1-1 '   and   tbUnit.theYear <= '2007-12-31 '  
and   tbUnit.name   like   '%北京交大科技孵化器有限公司% '  
or   tbUnit.name   like   '%北京宏翔鸿科技孵化基地有限公司% '  
group   by   tbUnit.theyear,   tbHatchUnit.Id,   tbUnit.Name,   tbZGCLocation.name,   tbInvestorType.name,tbEconomyType.name,   tbHatchUnit.Type,   tbUnit.RegFund   having   1=1
)t1
where   id   not   in  
(select   top   0   id   from  
(select   distinct   tbHatchUnit.Id   as   id,   tbUnit.Name,   tbZGCLocation.name   as   ZGCLocation,   tbInvestorType.name   as   investorType,   tbEconomyType.name   as   economyType,   tbHatchUnit.Type,   tbUnit.RegFund
from   tbUnit   join   tbHatchUnit   on   tbUnit.Id=tbHatchUnit.RefUnitId  
left   outer   join   tbIncomeInfo   on   tbIncomeInfo.RefUnitId   =tbUnit.Id  
left   outer   join   tbZGCLocation   on   tbHatchUnit.RefZGCLocationId=tbZGCLocation.Id  
left   outer   join   tbInvestorType   on   tbHatchUnit.RefInvestorTypeId=tbInvestorType.Id  
left   outer   join   tbEconomyType   on   tbUnit.RefEconTypeId=tbEconomyType.Id  
left   outer   join   tbHatchLocation   on   tbHatchLocation.RefHatchId=tbHatchUnit.Id  
where   (tbHatchUnit.Id   NOT   IN   (SELECT   tbSciencePark.refHatchId   FROM   tbSciencePark   UNION   (SELECT   tbOverseasPark.refHatchId   FROM   tbOverseasPark)))  
and   tbUnit.theYear> = '2007-1-1 '   and   tbUnit.theYear <= '2007-12-31 '  
and   tbUnit.name   like   '%北京交大科技孵化器有限公司% '  
or   tbUnit.name   like   '%北京宏翔鸿科技孵化基地有限公司% '  
group   by   tbHatchUnit.Id,   tbUnit.Name,   tbZGCLocation.name,   tbInvestorType.name,tbEconomyType.name,   tbHatchUnit.Type,   tbUnit.RegFund   having   1=1
)t1  

order   by   id)
order   by   id


这是部分字段:(不是我想要的)

604
2007-06-08   11:09:36.233   北京宏翔鸿科技孵化基地有限公司   不属于   616
2007-06-08   16:11:34.467   北京交大科技孵化器有限公司   中关村科技园   654
2006-06-08   11:09:36.000   北京宏翔鸿科技孵化基地有限公司   不属于  
这是我想要的结果啊
604
2007-06-08   11:09:36.233   北京宏翔鸿科技孵化基地有限公司   不属于   616
2007-06-08   16:11:34.467   北京交大科技孵化器有限公司   中关村科技园   654

[解决办法]
试试看.........


select top 10 *from
( select distinct tbHatchUnit.Id as id,tbUnit.theyear ,tbUnit.Name, tbZGCLocation.name as ZGCLocation, tbInvestorType.name as investorType, tbEconomyType.name as economyType, tbHatchUnit.Type, tbUnit.RegFund
from tbUnit
join tbHatchUnit on tbUnit.Id=tbHatchUnit.RefUnitId
left outer join tbIncomeInfo on tbIncomeInfo.RefUnitId =tbUnit.Id
left outer join tbZGCLocation on tbHatchUnit.RefZGCLocationId=tbZGCLocation.Id
left outer join tbInvestorType on tbHatchUnit.RefInvestorTypeId=tbInvestorType.Id
left outer join tbEconomyType on tbUnit.RefEconTypeId=tbEconomyType.Id
left outer join tbHatchLocation on tbHatchLocation.RefHatchId=tbHatchUnit.Id
where (tbHatchUnit.Id NOT IN (SELECT tbSciencePark.refHatchId FROM tbSciencePark UNION (SELECT tbOverseasPark.refHatchId FROM tbOverseasPark)))
and tbUnit.theYear> = '2007-1-1 ' and tbUnit.theYear <= '2007-12-31 '
and (tbUnit.name like '%北京交大科技孵化器有限公司% '
or tbUnit.name like '%北京宏翔鸿科技孵化基地有限公司% ')
group by tbUnit.theyear, tbHatchUnit.Id, tbUnit.Name, tbZGCLocation.name, tbInvestorType.name,tbEconomyType.name, tbHatchUnit.Type, tbUnit.RegFund having 1=1
)t1
where id not in
(select top 0 id from
(select distinct tbHatchUnit.Id as id, tbUnit.Name, tbZGCLocation.name as ZGCLocation, tbInvestorType.name as investorType, tbEconomyType.name as economyType, tbHatchUnit.Type, tbUnit.RegFund
from tbUnit join tbHatchUnit on tbUnit.Id=tbHatchUnit.RefUnitId
left outer join tbIncomeInfo on tbIncomeInfo.RefUnitId =tbUnit.Id
left outer join tbZGCLocation on tbHatchUnit.RefZGCLocationId=tbZGCLocation.Id
left outer join tbInvestorType on tbHatchUnit.RefInvestorTypeId=tbInvestorType.Id
left outer join tbEconomyType on tbUnit.RefEconTypeId=tbEconomyType.Id
left outer join tbHatchLocation on tbHatchLocation.RefHatchId=tbHatchUnit.Id
where (tbHatchUnit.Id NOT IN (SELECT tbSciencePark.refHatchId FROM tbSciencePark UNION (SELECT tbOverseasPark.refHatchId FROM tbOverseasPark)))
and tbUnit.theYear> = '2007-1-1 ' and tbUnit.theYear <= '2007-12-31 '
and (tbUnit.name like '%北京交大科技孵化器有限公司% '
or tbUnit.name like '%北京宏翔鸿科技孵化基地有限公司% ')
group by tbHatchUnit.Id, tbUnit.Name, tbZGCLocation.name, tbInvestorType.name,tbEconomyType.name, tbHatchUnit.Type, tbUnit.RegFund having 1=1
)t1

order by id)
order by id
[解决办法]
and (tbUnit.theYear between '2007-1-1 ' and '2007-12-31 ')
改为

热点排行