求解sql题,能写出来有偿

题目描述

计算新用户数,写出sql代码

题目来源及自己的思路

需要先转换日期格式吗,把本周和上周时间用sql定义出来

相关代码

粘贴代码文本(请勿用截图)

你期待的结果是什么?实际看到的错误信息又是什么?

阅读 1.5k
1 个回答
-- 表
fiance:表名 字段( date:时间 id:ID name:名称 name2:名称2 consume:消耗)


-- 今年
select count(*) from fiance as current_not_empty where year(date) = year(CURDATE()) and WEEK(date) = WEEK(CURDATE()) group by id having sum(consume) > 0 and 
exists (
    select id from fiance where year(date) = year(CURDATE()) and WEEK(date) = WEEK(CURDATE()) - 1 and id = current_not_empty.id group by id having sum(consume) = 0
)

-- 去年
select count(*) from fiance as current_not_empty where year(date) = year(CURDATE()) - 1 and WEEK(date) = WEEK(CURDATE()) group by id having sum(consume) > 0 and 
exists (
    select id from fiance where year(date) = year(CURDATE()) - 1 and WEEK(date) = WEEK(CURDATE()) - 1 and id = current_not_empty.id group by id having sum(consume) = 0
)
撰写回答
你尚未登录,登录后可以
  • 和开发者交流问题的细节
  • 关注并接收问题和回答的更新提醒
  • 参与内容的编辑和改进,让解决方法与时俱进
推荐问题