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;
發表評論
所有評論
還沒有人評論,想成為第一個評論的人麼? 請在上方評論欄輸入並且點擊發布.
相關文章