从具有多个条件的两个表中查询

时间:2011-04-27 08:45:37

标签: sql-server

  

可能重复:
  joining two table with different conditions of different column

tbl_User

ID  mobileno        dateofregistration       registrationstate 
44  1674174925  2011-04-18 10:17:30.670         0
45  1677864168  2011-03-31 10:20:22.450         1
46  1677864161  2011-04-18 20:47:35.293         0
47  1674174925  2011-03-29 09:28:55.200         1
48  1674174967  2011-03-29 09:28:55.100         1

Tbl_cdr

slno   ano            startTime
0   1677864161  2011-03-29 14:37:35.000
0   1677864161  2011-03-29 15:36:47.000
0   1677864165  2011-03-31 15:07:00.000
0   1677864166  2011-03-31 15:04:13.000
0   1677864167  2011-03-29 14:36:17.000
0   1677864167  2011-03-31 15:04:46.000
0   1677864168  2011-03-31 15:00:07.000
0   1677864168  2011-03-31 15:01:39.000
0   1677864168  2011-03-31 15:07:44.000
0   1677864168  2011-03-31 15:08:14.000

输出

   Date    ActFail  NewAct     RenFail  SuccessRen
29/03/2011   1        2           0         0
31/03/2011   3        1           0         0

条件

1.Date  : group by startTime ;
2. ActFail : Count(*) From Tbl_cdr with condition ano!=mobileno.`
3.NewAct : `count (*) from Tbl_user with condition registrationstate=1`
4.RenFail : `count * from Tbl_cdr with condition (ano=mobileno and registrationstate=0)`
5.SuccessRate : Count(*) from tbl_user when only registratonstate changed from 0 to 1

     All query will be groupby Tbl_cdr.startTime and between user given timelimit.

我,hv尝试了这个,但徒劳无功

select
  Convert(varchar(10), startTime,103) as Date,
  count(*) As ActivationFail,
  (Select COUNT(*) from tbl_User where registrationstate='1') as NewActivation
from Tbl_cdr c
  left join Tbl_User u  on c.ano = u.mobileno
where u.id is null and startTime between @date1 and @date2
group by Convert(varchar(10),startTime,103)

任何帮助,Plz ..是否可以用上述条件显示单个表中的所有5列???

1 个答案:

答案 0 :(得分:0)

你还没有解释你究竟需要做什么,但我可以提供一些建议。  删除所有转换和计数,并尝试先选择。您可以使用子查询来完成工作。