添加计算列

时间:2014-11-10 09:49:52

标签: sql-server


这是我的表: 使用这些列:

ShiftId ShiftNum    Date    ShiftType   StartTime   EndTime
1   1   2014-08-07  A   0:00:00 6:00:00
2   2   2014-08-07  B   6:01:00 18:00:00
3   3   2014-08-07  A   18:00:01    23:59:00
4   1   2014-08-08  A   0:00:00 6:00:00
5   2   2014-08-08  C   6:01:00 18:00:00
6   3   2014-08-08  B   18:00:01    23:59:00
7   1   2014-08-09  B   0:00:00 6:00:00
8   2   2014-08-09  C   6:01:00 18:00:00
9   3   2014-08-09  B   18:00:01    23:59:00
10  1   2014-08-10  B   0:00:00 6:00:00
11  2   2014-08-10  D   6:01:00 18:00:00
12  3   2014-08-10  C   18:00:01    23:59:00
13  1   2014-08-11  C   0:00:00 6:00:00
14  2   2014-08-11  D   6:01:00 18:00:00
15  3   2014-08-11  C   18:00:01    23:59:00
16  1   2014-08-12  C   0:00:00 6:00:00
17  2   2014-08-12  A   6:01:00 18:00:00
18  3   2014-08-12  D   18:00:01    23:59:00
19  1   2014-08-13  D   0:00:00 6:00:00
20  2   2014-08-13  A   6:01:00 18:00:00
21  3   2014-08-13  D   18:00:01    23:59:00
22  1   2014-08-14  D   0:00:00 6:00:00
23  2   2014-08-14  B   6:01:00 18:00:00

我想添加" DailyShiftId"列为computrd列表示当shiftType更改时,DailyShiftId已增加。

ShiftId ShiftNum    Date    ShiftType   StartTime   EndTime DailyShiftId
1   1   2014-08-07  A   0:00:00 6:00:00 1
2   2   2014-08-07  B   6:01:00 18:00:00    2
3   3   2014-08-07  A   18:00:01    23:59:00    3
4   1   2014-08-08  A   0:00:00 6:00:00 3
5   2   2014-08-08  C   6:01:00 18:00:00    4
6   3   2014-08-08  B   18:00:01    23:59:00    5
7   1   2014-08-09  B   0:00:00 6:00:00 5
8   2   2014-08-09  C   6:01:00 18:00:00    6
9   3   2014-08-09  B   18:00:01    23:59:00    7
10  1   2014-08-10  B   0:00:00 6:00:00 7
11  2   2014-08-10  D   6:01:00 18:00:00    8
12  3   2014-08-10  C   18:00:01    23:59:00    9
13  1   2014-08-11  C   0:00:00 6:00:00 9
14  2   2014-08-11  D   6:01:00 18:00:00    10
15  3   2014-08-11  C   18:00:01    23:59:00    11
16  1   2014-08-12  C   0:00:00 6:00:00 11
17  2   2014-08-12  A   6:01:00 18:00:00    12
18  3   2014-08-12  D   18:00:01    23:59:00    13
19  1   2014-08-13  D   0:00:00 6:00:00 13
20  2   2014-08-13  A   6:01:00 18:00:00    14
21  3   2014-08-13  D   18:00:01    23:59:00    15
22  1   2014-08-14  D   0:00:00 6:00:00 15
23  2   2014-08-14  B   6:01:00 18:00:00    16

我该怎么办?

3 个答案:

答案 0 :(得分:0)

您可以将DailyShiftId添加为普通列,并在表格中添加AFTER UPDATE Trigger

CREATE TRIGGER TAU_MyTable
ON MyTable
AFTER UPDATE
AS
BEGIN

    UPDATE MyTable SET DailyShiftId = DailyShiftId + 1 
    WHERE [INSERTED].ShiftType <> [DELETED].ShiftType

END

OR

您可以向数据库添加一个函数来计算新列的值,并使用计算列规范,如下所示:

http://www.c-sharpcorner.com/UploadFile/rohatash/formula-for-computed-column-specification-property-in-sql-se/

答案 1 :(得分:0)

在更新触发器上创建

CREATE TRIGGER ViewEmployeeTrigger ON Employee
INSTEAD OF UPDATE
AS
BEGIN
SET NOCOUNT ON

UPDATE Employee 
SET DailyShiftId=DailyShiftId+1
-- what ever your updation logic
FROM INSERTED I JOIN Employee C ON I.ShiftId= C.ShiftId


END
GO

答案 2 :(得分:0)

此查询有助于您在不使用触发器的情况下获得预期结果。

  DECLARE @Test Table
(
    ShiftId Integer,
    ShiftNum Int,
    Date DATETIME,
    ShiftType VARCHAR(1)
)

INSERT INTO @Test (ShiftId, ShiftNum, Date, ShiftType)

SELECT 1,   1,   '2014-08-07',  'A'    UNION ALL
SELECT 2,   2,   '2014-08-07',  'B'    UNION ALL
SELECT 3,   3,   '2014-08-07',  'A'    UNION ALL
SELECT 4,   1,   '2014-08-08',  'A'    UNION ALL
SELECT 5,   2,   '2014-08-08',  'C'    UNION ALL
SELECT 6,   3,   '2014-08-08',  'B'    UNION ALL
SELECT 7,   1,   '2014-08-09',  'B'         

   SELECT C.ShiftId, C.ShiftNum, C.Date, C.ShiftType , 
        DENSE_RANK() OVER( order by C.DailyShift ASC) AS DailyShift
    FROM 
    (
        SELECT A.*,
            CASE WHEN A.ShiftType = B.ShiftType  
                Then ROW_NUMBER() OVER(ORDER BY A.ShiftId ASC) - 1 
                Else  ROW_NUMBER() OVER(ORDER BY A.ShiftId ASC)
            END AS DailyShift
        FROM @Test AS A LEFT JOIN @Test AS B ON A.ShiftId = B.ShiftId + 1  
    ) AS C

使用计算列:

将列添加到指定函数的表(计算)。

  ALTER TABLE dbo.TableName
    ADD DailyShiftID AS dbo.Calculate(ShiftId, ShiftType)

CREATE FUNCTION dbo.Calculate(@nShiftId INT, @sShiftType VARCHAR(1))
RETURNS INT
AS
  BEGIN
    DECLARE @sPrevShiftType AS VARCHAR(1)
    DECLARE @nDailyShift AS INT

    SELECT @sPrevShiftType =  ShiftType, @nDailyShift =  ISNULL(DailyShiftID, 0) 
    FROM TableName WHERE ShiftId = @nShiftId - 1

    IF (@sPrevShiftType <> @sShiftType)
        SET @nDailyShift = @nDailyShift + 1

      RETURN @nDailyShift        
  END