❶ 有一个表,存用户的登录时间和IP,写sql语句取某一天每小时的登录人数。如(0-1,1-2,....23-24)
----------不考虑IP重复的问题------------------
select DATEPART ( hour,<时间>) ,count(*) from <表>as s group by DATEPART ( hour,<时间>) ;
--------考虑IP重复问题-----------
select hournum,count(*) from (select DATEPART ( hour,<时间>) as hournum,<IP> from <表>) group by hournum
如果还要考虑某一天,那就加上where子句限定日期
❷ sql 查询每天都有登录的人
提供个思路。就是先查出一周内的扮蔽用户。然敏缺态后按天,人分组。按条件统计
例如 select count(日期),人名 from 表 where 时间=一周桥源的 group by 人 having coutn(日期)>5
❸ 每一天的新用户的次日留存率,这个SQL应该怎么处理
1select ( select count(distinct userid ) from 登录表 where 登录时间 = 今天 ) / ( select count(1) from 注册表 where 注册时间 = 昨天 ) as 户留存率
❹ 如何sql计算用户分层:新增、活跃、新召回、新沉默、持续沉默
第一天:
INSERT INTO user_status AS
SELECT ‘2019-01-01’ AS date, driver_id, ‘族模New’ AS status
FROM record
WHERE date = ‘2019-01-01’
GROUP BY 1;
从第二天开始, 每天和前一天的status比较
CREATE OR REPLACE TEMPORARY TABLE today_active AS
SELECT uid
FROM record
WHERE date = ‘<TODAY>坦御’
GROUP BY 1;
CREATE OR REPLACE TEMPORARY TABLE yesterday_status AS
SELECT uid, status
FROM user_status
WHERE date = day_add(<TODAY>让穗岩, -1)
AND status IN (‘Active’, ‘Resurrected’, ‘New’, ‘Churned’, ‘Stale’);
INSERT INTO user_status AS
SELECT
‘<TODAY>’ AS date,
COALESCE(t.uid, y.uid) AS uid,
CASE WHEN t.uid IS NOT NULL AND y.status IS NULL THEN ‘New’
WHEN t. uid IS NOT NULL AND y.status IN (‘Churned’, ‘Stale’) THEN ‘Resurrected’
WHEN t.uid IS NOT NULL AND y.status IN (‘New’, ‘Resurrected’, ‘Active’) THEN ‘Active’
WHEN t.uid IS NULL AND y.status IN (‘Churned’, ‘Stale’) THEN ‘Stale’
WHEN t.uid IS NULL AND y.status IN (‘New’, ‘Resurrected’, ‘Active’) THEN ‘Churned’
ELSE NULL END AS status
FROM today_active t FULL OUTER JOIN yesterday_status y
ON t.uid = y.uid;
这样的结果, 最后会逐渐得到一个名叫user_status的表, 里面记录每一天每一个用户的status.
❺ sql怎么获取每个用户,最新的一次登陆数据
select 用户id,max(时间) from tablename group by 用户id。
这样就能获取每个用户最新一次登录的数据
❻ SQL查询--查询用户每个用户的最后一次登录记录
1、创建测试升冲凯表,
create table test_log(user_id number, v_date date);
❼ SQL查询是否是老客户
思路是先查兄陪老客户的,再查新客户的,最后启薯把两个结果合并
select A.客户姓名,1 from A,B where A.ID=B.ID and B.消费日期 BETWEEN 开始日期 and 结束日期
union all
select A.客户姓名,0 from A where A.ID not in (select B.ID from B where B.消费日期 BETWEEN 开悄尘者始日期 and 结束日期)