把多个where条件不同的sql语句合并为一个?
Posted
tags:
篇首语:本文由小常识网(cha138.com)小编为大家整理,主要介绍了把多个where条件不同的sql语句合并为一个?相关的知识,希望对你有一定的参考价值。
declare @area nvarchar(4000)
declare @date datetime
set @area='北京'
set @date='2013-5-8';
with result as(
select
( select count(chatkind) from visitors a where
chatkind in (5,6,7) and convert(varchar(10),vtime,120)= @date ) as '总有效对话',
(select count(chatkind) from visitors where
chatkind in (5,6,7) and convert(varchar(10),vtime,120)= @date and ipfrom like '%'+@area+'%' ) as '本地有效对话' ,
(select count(chatkind) from visitors where
chatkind =7 and convert(varchar(10),vtime,120)= @date and ipfrom like '%'+@area+'%' ) as '本地极佳对话' ,
(select count (distinct (cname)) from visitors where
ipfrom like '%'+@area+'%' and convert(varchar(10),vtime,120)= @date and
(ascii(SUBSTRING(cname,0,4)) between 65 and 123) and (ascii(SUBSTRING(cname,4,9)) between 48 and 57) ) as '总本地预约'
) select * from result
这样的slq语句,怎么能合并为一条语句,请各位大神帮忙;
(
select
count(case when chatkind in(5,6,7) then chatkind else null end) as '总有效对话',
count(case when chatkind in(5,6,7) and ipfrom like '%'+@area+'%' then chatkind
else null end) as '本地有效对话',
count(case when chatkind=7 and ipfrom like '%'+@area+'%' then chatkind
else null end) as '本地极佳对话',
count(distinct case when ipfrom like '%'+@area+'%' and ascii(substring(cname,0,4))
between 65 and 123) and (ascii(substring(cname,4,9)) between 48 and 57))
then cname else null end) as '总本地预约'
from
visitors
where
convert(varchar(10),vtime,120)=@date
)
select * from result;本回答被提问者采纳
以上是关于把多个where条件不同的sql语句合并为一个?的主要内容,如果未能解决你的问题,请参考以下文章
oracle SQL查询中,如何在where中用条件语句,判断不同情况,追加不同的And条件?