Mysql 每周累计计数

Mysql 每周累计计数,mysql,sql,sql-server,Mysql,Sql,Sql Server,目前我使用MySQL,但很快就会迁移到SQL Server,所以这两种方法都适用于解决方案 我有一个数据库模式,如(候选表): 表如下所示: | ID | intern | name | +------------+-----------------+-----------+ | 1 | '2020-01-02' | Mike | | 42 | '2020-01-05' | Nora |

目前我使用MySQL,但很快就会迁移到SQL Server,所以这两种方法都适用于解决方案

我有一个数据库模式,如(候选表):

表如下所示:

| ID         | intern          |   name    |
+------------+-----------------+-----------+
| 1          |  '2020-01-02'   |   Mike    |
| 42         |  '2020-01-05'   |   Nora    |
| 21         |  '2020-01-08'   |   Iris    |
| 331        |  '2020-01-12'   |   Caro    |
以及该表中其他一些不相关的列

我想按周计算所有实习日期。这是有效的:

SELECT 
    WEEK(intern) + 1 AS KW, COUNT(intern) AS counts
FROM 
    epunkt_sourcing.candidate
WHERE 
    WEEK(intern) IS NOT NULL AND YEAR(intern) = 2020
GROUP BY 
    KW
ORDER BY 
    KW;
结果是这样的:

| KW        | counts          |
+-----------+-----------------+
| 1         |  2              |
| 2         | 19              |
| 3         | 18              |
| 4         | 21              |
我还要每周的累计计数,是否有机会将计数相加

| KW        | counts          | cumulated   |
+-----------+-----------------+-------------+
| 1         |  2              |  2          |
| 2         | 19              | 21          |
| 3         | 18              | 39          |
| 4         | 21              | 60          |

您可以在SQL Server中尝试以下操作:

Declare @t table  (KW int,counts int)

Insert into @t values (1,2),(2,19),(3,18),(4,21)

select *, sum(counts) over (order by kw) as cumulated 
from @t
输出:

KW  counts  cumulated  
1     2           2  
2     19         21  
3     18         39  
4     21         60  

我认为这应该行得通。不确定这是否是最有效的

    SELECT WEEK(intern)+1 as KW, count(intern) as counts into #tempCount
    from epunkt_sourcing.candidate
    WHERE WEEK(intern) IS NOT NULL AND YEAR(intern) = 2020
    GROUP BY WEEK(intern)

    select
     a.kw
    ,a.counts
    ,sum(b.counts) as cumulated

    from #tempCount a
    inner join #tempCount b on a.kw >= b.kw

    group by a.kw, a.counts
    order by a.kw


请测试它并让我知道

请尝试以下查询,希望它能有所帮助:

SELECT t.KW, t.counts, @cum_total:=@cum_total + t.counts AS cumulative_sum
    FROM
        ( 
            SELECT WEEK(intern)+1 as KW, count(intern) as counts
                FROM epunkt_sourcing.candidate
            WHERE WEEK(intern) IS NOT NULL AND YEAR(intern) = 2020
            GROUP BY WEEK(intern)
        ) AS t
    JOIN (SELECT @cum_total:=0) r;

在MySQL和SQL Server中,应该使用累积和。MySQL版本是:

SELECT WEEK(intern)+1 as KW, COUNT(*) as counts,
      SUM(COUNT(*)) OVER (ORDER BY MIN(intern)) as cumulative
FROM epunkt_sourcing.candidate
WHERE intern >= '2020-01-01' AND intern < '2021-01-01'
GROUP BY KW
ORDER BY KW;
选择周(实习)+1作为功率,计数(*)作为计数,
累计(按分钟(实习生)排序)的总和(计数(*)
来自epunkt_采购部
其中实习生>='2020-01-01'和实习生<'2021-01-01'
按千瓦分组
按千瓦订购;
在SQL Server中,您将使用:

SELECT DATE_PART(WEEK, intern)+1 as KW, COUNT(*) as counts,
      SUM(COUNT(*)) OVER (ORDER BY MIN(intern)) as cumulative
FROM epunkt_sourcing.candidate
WHERE intern >= '2020-01-01' AND intern < '2021-01-01'
GROUP BY DATE_PART(WEEK, intern)+1 
ORDER BY KW;
选择日期零件(周,实习生)+1作为功率,计数(*)作为计数,
累计(按分钟(实习生)排序)的总和(计数(*)
来自epunkt_采购部
其中实习生>='2020-01-01'和实习生<'2021-01-01'
按日期分组\u部分(周,实习生)+1
按千瓦订购;
注:

  • 根据年份的不同,第一周可能是部分周。周从周一开始,但年可以从任何日期开始
  • 该年的比较使用日期不平等。这在两个数据库中都是推荐的
  • 不同数据库的日期部分函数不同
  • SQL Server不允许在
    分组依据中使用列别名
  • 这两个数据库都支持窗口功能。MySQL支持变量,但它们现在已被弃用,因此您不应该使用它们

MySQL还是SQL Server?您已经标记了两者?我现在使用mysql,但将在接下来的几周内迁移到sql server,所以这两个都是用于解决方案的。好的。目前我使用mysql的版本是什么?这对你的问题至关重要。。。。并提供一些fiddle示例(并显示fiddle数据所需的结果,而不是抽象数据)。此MySQL版本的累积和使用2个表副本或用户定义的变量实现。这两个方法在SQL server中使用时都必须完全重写,其中存在
SUM()OVER()
。如果要迁移到SQL Server,请将MySQL更新到最新(8.0.19)版本。
SELECT DATE_PART(WEEK, intern)+1 as KW, COUNT(*) as counts,
      SUM(COUNT(*)) OVER (ORDER BY MIN(intern)) as cumulative
FROM epunkt_sourcing.candidate
WHERE intern >= '2020-01-01' AND intern < '2021-01-01'
GROUP BY DATE_PART(WEEK, intern)+1 
ORDER BY KW;