大虾们,救救我吧!!!!!!!1

gyydreams 2003-11-03 04:54:43
求救
我有两张表 t1,t2
t1的字段
c_jhno(计划编号) , dt_start(开始时间),dt_end(结束时间)

t2的字段

c_jhno(计划编号) c_dl(段路编号) ,c_bs(检查对象), i_sign (是否检查(1表示已查0表示未查))

现在要用一条语句写出如下格式的报表

c_dl(段路编号) , i_yjnum(应该检查的数量) ,i_sjnum(实际检查数量)

要有时间范围的查询条件,即:dt_start >= pdt_start and dt_start <= pdt_end
...全文
20 4 打赏 收藏 转发到动态 举报
写回复
用AI写文章
4 条回复
切换为时间正序
请发表友善的回复…
发表回复
zjcxc 元老 2003-11-03
  • 打赏
  • 举报
回复
上面的写错了字段:
select c_d1,i_yjnum=sum(1),i_sjnum=sum(cast(i_sign as int))
from t2 where c_jhno in(select c_jhno from t1 where dt_start >= pdt_start and dt_start <= pdt_end)


或:
select a.c_d1,i_yjnum=sum(1),i_sjnum=sum(cast(a.i_sign as int))
from t2 a inner join t1 b on a.c_jhno=b.c_jhno
where a.dt_start >= pdt_start and a.dt_start <= pdt_end)
zjcxc 元老 2003-11-03
  • 打赏
  • 举报
回复

select c_d1,i_yjnum=sum(1),i_sjnum=sum(cast(i_sign as int))
from t2 where c_jhno in(select jhno from t1 where dt_start >= pdt_start and dt_start <= pdt_end)
txlicenhe 2003-11-03
  • 打赏
  • 举报
回复
这样好象也可以:
Select c_d1,sum(1) as 应该检查的数量,sum(i_sign) as 实际检查数量
from t2
join t1 on t2.c_jhno = t1.c_jhno
where t1.dt_start >= pdt_start and t1.dt_start <= pdt_end
group by c_d1
txlicenhe 2003-11-03
  • 打赏
  • 举报
回复
Select c_d1,sum(1) as 应该检查的数量,sum(case when i_sign = 1 then 1 else 0 end) as 实际检查数量
from t2
join t1 on t2.c_jhno = t1.c_jhno
where t1.dt_start >= pdt_start and t1.dt_start <= pdt_end
group by c_d1

34,590

社区成员

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

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