新手高分求助,大数据复杂查询
A表:
a_id 标题 最后更新时间 用户ID
1 我是标题1 2013-01-30 18:14:00 5277
2 我是标题2 2013-02-04 20:22:12 5277
3 我是标题3 5277
B表:
b_id aid 提交时间
1 1 2013-01-01 08:14:15
2 1 2013-01-06 12:00:09
3 1 2013-01-30 18:14:00
4 2 2013-02-01 23:11:19
5 2 2013-02-04 20:22:12
B表中的aid是A表的主键ID
查询条件是根据【用户ID】和【时间段如[2013-01-01 00:00:00]到[2013-02-06 23:59:59]】
如查用户5277 在时间段[2013-01-01 00:00:00]到[2013-02-04 20:22:12]的数据
需要得到的数据如下:
用户 记录数1 记录数2 记录数3 0点到12点 12点23点
5277 3 2 5 1 4
记录数1:按照条件查询count A表
记录数2:按照条件查询count A表中【最后更新时间】不为空的
记录数3:根据条件查询A表中【最后更新时间】不为空,关联到B表的所有符合时间段的数据
因为A,B的数据都很庞大,特别是B表,差不多有60多W左右,
哪位高手有效率比较高的查询语句提供么?
求助求助,高分感谢!!!!
复杂,大数据,子查询
[解决办法]
select 用户,(select count(1) from A where 用户ID='5277' and 最后更新时间<>'') 记录数1,
count(distinct a.a_id)记录数2,
count(1) 记录数3,
sum(case when convert(varchar,b.提交时间,23)>='00:00:00'
and convert(varchar,b.提交时间,23)<'12:00:00' then 1 else 0 end) [0点到12点],
sum(case when convert(varchar,b.提交时间,23)>='12:00:00'
and convert(varchar,b.提交时间,23)<='23:59:59' then 1 else 0 end) [12点23点]
from A,B where a.a_id=b.a_id and a.最后更新时间<>''
select
(select count(1) from tba where userid=5277 ) [记录数1],
(select count(1) from tba where userid=5277 and isnull(lasttime,'')!='') [记录数2],
count(1)[记录3],
sum(case when convert(varchar(20),b.tjtime,114) between '00:00:00' and '12:00:00' then 1 else 0 end) [0点到12点],
sum(case when convert(varchar(20),b.tjtime,114) between '12:00:01' and '23:59:59' then 1 else 0 end) [12点23点]
from tba a join tbb b on a.a_id=b.aid