sql server 自动核算
作者:互联网
USE tempdb; CREATE TABLE #temptable ( [姓名] NVARCHAR(255), [加班日期] DATE, [加班时长] DECIMAL(8, 2) ); INSERT INTO #temptable ( [姓名], [加班日期], [加班时长] ) VALUES (N'张三', N'2021-10-09', 1), (N'张三', N'2021-10-08', 2), (N'张三', N'2021-10-07', 3), (N'张三', N'2021-10-06', 2); --定义参数 DECLARE @v DECIMAL(8, 2); SET @v = 2.2; WITH cte_base AS (SELECT *, ROW_NUMBER() OVER (PARTITION BY 姓名 ORDER BY 加班日期 DESC) n FROM #temptable), cte AS (SELECT cte_base.n, cte_base.姓名, cte_base.加班日期, cte_base.加班时长, CAST(加班时长 AS DECIMAL(8, 2)) AS sum_, '√' AS flag FROM cte_base WHERE n = 1 UNION ALL SELECT cte_base.n, cte_base.姓名, cte_base.加班日期, cte_base.加班时长, CAST((cte.sum_ + cte_base.加班时长) AS DECIMAL(8, 2)) AS sum_, CASE WHEN (cte.sum_ < @v) THEN '√' END AS flag FROM cte JOIN cte_base ON cte.n + 1 = cte_base.n) SELECT * FROM cte;
标签:10,DECIMAL,加班,cte,base,sql,server,核算,SELECT 来源: https://www.cnblogs.com/BinBinGo/p/15480017.html