使用Sql获得每年增加工资列的工资

时间:2017-05-21 16:46:51

标签: sql sql-server-2008

我要求员工逐年增加员工年薪,在我进入之前,请先看下面的示例:

样本1:1 - 4年增量

EmpID   EmpName 1st Increment   2nd Increment   3rd Increment   4th Increment
1       John    1000            2000            3000            4000
2       Jack    2000            4000            8000            16000

样本2:5 - 8年增量

EmpID   EmpName 1st Increment   2nd Increment   3rd Increment   4th Increment
1       John    5000            6000            7000            8000
2       Jack    32000           64000           128000          256000

我想,我能够理解它究竟是什么。列1-4的增量应该保持静态,并且增加1 - 4年,第一个样本可以被管理,我已经尝试了查询,但效率不高:(想法是获得第1,第2,第3 ,样本1)的特定id 1的第4行

WITH CTE AS 
(
    SELECT 
        *, 
        ROW_NUMBER() OVER (ORDER BY EmpID ASC) as RowNum
    FROM EmpIncrement 
    WHERE EmpID = 1
)
SELECT 
    CTE.EmpID AS ID, CTE.Amt AS Amount,
    (CASE WHEN CTE.RowNum = 1 THEN CTE.Amt ELSE 0 END) AS '1st Increment',
    (CASE WHEN CTE.RowNum = 2 THEN CTE.Amt ELSE 0 END) AS '2nd Increment'
FROM 
    CTE 
WHERE 
    CTE.RowNum IN (1, 2)

这就是我所拥有的:

sample_increment

在样本2中,当涉及到第5个增量时,增量应该放在< 1st Increment '列和其余相应的。可能有一个很好的解决方案,但无法完成。可以通过简单的方法完成,或任何想法/样品将不胜感激。感谢。

表结构和示例如下(脚本):

USE [Demo]
GO

SET ANSI_NULLS ON
GO
SET QUOTED_IDENTIFIER ON
GO

CREATE TABLE [dbo].[Employee]
(
    [ID] [int] IDENTITY(1,1) NOT NULL,
    [NAME] [nvarchar](100) NULL,
    [ADDRESS] [nvarchar](max) NULL,
    [SALARY] [float] NULL,

    CONSTRAINT [PK__Employee__3214EC2714270015] 
        PRIMARY KEY CLUSTERED ([ID] ASC)
) ON [PRIMARY]
GO

SET IDENTITY_INSERT [dbo].[Employee] ON

INSERT [dbo].[Employee] ([ID], [NAME], [ADDRESS], [SALARY]) 
VALUES (1, N'John', N'Germany', 12000)
INSERT [dbo].[Employee] ([ID], [NAME], [ADDRESS], [SALARY]) 
VALUES (2, N'Jack', N'France', 14000)

SET IDENTITY_INSERT [dbo].[Employee] OFF


SET ANSI_NULLS ON
GO
SET QUOTED_IDENTIFIER ON
GO

CREATE TABLE [dbo].[EmpIncrement]
(
    [IncrementID] [int] IDENTITY(1,1) NOT NULL,
    [EmpID] [int] NULL,
    [Amt] [float] NULL,
    [IncrementDate] [datetime] NULL,

    CONSTRAINT [PK_EmpIncrement] 
        PRIMARY KEY CLUSTERED ([IncrementID] ASC)
) ON [PRIMARY]
GO

SET IDENTITY_INSERT [dbo].[EmpIncrement] ON

INSERT [dbo].[EmpIncrement] ([IncrementID], [EmpID], [Amt], [IncrementDate]) 
VALUES (1, 1, 2000, CAST(0x0000A6D800000000 AS DateTime))
INSERT [dbo].[EmpIncrement] ([IncrementID], [EmpID], [Amt], [IncrementDate]) 
VALUES (2, 2, 8000, CAST(0x0000A6D800000000 AS DateTime))
INSERT [dbo].[EmpIncrement] ([IncrementID], [EmpID], [Amt], [IncrementDate]) 
VALUES (3, 1, 1000, CAST(0x0000A56A00000000 AS DateTime))
INSERT [dbo].[EmpIncrement] ([IncrementID], [EmpID], [Amt], [IncrementDate]) 
VALUES (4, 2, 4000, CAST(0x0000A56A00000000 AS DateTime))

SET IDENTITY_INSERT [dbo].[EmpIncrement] OFF

1 个答案:

答案 0 :(得分:1)

看看是否有效。

WITH CTE AS 
(
    SELECT e.ID,e.[Name],ei.Amt, 
        ROW_NUMBER() OVER (PARTITION BY EmpID ORDER BY DATEPART(yy,IncrementDate)) as RowNum
    FROM EmpIncrement ei 
         JOIN Employee e ON ei.EmpID = e.ID
),

CTE2 AS
    (
    SELECT CTE.ID 'Id',CTE.[NAME] 'Name',
    SUM(CASE WHEN CTE.RowNum = 1 THEN CTE.Amt ELSE 0 END) AS '1stIncrement',
    SUM(CASE WHEN CTE.RowNum = 2 THEN CTE.Amt ELSE 0 END) AS '2ndIncrement',
    SUM(CASE WHEN CTE.RowNum = 3 THEN CTE.Amt ELSE 0 END) AS '3rdIncrement',
    SUM(CASE WHEN CTE.RowNum = 4 THEN CTE.Amt ELSE 0 END) AS '4thIncrement',
    SUM(CASE WHEN CTE.RowNum = 5 THEN CTE.Amt ELSE 0 END) AS '5thIncrement',
    SUM(CASE WHEN CTE.RowNum = 6 THEN CTE.Amt ELSE 0 END) AS '6thIncrement',
    SUM(CASE WHEN CTE.RowNum = 7 THEN CTE.Amt ELSE 0 END) AS '7thIncrement',
    SUM(CASE WHEN CTE.RowNum = 8 THEN CTE.Amt ELSE 0 END) AS '8thIncrement'
    FROM CTE 
    GROUP BY CTE.ID,CTE.[NAME]
    )

SELECT ID,[NAME], 
       CASE WHEN [5thIncrement] > 0 THEN [5thIncrement] ELSE [1stIncrement] END AS '1st Increment',
       CASE WHEN [6thIncrement] > 0 THEN [6thIncrement] ELSE [2ndIncrement] END AS '2nd Increment',
       CASE WHEN [7thIncrement] > 0 THEN [7thIncrement] ELSE [3rdIncrement] END AS '3rd Increment',
       CASE WHEN [8thIncrement] > 0 THEN [8thIncrement] ELSE [4thIncrement] END AS '4th Increment'
FROM CTE2