高分请教,sql语句的写法,在线等,急!

superxiumu 2004-06-23 11:41:55
小弟打算写这样一个sql语句:
表A描述如小:
ID   SBRQ
001  2003-01
002  2003-01
003  2003-01
001  2003-02
002  2003-02
001  2004-01
002  2004-01

.............


字段id:是记录企业的标志号 
字段SBRQ:记录企业申报数据的时间
其中id字段记录很多家企业

如果我想得到:
2003年1月到2003年3月 和 2004年1月到2004年3月
每个月都申报过数据的企业的id
这条sql语句怎么写?谢谢!!
...全文
194 14 打赏 收藏 转发到动态 举报
写回复
用AI写文章
14 条回复
切换为时间正序
请发表友善的回复…
发表回复
zjcxc 元老 2004-06-23
  • 打赏
  • 举报
回复
--如果就是日期这个限制,可以这样写存储过程
create proc p_qry
@dt datetime --要查询的日期
as
select a.id from(
select id from a
where sbrq
between convert(char(5),dateadd(year,-1,@dt),120)+'-01'
and convert(char(7),dateadd(year,-1,@dt),120)
group by id
having count(distinct sbrq)=month(@dt)
)a join(
select id from a
where sbrq
between convert(char(5),@dt,120)+'-01'
and convert(char(7),@dt,120)
group by id
having count(distinct sbrq)=month(@dt)
)b on a.id=b.id
superxiumu 2004-06-23
  • 打赏
  • 举报
回复
申报日期一般不会有重复!
但不一定!
superxiumu 2004-06-23
  • 打赏
  • 举报
回复
没有了!
zjcxc 元老 2004-06-23
  • 打赏
  • 举报
回复
--就今年和上年的差别而已,没有其他条件限制了?
superxiumu 2004-06-23
  • 打赏
  • 举报
回复
我的日期区间是不定的
例如程序传来日期,我的区间要求是今年一月份到该日期月份,上年一月份到上年该月!
zjcxc 元老 2004-06-23
  • 打赏
  • 举报
回复
--如果会重复,可以这样写:

select a.id from(
select id from a
where sbrq bettween '2003-1' and '2003-3'
group by id
having count(distinct sbrq)=3
)a join(
select id from a
where sbrq bettween '2004-1' and '2004-3'
group by id
having count(distinct sbrq)=3
)b on a.id=b.id
pjy 2004-06-23
  • 打赏
  • 举报
回复
楼上好快哟!
pjy 2004-06-23
  • 打赏
  • 举报
回复
因为你可能有几个时间间隔,建议你先把相应的间隔期间的SBRQ写到一个##SBRQ中,语句我就不写了!
测试语句:
create table a (id varchar(3),sbrq varchar(7))
insert into a
select '001','2003-01'
union
select '002','2003-01'
union
select '001','2003-02'
create table ##SBRQ (sbrq varchar(7))
insert ##SBRQ
select '2003-01'
union
select '2003-02'
执行语句:
select ID from
(
select ID,count(ID) as IDNum,(Select Count(*) from ##sbrq) as rqNum from
(select ID from a,##sbrq b where a.sbrq = b.sbrq) c
group by ID
) c
where c.IDNum = rqNum
返回结果:
001
jackjingsg 2004-06-23
  • 打赏
  • 举报
回复
select id from table where count(*)=6 group by id having sbrq in ('2003-01','2003-02','2003-03','2004-01','2004-02','2004-03') order by id;
zjcxc 元老 2004-06-23
  • 打赏
  • 举报
回复
--每个月的申报会不会重复? 如果不会重复,可以这样写:

select a.id from(
select id from a
where sbrq bettween '2003-1' and '2003-3'
group by id
having count(*)=3
)a join(
select id from a
where sbrq bettween '2004-1' and '2004-3'
group by id
having count(*)=3
)b on a.id=b.id
pbsql 2004-06-23
  • 打赏
  • 举报
回复
select a1.id
from (select id from a where sbrq='2003-01') a1,
(select id from a where sbrq='2003-02') a2,
(select id from a where sbrq='2003-03') a3,
(select id from a where sbrq='2004-01') a4,
(select id from a where sbrq='2004-02') a5,
(select id from a where sbrq='2004-03') a6
where a1.id=a2.id and a1.id=a3.id
and a1.id=a4.id and a1.id=a5.id and a1.id=a6.id
seekg 2004-06-23
  • 打赏
  • 举报
回复
kankan
沃尔特容易 2004-06-23
  • 打赏
  • 举报
回复
如果你的表名字为atable,则
SELECT DISTINCT ID FROM atable WHERE (CAST(SBRQ+'-15' AS DATETIME) > '2003-1-1' AND CAST(SBRQ+'-15' AS DATETIME) < '2003-3-1') OR (CAST(SBRQ+'-15' AS DATETIME) > '2004-1-1' AND CAST(SBRQ+'-15' AS DATETIME) < '2004-3-1')即可.
pjy 2004-06-23
  • 打赏
  • 举报
回复
我写的应该可以呀,就是自己需要加一点程序!

34,575

社区成员

发帖
与我相关
我的任务
社区描述
MS-SQL Server相关内容讨论专区
社区管理员
  • 基础类社区
  • 二月十六
  • 卖水果的net
加入社区
  • 近7日
  • 近30日
  • 至今
社区公告
暂无公告

试试用AI创作助手写篇文章吧