编写查询以显示图像中的样子

问题描述 投票:0回答:5

提供的表格显示了在特定日期注册的所有新用户,格式为

YYYY-MM-DD

您的查询应输出从一个月到下一个月的变化。由于第一个月没有前一个月,因此您的输出应跳过该行。您的输出应如下表所示。

我的表格数据

表格数据:

ID    DateJoined
1     2017-01-06
2     2017-01-12
3     2017-01-16
4     2017-01-25
5     2017-02-05
6     2017-02-07
7     2017-02-21
8     2017-03-05
9     2017-03-07
10    2017-03-14
11    2017-03-16
12    2017-03-25
13    2017-03-25
14    2017-03-25
15    2017-03-25
16    2017-03-26
17    2017-04-05
18    2017-04-14
19    2017-04-21
20    2017-05-07
23    2017-05-14
24    2017-05-16
25    2017-05-25
26    2017-05-25
27    2017-05-25
28    2017-05-25

Enter image description here

我想要这个输出: 统计每个月的所有记录并从下个月的记录中减去它。

这是我的询问:

SELECT
   MONTH(L.joindate),
   COUNT(L.joindate) -  COUNT(R.joindate),
   MONTH(R.joindate),
   COUNT(R.joindate)
FROM
   userlog       AS L
LEFT JOIN
   userlog       AS R
      ON MONTH(R.joindate)= (SELECT MIN(MONTH(joindate)) FROM userlog WHERE MONTH(joindate) < MONTH(L.joindate))
GROUP BY (MONTH(L.joindate)),(MONTH(R.joindate));

1

mysql sql count subquery aggregate-functions
5个回答
1
投票

使用

lag()
,在 MySQL 8.0 中可用:

select date_format(joindate, '%Y-%m-01') joinmonth,
    count(*) - lag(count(*), 1, 0) over(order by date_format(joindate, '%Y-%m-01')) m2m
from userlog
group by joinmonth

请注意,我更改了将日期截断为月初的逻辑以使用

date_format()

在早期版本中,您可以使用相关子查询:

select date_format(joindate, '%Y-%m-01') joinmonth,
    count(*) - (
        select count(*)
        from userlog l1
        where l1.joindate >= date_format(l.joindate, '%Y-%m-01') - interval 1 month
          and l1.joindate <  date_format(l.joindate, '%Y-%m-01')
    ) m2m
from userlog l
group by joinmonth
LIMIT 12 OFFSET 1

1
投票
select 
      MONTHNAME(UL1.DateJoined) as MONTH,
      count(UL1.DateJoined) -
(
 select count(UL2.DateJoined)
 from tablename UL2
 where MONTH(UL2.DateJoined )=MONTH(UL1.DateJoined) -1
 ) as MonthToMonthChange
 from tablename UL1
 where Month(UL1.DateJoined)!=1
 Group by MONTHNAME(UL1.DateJoined)
 order by UL1.DateJoined ASC; 

https://i.stack.imgur.com/BXXDb.png


0
投票

你需要使用滞后。另外,因为它说你需要跳过第一行,所以我使用了非空条件。我相信这个查询应该有效。

select 
  Month, 
  MonthToMonthChange 
from 
  (
    select 
      m_name as Month, 
      (total_id - diff) as MonthToMonthChange 
    from 
      (
        select 
          total_id, 
          m_name, 
          Lag(total_id, 1) OVER(
            ORDER BY 
              m_num ASC
          ) AS diff 
        from 
          (
            select 
              MonthNAME(DateJoined) m_name, 
              Month(DateJoined) m_num, 
              count(*) total_id 
            from 
              maintable 
            Group by 
              m_name, 
              m_num
          ) as first_subquery
      ) as second_subquery
  ) as final_query 
where 
  MonthToMonthChange IS NOT NULL;

0
投票

带有 as( 从用户日志中选择月份(joindate) joindate,count(joindate)datec 按月分组(加入日期) ) 从 ( 中选择 * 选择 datename(month,datefromparts(2021,joindate,1))MONTH,datec-lag(datec) over(order by joindate)MonthToMonthChange from a ) b 其中 MonthToMonthChange 不为 null

当您在 sql server 中运行上述脚本时,您将得到以下输出

enter image description here


-1
投票

我尝试过这个并且有效

select date_format(DateJoined, CONCAT('%M')) as Month,
count(*) - lag(count(*), 1, 0) over(order by date_format(DateJoined, CONCAT('%m'))) MonthToMonthChange
from maintable_OKLOT
group by Month
limit 12 offset 1
© www.soinside.com 2019 - 2024. All rights reserved.