再问分段数值范围查询,请帮忙!!!

linyany 2006-03-09 11:02:40
我用这个查询可以查出结果
SELECT iif(时长秒<=3,"0:3",iif((时长秒>3 and 时长秒<=6),"3:6",iif((时长秒>6 and 时长秒<=15),"6:15",iif((时长秒>15 and 时长秒<=30),"15:30",iif((时长秒>30),">30"))))) AS range,count(1) as count
FROM biao1 group by iif(时长秒<=3,"0:3",iif((时长秒>3 and 时长秒<=6),"3:6",iif((时长秒>6 and 时长秒<=15),"6:15",iif((时长秒>15 and 时长秒<=30),"15:30",iif((时长秒>30),">30")))))

我的表是这样的:

日期   时长秒
1     2
1     3
1     5
2     8
2     1
3     9
3     6
需要的结果是,每天,时长在"0:3","3:6","6:15","15:30",">30"的个数.
我按以上查询,可以查出不同范围的个数,但问题有两个:

1,查出来,我想要的结果是行,我想要字段,列的
  如,现在查出来
    range count
  "0:3"  3
" 3:6" 2
......
需要的结果

   "0:3"   "3:6" 

     3       2 .......

2,我怎么将日期分离出来?现在这样的结果是所有日期的和
  需要的结果

日期   "0:3"  "3:6"
1      2     1
2      1     0..........
...全文
83 8 打赏 收藏 转发到动态 举报
写回复
用AI写文章
8 条回复
切换为时间正序
请发表友善的回复…
发表回复
linyany 2006-03-10
  • 打赏
  • 举报
回复
select count(小于3),count(大于3小于6) from(
select 次数 as 小于3,0 as 大于3小于6 from biao1 where 时长秒<=3 UNION ALL
select 0 as 小于3, 次数 as 大于3小于6 from biao1 where 时长秒>3 and 时长秒<=6 )

怎么说我语法错误?
changechange 2006-03-09
  • 打赏
  • 举报
回复
如果你还要加上日期,应该用


select sum(小于3),sum(大于3小于6),sum(大于6小于15),sum(大于15小于30),sum( 大于30 ) ,日期 from (

select 日期,呼叫 as 小于3,0 as 大于3小于6,0 as 大于6小于15,0 as 大于15小于30,0 as 大于30 from 表 where 时长<3

union all

select 日期,0 小于3,呼叫 as 大于3小于6,0 as 大于6小于15,0 as 大于15小于30,0 as 大于30 from 表 where 时长>3 and 时长<6

union all ....) group by 日期

上述语句非常简单,一共只有两个套嵌而已
changechange 2006-03-09
  • 打赏
  • 举报
回复
select sum(小于3),sum(大于3小于6),sum(大于6小于15),sum(大于15小于30),sum( 大于30 ) from (

select 呼叫 as 小于3,0 as 大于3小于6,0 as 大于6小于15,0 as 大于15小于30,0 as 大于30 from 表 where 时长<3

union all

select 0 小于3,呼叫 as 大于3小于6,0 as 大于6小于15,0 as 大于15小于30,0 as 大于30 from 表 where 时长>3 and 时长<6

union all ....)
changechange 2006-03-09
  • 打赏
  • 举报
回复
如果你要横过来,就按我第一次告诉你的去做!
changechange 2006-03-09
  • 打赏
  • 举报
回复
select "0:3" as range,count(时长秒) from 表 where 时长<3 UNION ALL
select "3:6" as range,count(时长秒) from 表 where 时长>3 and 时长<6 UNION ALL ....

这样就完全实现你的要求

changechange 2006-03-09
  • 打赏
  • 举报
回复
在回答你上一个问题时我已经明确说明了,应该使用最简单的 UNION ALL,而且你现在的要求也应该使用 UNION ALL ,而不是IIF 去完成

非常简单,你要做的只是写不同的WHERE 子句,然后将他们组合起来而已。用 ASNI SQL 就可以做到。



select 呼叫 as 小于3,0 as 大于3小于6,0 as 大于6小于15,0 as 大于15小于30,0 as 大于30 from where 时长<3

union all

select 0 小于3,呼叫 as 大于3小于6,0 as 大于6小于15,0 as 大于15小于30,0 as 大于30 from where 时长>3 and 时长<6

union all ....


一直持续下去

如果你要汇总只要写一个 sum语句即可


select sum(小于3),sum(大于3小于6),sum(大于6小于15),sum(大于15小于30),sum( 大于30 ) from (

select 呼叫 as 小于3,0 as 大于3小于6,0 as 大于6小于15,0 as 大于15小于30,0 as 大于30 from where 时长<3

union all

select 0 小于3,呼叫 as 大于3小于6,0 as 大于6小于15,0 as 大于15小于30,0 as 大于30 from where 时长>3 and 时长<6

union all ....)


太复杂了吧,有没有简单点的,用IIF什么的?
--------请注意!用 IIF 才是最复杂的,因为它涉及到 JET DB / ACCESS JET SQL /VBA 三方面!

而用 UNION ALL 只涉及到 JET DB ,该语句通用性非常强
linyany 2006-03-09
  • 打赏
  • 举报
回复
union all的时候,为什么where 后面是<3,但还要select 0,3,6,15什么的呢?
wwwwb 2006-03-09
  • 打赏
  • 举报
回复
用数个语句UNION即可

7,714

社区成员

发帖
与我相关
我的任务
社区描述
Microsoft Office Access是由微软发布的关系数据库管理系统。它结合了 MicrosoftJet Database Engine 和 图形用户界面两项特点。
社区管理员
  • Access
加入社区
  • 近7日
  • 近30日
  • 至今
社区公告
暂无公告

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