SQL 服务器 - 从同一视图中的其他字段创建一个字段的值 table

SQL Server - create value one field from other field in same View table

我需要你的帮助,我不知道具体的问题名称。我想在同一 table 视图中将其他字段的记录创建到一个字段中。但是当我创建它时,显示的值来自同一条记录。

我有 1 个 table 有 4 个字段 有 YEAR,PEOPLE,REMAINING,CF

这样的逻辑:

  1. IF people A in 2014 have remaining value 25 and CF values 0 THEN people A in 2015 will have CF values 6.

  2. IF Remaining > 6 THEN CF Values only 6, IF Remaining between 0 to 6 THEN CF Values = Remaining, IF Remaining < 0 THEN CF Values 0

  3. IF values in Current Year will always take from Remaining in Last Year so if People A didn't have remaining in last year, the CF in current year become 0

这是我的代码:

SELECT  
    A.FiscalYear,
    C.EmployeeName, 
    CASE 
        WHEN A.FiscalYear < 2015 THEN A.Remaining 
        WHEN B_1.daystaken < 0 THEN A.Remaining + ISNULL(B_1.DaysTaken, 0) 
        ELSE A.Entitlement 
    END AS Remaining,
    CASE 
        WHEN A.remaining > 6 THEN 6
        WHEN A.remaining BETWEEN 0 AND 6 THEN A.remaining
        WHEN A.remaining < 0 THEN 0
        ELSE ISNULL(A.remaining,0)
    END AS CF
FROM  dbo.DataLeaveBalance AS A 
LEFT OUTER JOIN dbo.VWLeave_takens AS B_1 
    ON A.FiscalYear = B_1.AffectFY 
    AND A.EmpCode = B_1.EmpCode 
LEFT OUTER JOIN dbo.MasterEmployee AS C
    ON A.EmpCode = C.EmpCode
This the result from my code:

year        people  remain      cf
----------- ------- ----------- -----------
2014        Jackson 14          6
2014        Eva     5           5
2014        Akson   0           0
2015        Jackson 10          6
2015        Eva     10          6
2015        Akson   13          6

I want the result like this:

year        people  remain      cf
----------- ------- ----------- -----------
2014        Jackson 14          0
2014        Eva     5           0
2014        Akson   0           0
2015        Jackson 10          6
2015        Eva     10          5
2015        Akson   13          0
2016        Jackson 10          6
2016        Eva     10          6
2016        Akson   13          6

每行中只有当前年份的可用值。并且您的计算需要访问上一年的值。

您可以使用像 FiscalYear-1 这样的连接获取前一年的值

现在您可以在行级别获得当前 (B_1) 和上一年 (B_2) 的信息,您可以在计算中使用它。

SELECT  
    *
FROM  dbo.DataLeaveBalance AS A 
LEFT OUTER JOIN dbo.VWLeave_takens AS B_1 
    ON A.FiscalYear = B_1.AffectFY 
    AND A.EmpCode = B_1.EmpCode 
LEFT OUTER JOIN dbo.VWLeave_takens AS B_2
    ON (A.FiscalYear-1) = B_1.AffectFY 
    AND A.EmpCode = B_1.EmpCode 
LEFT OUTER JOIN dbo.MasterEmployee AS C
    ON A.EmpCode = C.EmpCode

如果我没记错的话你需要使用 CTE。

像这样:

;WITH CTE AS(
    SELECT  
    A.FiscalYear,
    C.EmployeeName, 
    CASE 
        WHEN A.FiscalYear < 2015 THEN A.Remaining 
        WHEN B_1.daystaken < 0 THEN A.Remaining + ISNULL(B_1.DaysTaken, 0) 
        ELSE A.Entitlement 
    END AS Remaining
    FROM  dbo.DataLeaveBalance AS A 
    LEFT OUTER JOIN dbo.VWLeave_takens AS B_1 
        ON A.FiscalYear = B_1.AffectFY 
        AND A.EmpCode = B_1.EmpCode 
    LEFT OUTER JOIN dbo.MasterEmployee AS C
        ON A.EmpCode = C.EmpCode
    )
SELECT *,   CASE 
                WHEN CTE.Remaining > 6 THEN 6
                WHEN CTE.Remaining BETWEEN 0 AND 6 THEN CTE.Remaining
                WHEN CTE.Remaining < 0 THEN 0
                ELSE ISNULL(CTE.Remaining,0)
            END AS CF
FROM CTE