sqlServer变量运用示例

1.定义变量
declare @startTime varchar(255)
-- 2.设置变量
set @startTime = convert(VARCHAR(10),dateadd(day,-1,getdate()),120)+'%'
--  也可以从这里指定要查询的日期
-- set @startTime = '2017-04-05%'
-- 显示指定的日期
PRINT(@startTime)
select REPLACE(@startTime,'%','') as 日期,当日活跃用户,当日新增用户 from
(
        -- 3.查询当日活跃用户
        select '01' as id,count(1) as 当日活跃用户 from
        (
                select DISTINCT usId from loginLog t where CONVERT(varchar(100), t.addTime, 25) like @startTime
        ) as a
) as a2,
-- 4.查询当日新增用户
(select '01' as id,count(1) as 当日新增用户 from userT t where CONVERT(varchar(100), t.addTime, 25) like @startTime) b
where a2.id = b.id;

猜你喜欢

转载自blog.51cto.com/1197822/2157336
今日推荐