2017-05-21 39 views
0

我有一個要求,讓員工的工資每年遞增明智列,在我進入,請參閱下面的示例第一:拿工資每年遞增逐列使用SQL

樣品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)特定的id爲1的行

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增量,那麼增加量應被放置在'第一增量'列中,其餘部分相應地放置。可能有一個很好的解決方案,但無法完成。可以用簡單的方法完成,或者任何想法/樣本,將不勝感激。謝謝。

表結構和示例如下(腳本):

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

見,如果這個工程。

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 
+0

非常感謝@Jason,工作完美。我想知道兩件事 - 我明白你在'CTE'和'CTE2'上試過的東西。在「SELECT」查詢中(第三節),你實際上做了什麼?雖然我知道一點,只是想澄清。另一件事是:它可以像第10次增量那樣動態化,列名爲'** 10th Increment **',等等,對於員工? –

+0

從CTE2中選擇的第三部分是檢查是否存在第5,6,7或8個增量。如果金額大於0,那麼邏輯將爲真,因此使用該值。如果該值爲0,則將使用較低的增量值。如果你增加到'EmpIncrement'表中,那麼你需要添加另一行代碼到CTE2和第三部分。您可以複製並粘貼並修改這些值。你不需要對第一部分做任何事情。 – Jason

+0

此外,您可以註釋掉最後一部分CTE2語法,然後運行查詢以查看其差異。這應該告訴你第三部分到底在做什麼。 – Jason