sql server 自动核算

2021/10/29 19:12:48

本文主要是介绍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;

 



这篇关于sql server 自动核算的文章就介绍到这儿,希望我们推荐的文章对大家有所帮助,也希望大家多多支持为之网!


扫一扫关注最新编程教程