Regarding NIGHT SHIFT Employees












0















I have small issue on night shit of our employees .. Shift timings are from 22:00 to 6:00 Am .. If the shift starts today at 11 Pm it will end the next morning at 6:00 Am (7 Hours ). Here I want to know the first Swipe in and First last Swipe out.. Below here am providing the table structure and some sample data along with the expected output ....I need a procedure with inputs like @loginname, @startdate, @Enddate



CREATE TABLE [dbo].[CardData](
[CardDataID] [bigint] NOT NULL,
[DataMovementHistoryID] [bigint] NULL,
[LoginName] varchar NULL,
[ShiftID] [int] NULL,
[CardTime] [datetime] NOT NULL,
[IsInOrOut] [bit] NULL,
[IsProcessed] [bit] NULL
) ON [PRIMARY]



--TestCase 1--



INSERT INTO [dbo].[CardData] ([CardDataID], [DataMovementHistoryID], [LoginName], [ShiftID], [CardTime], [IsInOrOut], [IsProcessed]) VALUES (11290, 2200, 'B1000', 1, CAST(N'2019-06-01 9:24:00.000' AS DateTime), 1, 1)



INSERT INTO [dbo].[CardData] ([CardDataID], [DataMovementHistoryID], [LoginName], [ShiftID], [CardTime], [IsInOrOut], [IsProcessed]) VALUES (11291, 2200, 'B1000', 1, CAST(N'2019-06-01 12:55:00.000' AS DateTime), 0, 1)



INSERT INTO [dbo].[CardData] ([CardDataID], [DataMovementHistoryID], [LoginName], [ShiftID], [CardTime], [IsInOrOut], [IsProcessed]) VALUES (11292, 2200, 'B1000', 1, CAST(N'2019-06-01 15:24:00.000' AS DateTime), 1, 1)



INSERT INTO [dbo].[CardData] ([CardDataID], [DataMovementHistoryID], [LoginName], [ShiftID], [CardTime], [IsInOrOut], [IsProcessed]) VALUES (11293, 2200, 'B1000', 1, CAST(N'2019-06-01 17:55:00.000' AS DateTime), 0, 1)



INSERT INTO [dbo].[CardData] ([CardDataID], [DataMovementHistoryID], [LoginName], [ShiftID], [CardTime], [IsInOrOut], [IsProcessed]) VALUES (11294, 2200, 'B1000', 1, CAST(N'2019-06-02 10:24:00.000' AS DateTime), 0, 1)



INSERT INTO [dbo].[CardData] ([CardDataID], [DataMovementHistoryID], [LoginName], [ShiftID], [CardTime], [IsInOrOut], [IsProcessed]) VALUES (11295, 2200, 'B1000', 1, CAST(N'2019-06-02 13:55:00.000' AS DateTime), 1, 1)



INSERT INTO [dbo].[CardData] ([CardDataID], [DataMovementHistoryID], [LoginName], [ShiftID], [CardTime], [IsInOrOut], [IsProcessed]) VALUES (11296, 2200, 'B1000', 1, CAST(N'2019-06-02 14:24:00.000' AS DateTime), 0, 1)



INSERT INTO [dbo].[CardData] ([CardDataID], [DataMovementHistoryID], [LoginName], [ShiftID], [CardTime], [IsInOrOut], [IsProcessed]) VALUES (11297, 2200, 'B1000', 1, CAST(N'2019-06-02 16:01:00.000' AS DateTime), 0, 1)



INSERT INTO [dbo].[CardData] ([CardDataID], [DataMovementHistoryID], [LoginName], [ShiftID], [CardTime], [IsInOrOut], [IsProcessed]) VALUES (11295, 2200, 'B1000', 1, CAST(N'2019-06-03 08:24:00.000' AS DateTime), 1, 1)



INSERT INTO [dbo].[CardData] ([CardDataID], [DataMovementHistoryID], [LoginName], [ShiftID], [CardTime], [IsInOrOut], [IsProcessed]) VALUES (11296, 2200, 'B1000', 1, CAST(N'2019-06-03 13:55:00.000' AS DateTime), 0, 1)



INSERT INTO [dbo].[CardData] ([CardDataID], [DataMovementHistoryID], [LoginName], [ShiftID], [CardTime], [IsInOrOut], [IsProcessed]) VALUES (11297, 2200, 'B1000', 1, CAST(N'2019-06-03 15:24:00.000' AS DateTime), 0, 1)



INSERT INTO [dbo].[CardData] ([CardDataID], [DataMovementHistoryID], [LoginName], [ShiftID], [CardTime], [IsInOrOut], [IsProcessed]) VALUES (11298, 2200, 'B1000', 1, CAST(N'2019-06-03 17:01:00.000' AS DateTime), 1, 1)



---@loginname='B1000'
---@startdate='2019-06-01'
---@Enddate='2019-06-15'



loginname , DT , FirstIn , InFlag , LastOut , OutFlag

B1000 , 2019-06-02 ,2019-06-02 10:24:00, 0 , 2019-06-02 16:01:00, 0

B1000 , 2019-06-03, 2019-06-03 8:24:00 ,1 , 2019-06-03 17:01:00 ,1



INSERT INTO [dbo].[CardData] ([CardDataID], [DataMovementHistoryID], [LoginName], [ShiftID], [CardTime], [IsInOrOut], [IsProcessed]) VALUES (11299, 2200, 'C2000', 8, CAST(N'2019-07-01 23:05:00.000' AS DateTime), 0, 1)



INSERT INTO [dbo].[CardData] ([CardDataID], [DataMovementHistoryID], [LoginName], [ShiftID], [CardTime], [IsInOrOut], [IsProcessed]) VALUES (11300, 2200, 'C2000', 8, CAST(N'2019-07-02 00:55:00.000' AS DateTime), 0, 1)



INSERT INTO [dbo].[CardData] ([CardDataID], [DataMovementHistoryID], [LoginName], [ShiftID], [CardTime], [IsInOrOut], [IsProcessed]) VALUES (11301, 2200, 'C2000', 8, CAST(N'2019-07-02 3:24:00.000' AS DateTime), 1, 1)



INSERT INTO [dbo].[CardData] ([CardDataID], [DataMovementHistoryID], [LoginName], [ShiftID], [CardTime], [IsInOrOut], [IsProcessed]) VALUES (11302, 2200, 'C2000', 8, CAST(N'2019-07-02 5:05:00.000' AS DateTime), 1, 1)



INSERT INTO [dbo].[CardData] ([CardDataID], [DataMovementHistoryID], [LoginName], [ShiftID], [CardTime], [IsInOrOut], [IsProcessed]) VALUES (11303, 2200, 'C2000', 8, CAST(N'2019-07-03 23:24:00.000' AS DateTime), 1, 1)



INSERT INTO [dbo].[CardData] ([CardDataID], [DataMovementHistoryID], [LoginName], [ShiftID], [CardTime], [IsInOrOut], [IsProcessed]) VALUES (11304, 2200, 'C2000', 8, CAST(N'2019-07-04 02:55:00.000' AS DateTime), 0, 1)



INSERT INTO [dbo].[CardData] ([CardDataID], [DataMovementHistoryID], [LoginName], [ShiftID], [CardTime], [IsInOrOut], [IsProcessed]) VALUES (11305, 2200, 'C2000', 8, CAST(N'2019-07-04 4:24:00.000' AS DateTime), 1, 1)



INSERT INTO [dbo].[CardData] ([CardDataID], [DataMovementHistoryID], [LoginName], [ShiftID], [CardTime], [IsInOrOut], [IsProcessed]) VALUES (11306, 2200, 'C2000', 8, CAST(N'2019-07-04 05:57:00.000' AS DateTime), 0, 1)



INSERT INTO [dbo].[CardData] ([CardDataID], [DataMovementHistoryID], [LoginName], [ShiftID], [CardTime], [IsInOrOut], [IsProcessed]) VALUES (11307, 2200, 'C2000', 8, CAST(N'2019-07-05 23:24:00.000' AS DateTime), 1, 1)



INSERT INTO [dbo].[CardData] ([CardDataID], [DataMovementHistoryID], [LoginName], [ShiftID], [CardTime], [IsInOrOut], [IsProcessed]) VALUES (11308, 2200, 'C2000', 8, CAST(N'2019-07-06 02:57:00.000' AS DateTime), 0, 1)



INSERT INTO [dbo].[CardData] ([CardDataID], [DataMovementHistoryID], [LoginName], [ShiftID], [CardTime], [IsInOrOut], [IsProcessed]) VALUES (11309, 2200, 'C2000', 8, CAST(N'2019-07-06 04:24:00.000' AS DateTime), 0, 1)



INSERT INTO [dbo].[CardData] ([CardDataID], [DataMovementHistoryID], [LoginName], [ShiftID], [CardTime], [IsInOrOut], [IsProcessed]) VALUES (11310, 2200, 'C2000', 8, CAST(N'2019-07-06 05:57:00.000' AS DateTime), 1, 1)



---@loginname='C2000'
---@startdate='2019-07-01'
---@Enddate='2019-07-15'



--Required output
loginname, DT, FIrstIN, InFLag, Lastout, Outflag

C2000, 2019-07-01, 2019-07-01 23:05:00, 0, 2019-07-01 5:05:00, 1

C2000, 2019-07-05, 2019-07-05 23:24:00, 1, 2019-07-05 05:57:00.000, 1









share



























    0















    I have small issue on night shit of our employees .. Shift timings are from 22:00 to 6:00 Am .. If the shift starts today at 11 Pm it will end the next morning at 6:00 Am (7 Hours ). Here I want to know the first Swipe in and First last Swipe out.. Below here am providing the table structure and some sample data along with the expected output ....I need a procedure with inputs like @loginname, @startdate, @Enddate



    CREATE TABLE [dbo].[CardData](
    [CardDataID] [bigint] NOT NULL,
    [DataMovementHistoryID] [bigint] NULL,
    [LoginName] varchar NULL,
    [ShiftID] [int] NULL,
    [CardTime] [datetime] NOT NULL,
    [IsInOrOut] [bit] NULL,
    [IsProcessed] [bit] NULL
    ) ON [PRIMARY]



    --TestCase 1--



    INSERT INTO [dbo].[CardData] ([CardDataID], [DataMovementHistoryID], [LoginName], [ShiftID], [CardTime], [IsInOrOut], [IsProcessed]) VALUES (11290, 2200, 'B1000', 1, CAST(N'2019-06-01 9:24:00.000' AS DateTime), 1, 1)



    INSERT INTO [dbo].[CardData] ([CardDataID], [DataMovementHistoryID], [LoginName], [ShiftID], [CardTime], [IsInOrOut], [IsProcessed]) VALUES (11291, 2200, 'B1000', 1, CAST(N'2019-06-01 12:55:00.000' AS DateTime), 0, 1)



    INSERT INTO [dbo].[CardData] ([CardDataID], [DataMovementHistoryID], [LoginName], [ShiftID], [CardTime], [IsInOrOut], [IsProcessed]) VALUES (11292, 2200, 'B1000', 1, CAST(N'2019-06-01 15:24:00.000' AS DateTime), 1, 1)



    INSERT INTO [dbo].[CardData] ([CardDataID], [DataMovementHistoryID], [LoginName], [ShiftID], [CardTime], [IsInOrOut], [IsProcessed]) VALUES (11293, 2200, 'B1000', 1, CAST(N'2019-06-01 17:55:00.000' AS DateTime), 0, 1)



    INSERT INTO [dbo].[CardData] ([CardDataID], [DataMovementHistoryID], [LoginName], [ShiftID], [CardTime], [IsInOrOut], [IsProcessed]) VALUES (11294, 2200, 'B1000', 1, CAST(N'2019-06-02 10:24:00.000' AS DateTime), 0, 1)



    INSERT INTO [dbo].[CardData] ([CardDataID], [DataMovementHistoryID], [LoginName], [ShiftID], [CardTime], [IsInOrOut], [IsProcessed]) VALUES (11295, 2200, 'B1000', 1, CAST(N'2019-06-02 13:55:00.000' AS DateTime), 1, 1)



    INSERT INTO [dbo].[CardData] ([CardDataID], [DataMovementHistoryID], [LoginName], [ShiftID], [CardTime], [IsInOrOut], [IsProcessed]) VALUES (11296, 2200, 'B1000', 1, CAST(N'2019-06-02 14:24:00.000' AS DateTime), 0, 1)



    INSERT INTO [dbo].[CardData] ([CardDataID], [DataMovementHistoryID], [LoginName], [ShiftID], [CardTime], [IsInOrOut], [IsProcessed]) VALUES (11297, 2200, 'B1000', 1, CAST(N'2019-06-02 16:01:00.000' AS DateTime), 0, 1)



    INSERT INTO [dbo].[CardData] ([CardDataID], [DataMovementHistoryID], [LoginName], [ShiftID], [CardTime], [IsInOrOut], [IsProcessed]) VALUES (11295, 2200, 'B1000', 1, CAST(N'2019-06-03 08:24:00.000' AS DateTime), 1, 1)



    INSERT INTO [dbo].[CardData] ([CardDataID], [DataMovementHistoryID], [LoginName], [ShiftID], [CardTime], [IsInOrOut], [IsProcessed]) VALUES (11296, 2200, 'B1000', 1, CAST(N'2019-06-03 13:55:00.000' AS DateTime), 0, 1)



    INSERT INTO [dbo].[CardData] ([CardDataID], [DataMovementHistoryID], [LoginName], [ShiftID], [CardTime], [IsInOrOut], [IsProcessed]) VALUES (11297, 2200, 'B1000', 1, CAST(N'2019-06-03 15:24:00.000' AS DateTime), 0, 1)



    INSERT INTO [dbo].[CardData] ([CardDataID], [DataMovementHistoryID], [LoginName], [ShiftID], [CardTime], [IsInOrOut], [IsProcessed]) VALUES (11298, 2200, 'B1000', 1, CAST(N'2019-06-03 17:01:00.000' AS DateTime), 1, 1)



    ---@loginname='B1000'
    ---@startdate='2019-06-01'
    ---@Enddate='2019-06-15'



    loginname , DT , FirstIn , InFlag , LastOut , OutFlag

    B1000 , 2019-06-02 ,2019-06-02 10:24:00, 0 , 2019-06-02 16:01:00, 0

    B1000 , 2019-06-03, 2019-06-03 8:24:00 ,1 , 2019-06-03 17:01:00 ,1



    INSERT INTO [dbo].[CardData] ([CardDataID], [DataMovementHistoryID], [LoginName], [ShiftID], [CardTime], [IsInOrOut], [IsProcessed]) VALUES (11299, 2200, 'C2000', 8, CAST(N'2019-07-01 23:05:00.000' AS DateTime), 0, 1)



    INSERT INTO [dbo].[CardData] ([CardDataID], [DataMovementHistoryID], [LoginName], [ShiftID], [CardTime], [IsInOrOut], [IsProcessed]) VALUES (11300, 2200, 'C2000', 8, CAST(N'2019-07-02 00:55:00.000' AS DateTime), 0, 1)



    INSERT INTO [dbo].[CardData] ([CardDataID], [DataMovementHistoryID], [LoginName], [ShiftID], [CardTime], [IsInOrOut], [IsProcessed]) VALUES (11301, 2200, 'C2000', 8, CAST(N'2019-07-02 3:24:00.000' AS DateTime), 1, 1)



    INSERT INTO [dbo].[CardData] ([CardDataID], [DataMovementHistoryID], [LoginName], [ShiftID], [CardTime], [IsInOrOut], [IsProcessed]) VALUES (11302, 2200, 'C2000', 8, CAST(N'2019-07-02 5:05:00.000' AS DateTime), 1, 1)



    INSERT INTO [dbo].[CardData] ([CardDataID], [DataMovementHistoryID], [LoginName], [ShiftID], [CardTime], [IsInOrOut], [IsProcessed]) VALUES (11303, 2200, 'C2000', 8, CAST(N'2019-07-03 23:24:00.000' AS DateTime), 1, 1)



    INSERT INTO [dbo].[CardData] ([CardDataID], [DataMovementHistoryID], [LoginName], [ShiftID], [CardTime], [IsInOrOut], [IsProcessed]) VALUES (11304, 2200, 'C2000', 8, CAST(N'2019-07-04 02:55:00.000' AS DateTime), 0, 1)



    INSERT INTO [dbo].[CardData] ([CardDataID], [DataMovementHistoryID], [LoginName], [ShiftID], [CardTime], [IsInOrOut], [IsProcessed]) VALUES (11305, 2200, 'C2000', 8, CAST(N'2019-07-04 4:24:00.000' AS DateTime), 1, 1)



    INSERT INTO [dbo].[CardData] ([CardDataID], [DataMovementHistoryID], [LoginName], [ShiftID], [CardTime], [IsInOrOut], [IsProcessed]) VALUES (11306, 2200, 'C2000', 8, CAST(N'2019-07-04 05:57:00.000' AS DateTime), 0, 1)



    INSERT INTO [dbo].[CardData] ([CardDataID], [DataMovementHistoryID], [LoginName], [ShiftID], [CardTime], [IsInOrOut], [IsProcessed]) VALUES (11307, 2200, 'C2000', 8, CAST(N'2019-07-05 23:24:00.000' AS DateTime), 1, 1)



    INSERT INTO [dbo].[CardData] ([CardDataID], [DataMovementHistoryID], [LoginName], [ShiftID], [CardTime], [IsInOrOut], [IsProcessed]) VALUES (11308, 2200, 'C2000', 8, CAST(N'2019-07-06 02:57:00.000' AS DateTime), 0, 1)



    INSERT INTO [dbo].[CardData] ([CardDataID], [DataMovementHistoryID], [LoginName], [ShiftID], [CardTime], [IsInOrOut], [IsProcessed]) VALUES (11309, 2200, 'C2000', 8, CAST(N'2019-07-06 04:24:00.000' AS DateTime), 0, 1)



    INSERT INTO [dbo].[CardData] ([CardDataID], [DataMovementHistoryID], [LoginName], [ShiftID], [CardTime], [IsInOrOut], [IsProcessed]) VALUES (11310, 2200, 'C2000', 8, CAST(N'2019-07-06 05:57:00.000' AS DateTime), 1, 1)



    ---@loginname='C2000'
    ---@startdate='2019-07-01'
    ---@Enddate='2019-07-15'



    --Required output
    loginname, DT, FIrstIN, InFLag, Lastout, Outflag

    C2000, 2019-07-01, 2019-07-01 23:05:00, 0, 2019-07-01 5:05:00, 1

    C2000, 2019-07-05, 2019-07-05 23:24:00, 1, 2019-07-05 05:57:00.000, 1









    share

























      0












      0








      0








      I have small issue on night shit of our employees .. Shift timings are from 22:00 to 6:00 Am .. If the shift starts today at 11 Pm it will end the next morning at 6:00 Am (7 Hours ). Here I want to know the first Swipe in and First last Swipe out.. Below here am providing the table structure and some sample data along with the expected output ....I need a procedure with inputs like @loginname, @startdate, @Enddate



      CREATE TABLE [dbo].[CardData](
      [CardDataID] [bigint] NOT NULL,
      [DataMovementHistoryID] [bigint] NULL,
      [LoginName] varchar NULL,
      [ShiftID] [int] NULL,
      [CardTime] [datetime] NOT NULL,
      [IsInOrOut] [bit] NULL,
      [IsProcessed] [bit] NULL
      ) ON [PRIMARY]



      --TestCase 1--



      INSERT INTO [dbo].[CardData] ([CardDataID], [DataMovementHistoryID], [LoginName], [ShiftID], [CardTime], [IsInOrOut], [IsProcessed]) VALUES (11290, 2200, 'B1000', 1, CAST(N'2019-06-01 9:24:00.000' AS DateTime), 1, 1)



      INSERT INTO [dbo].[CardData] ([CardDataID], [DataMovementHistoryID], [LoginName], [ShiftID], [CardTime], [IsInOrOut], [IsProcessed]) VALUES (11291, 2200, 'B1000', 1, CAST(N'2019-06-01 12:55:00.000' AS DateTime), 0, 1)



      INSERT INTO [dbo].[CardData] ([CardDataID], [DataMovementHistoryID], [LoginName], [ShiftID], [CardTime], [IsInOrOut], [IsProcessed]) VALUES (11292, 2200, 'B1000', 1, CAST(N'2019-06-01 15:24:00.000' AS DateTime), 1, 1)



      INSERT INTO [dbo].[CardData] ([CardDataID], [DataMovementHistoryID], [LoginName], [ShiftID], [CardTime], [IsInOrOut], [IsProcessed]) VALUES (11293, 2200, 'B1000', 1, CAST(N'2019-06-01 17:55:00.000' AS DateTime), 0, 1)



      INSERT INTO [dbo].[CardData] ([CardDataID], [DataMovementHistoryID], [LoginName], [ShiftID], [CardTime], [IsInOrOut], [IsProcessed]) VALUES (11294, 2200, 'B1000', 1, CAST(N'2019-06-02 10:24:00.000' AS DateTime), 0, 1)



      INSERT INTO [dbo].[CardData] ([CardDataID], [DataMovementHistoryID], [LoginName], [ShiftID], [CardTime], [IsInOrOut], [IsProcessed]) VALUES (11295, 2200, 'B1000', 1, CAST(N'2019-06-02 13:55:00.000' AS DateTime), 1, 1)



      INSERT INTO [dbo].[CardData] ([CardDataID], [DataMovementHistoryID], [LoginName], [ShiftID], [CardTime], [IsInOrOut], [IsProcessed]) VALUES (11296, 2200, 'B1000', 1, CAST(N'2019-06-02 14:24:00.000' AS DateTime), 0, 1)



      INSERT INTO [dbo].[CardData] ([CardDataID], [DataMovementHistoryID], [LoginName], [ShiftID], [CardTime], [IsInOrOut], [IsProcessed]) VALUES (11297, 2200, 'B1000', 1, CAST(N'2019-06-02 16:01:00.000' AS DateTime), 0, 1)



      INSERT INTO [dbo].[CardData] ([CardDataID], [DataMovementHistoryID], [LoginName], [ShiftID], [CardTime], [IsInOrOut], [IsProcessed]) VALUES (11295, 2200, 'B1000', 1, CAST(N'2019-06-03 08:24:00.000' AS DateTime), 1, 1)



      INSERT INTO [dbo].[CardData] ([CardDataID], [DataMovementHistoryID], [LoginName], [ShiftID], [CardTime], [IsInOrOut], [IsProcessed]) VALUES (11296, 2200, 'B1000', 1, CAST(N'2019-06-03 13:55:00.000' AS DateTime), 0, 1)



      INSERT INTO [dbo].[CardData] ([CardDataID], [DataMovementHistoryID], [LoginName], [ShiftID], [CardTime], [IsInOrOut], [IsProcessed]) VALUES (11297, 2200, 'B1000', 1, CAST(N'2019-06-03 15:24:00.000' AS DateTime), 0, 1)



      INSERT INTO [dbo].[CardData] ([CardDataID], [DataMovementHistoryID], [LoginName], [ShiftID], [CardTime], [IsInOrOut], [IsProcessed]) VALUES (11298, 2200, 'B1000', 1, CAST(N'2019-06-03 17:01:00.000' AS DateTime), 1, 1)



      ---@loginname='B1000'
      ---@startdate='2019-06-01'
      ---@Enddate='2019-06-15'



      loginname , DT , FirstIn , InFlag , LastOut , OutFlag

      B1000 , 2019-06-02 ,2019-06-02 10:24:00, 0 , 2019-06-02 16:01:00, 0

      B1000 , 2019-06-03, 2019-06-03 8:24:00 ,1 , 2019-06-03 17:01:00 ,1



      INSERT INTO [dbo].[CardData] ([CardDataID], [DataMovementHistoryID], [LoginName], [ShiftID], [CardTime], [IsInOrOut], [IsProcessed]) VALUES (11299, 2200, 'C2000', 8, CAST(N'2019-07-01 23:05:00.000' AS DateTime), 0, 1)



      INSERT INTO [dbo].[CardData] ([CardDataID], [DataMovementHistoryID], [LoginName], [ShiftID], [CardTime], [IsInOrOut], [IsProcessed]) VALUES (11300, 2200, 'C2000', 8, CAST(N'2019-07-02 00:55:00.000' AS DateTime), 0, 1)



      INSERT INTO [dbo].[CardData] ([CardDataID], [DataMovementHistoryID], [LoginName], [ShiftID], [CardTime], [IsInOrOut], [IsProcessed]) VALUES (11301, 2200, 'C2000', 8, CAST(N'2019-07-02 3:24:00.000' AS DateTime), 1, 1)



      INSERT INTO [dbo].[CardData] ([CardDataID], [DataMovementHistoryID], [LoginName], [ShiftID], [CardTime], [IsInOrOut], [IsProcessed]) VALUES (11302, 2200, 'C2000', 8, CAST(N'2019-07-02 5:05:00.000' AS DateTime), 1, 1)



      INSERT INTO [dbo].[CardData] ([CardDataID], [DataMovementHistoryID], [LoginName], [ShiftID], [CardTime], [IsInOrOut], [IsProcessed]) VALUES (11303, 2200, 'C2000', 8, CAST(N'2019-07-03 23:24:00.000' AS DateTime), 1, 1)



      INSERT INTO [dbo].[CardData] ([CardDataID], [DataMovementHistoryID], [LoginName], [ShiftID], [CardTime], [IsInOrOut], [IsProcessed]) VALUES (11304, 2200, 'C2000', 8, CAST(N'2019-07-04 02:55:00.000' AS DateTime), 0, 1)



      INSERT INTO [dbo].[CardData] ([CardDataID], [DataMovementHistoryID], [LoginName], [ShiftID], [CardTime], [IsInOrOut], [IsProcessed]) VALUES (11305, 2200, 'C2000', 8, CAST(N'2019-07-04 4:24:00.000' AS DateTime), 1, 1)



      INSERT INTO [dbo].[CardData] ([CardDataID], [DataMovementHistoryID], [LoginName], [ShiftID], [CardTime], [IsInOrOut], [IsProcessed]) VALUES (11306, 2200, 'C2000', 8, CAST(N'2019-07-04 05:57:00.000' AS DateTime), 0, 1)



      INSERT INTO [dbo].[CardData] ([CardDataID], [DataMovementHistoryID], [LoginName], [ShiftID], [CardTime], [IsInOrOut], [IsProcessed]) VALUES (11307, 2200, 'C2000', 8, CAST(N'2019-07-05 23:24:00.000' AS DateTime), 1, 1)



      INSERT INTO [dbo].[CardData] ([CardDataID], [DataMovementHistoryID], [LoginName], [ShiftID], [CardTime], [IsInOrOut], [IsProcessed]) VALUES (11308, 2200, 'C2000', 8, CAST(N'2019-07-06 02:57:00.000' AS DateTime), 0, 1)



      INSERT INTO [dbo].[CardData] ([CardDataID], [DataMovementHistoryID], [LoginName], [ShiftID], [CardTime], [IsInOrOut], [IsProcessed]) VALUES (11309, 2200, 'C2000', 8, CAST(N'2019-07-06 04:24:00.000' AS DateTime), 0, 1)



      INSERT INTO [dbo].[CardData] ([CardDataID], [DataMovementHistoryID], [LoginName], [ShiftID], [CardTime], [IsInOrOut], [IsProcessed]) VALUES (11310, 2200, 'C2000', 8, CAST(N'2019-07-06 05:57:00.000' AS DateTime), 1, 1)



      ---@loginname='C2000'
      ---@startdate='2019-07-01'
      ---@Enddate='2019-07-15'



      --Required output
      loginname, DT, FIrstIN, InFLag, Lastout, Outflag

      C2000, 2019-07-01, 2019-07-01 23:05:00, 0, 2019-07-01 5:05:00, 1

      C2000, 2019-07-05, 2019-07-05 23:24:00, 1, 2019-07-05 05:57:00.000, 1









      share














      I have small issue on night shit of our employees .. Shift timings are from 22:00 to 6:00 Am .. If the shift starts today at 11 Pm it will end the next morning at 6:00 Am (7 Hours ). Here I want to know the first Swipe in and First last Swipe out.. Below here am providing the table structure and some sample data along with the expected output ....I need a procedure with inputs like @loginname, @startdate, @Enddate



      CREATE TABLE [dbo].[CardData](
      [CardDataID] [bigint] NOT NULL,
      [DataMovementHistoryID] [bigint] NULL,
      [LoginName] varchar NULL,
      [ShiftID] [int] NULL,
      [CardTime] [datetime] NOT NULL,
      [IsInOrOut] [bit] NULL,
      [IsProcessed] [bit] NULL
      ) ON [PRIMARY]



      --TestCase 1--



      INSERT INTO [dbo].[CardData] ([CardDataID], [DataMovementHistoryID], [LoginName], [ShiftID], [CardTime], [IsInOrOut], [IsProcessed]) VALUES (11290, 2200, 'B1000', 1, CAST(N'2019-06-01 9:24:00.000' AS DateTime), 1, 1)



      INSERT INTO [dbo].[CardData] ([CardDataID], [DataMovementHistoryID], [LoginName], [ShiftID], [CardTime], [IsInOrOut], [IsProcessed]) VALUES (11291, 2200, 'B1000', 1, CAST(N'2019-06-01 12:55:00.000' AS DateTime), 0, 1)



      INSERT INTO [dbo].[CardData] ([CardDataID], [DataMovementHistoryID], [LoginName], [ShiftID], [CardTime], [IsInOrOut], [IsProcessed]) VALUES (11292, 2200, 'B1000', 1, CAST(N'2019-06-01 15:24:00.000' AS DateTime), 1, 1)



      INSERT INTO [dbo].[CardData] ([CardDataID], [DataMovementHistoryID], [LoginName], [ShiftID], [CardTime], [IsInOrOut], [IsProcessed]) VALUES (11293, 2200, 'B1000', 1, CAST(N'2019-06-01 17:55:00.000' AS DateTime), 0, 1)



      INSERT INTO [dbo].[CardData] ([CardDataID], [DataMovementHistoryID], [LoginName], [ShiftID], [CardTime], [IsInOrOut], [IsProcessed]) VALUES (11294, 2200, 'B1000', 1, CAST(N'2019-06-02 10:24:00.000' AS DateTime), 0, 1)



      INSERT INTO [dbo].[CardData] ([CardDataID], [DataMovementHistoryID], [LoginName], [ShiftID], [CardTime], [IsInOrOut], [IsProcessed]) VALUES (11295, 2200, 'B1000', 1, CAST(N'2019-06-02 13:55:00.000' AS DateTime), 1, 1)



      INSERT INTO [dbo].[CardData] ([CardDataID], [DataMovementHistoryID], [LoginName], [ShiftID], [CardTime], [IsInOrOut], [IsProcessed]) VALUES (11296, 2200, 'B1000', 1, CAST(N'2019-06-02 14:24:00.000' AS DateTime), 0, 1)



      INSERT INTO [dbo].[CardData] ([CardDataID], [DataMovementHistoryID], [LoginName], [ShiftID], [CardTime], [IsInOrOut], [IsProcessed]) VALUES (11297, 2200, 'B1000', 1, CAST(N'2019-06-02 16:01:00.000' AS DateTime), 0, 1)



      INSERT INTO [dbo].[CardData] ([CardDataID], [DataMovementHistoryID], [LoginName], [ShiftID], [CardTime], [IsInOrOut], [IsProcessed]) VALUES (11295, 2200, 'B1000', 1, CAST(N'2019-06-03 08:24:00.000' AS DateTime), 1, 1)



      INSERT INTO [dbo].[CardData] ([CardDataID], [DataMovementHistoryID], [LoginName], [ShiftID], [CardTime], [IsInOrOut], [IsProcessed]) VALUES (11296, 2200, 'B1000', 1, CAST(N'2019-06-03 13:55:00.000' AS DateTime), 0, 1)



      INSERT INTO [dbo].[CardData] ([CardDataID], [DataMovementHistoryID], [LoginName], [ShiftID], [CardTime], [IsInOrOut], [IsProcessed]) VALUES (11297, 2200, 'B1000', 1, CAST(N'2019-06-03 15:24:00.000' AS DateTime), 0, 1)



      INSERT INTO [dbo].[CardData] ([CardDataID], [DataMovementHistoryID], [LoginName], [ShiftID], [CardTime], [IsInOrOut], [IsProcessed]) VALUES (11298, 2200, 'B1000', 1, CAST(N'2019-06-03 17:01:00.000' AS DateTime), 1, 1)



      ---@loginname='B1000'
      ---@startdate='2019-06-01'
      ---@Enddate='2019-06-15'



      loginname , DT , FirstIn , InFlag , LastOut , OutFlag

      B1000 , 2019-06-02 ,2019-06-02 10:24:00, 0 , 2019-06-02 16:01:00, 0

      B1000 , 2019-06-03, 2019-06-03 8:24:00 ,1 , 2019-06-03 17:01:00 ,1



      INSERT INTO [dbo].[CardData] ([CardDataID], [DataMovementHistoryID], [LoginName], [ShiftID], [CardTime], [IsInOrOut], [IsProcessed]) VALUES (11299, 2200, 'C2000', 8, CAST(N'2019-07-01 23:05:00.000' AS DateTime), 0, 1)



      INSERT INTO [dbo].[CardData] ([CardDataID], [DataMovementHistoryID], [LoginName], [ShiftID], [CardTime], [IsInOrOut], [IsProcessed]) VALUES (11300, 2200, 'C2000', 8, CAST(N'2019-07-02 00:55:00.000' AS DateTime), 0, 1)



      INSERT INTO [dbo].[CardData] ([CardDataID], [DataMovementHistoryID], [LoginName], [ShiftID], [CardTime], [IsInOrOut], [IsProcessed]) VALUES (11301, 2200, 'C2000', 8, CAST(N'2019-07-02 3:24:00.000' AS DateTime), 1, 1)



      INSERT INTO [dbo].[CardData] ([CardDataID], [DataMovementHistoryID], [LoginName], [ShiftID], [CardTime], [IsInOrOut], [IsProcessed]) VALUES (11302, 2200, 'C2000', 8, CAST(N'2019-07-02 5:05:00.000' AS DateTime), 1, 1)



      INSERT INTO [dbo].[CardData] ([CardDataID], [DataMovementHistoryID], [LoginName], [ShiftID], [CardTime], [IsInOrOut], [IsProcessed]) VALUES (11303, 2200, 'C2000', 8, CAST(N'2019-07-03 23:24:00.000' AS DateTime), 1, 1)



      INSERT INTO [dbo].[CardData] ([CardDataID], [DataMovementHistoryID], [LoginName], [ShiftID], [CardTime], [IsInOrOut], [IsProcessed]) VALUES (11304, 2200, 'C2000', 8, CAST(N'2019-07-04 02:55:00.000' AS DateTime), 0, 1)



      INSERT INTO [dbo].[CardData] ([CardDataID], [DataMovementHistoryID], [LoginName], [ShiftID], [CardTime], [IsInOrOut], [IsProcessed]) VALUES (11305, 2200, 'C2000', 8, CAST(N'2019-07-04 4:24:00.000' AS DateTime), 1, 1)



      INSERT INTO [dbo].[CardData] ([CardDataID], [DataMovementHistoryID], [LoginName], [ShiftID], [CardTime], [IsInOrOut], [IsProcessed]) VALUES (11306, 2200, 'C2000', 8, CAST(N'2019-07-04 05:57:00.000' AS DateTime), 0, 1)



      INSERT INTO [dbo].[CardData] ([CardDataID], [DataMovementHistoryID], [LoginName], [ShiftID], [CardTime], [IsInOrOut], [IsProcessed]) VALUES (11307, 2200, 'C2000', 8, CAST(N'2019-07-05 23:24:00.000' AS DateTime), 1, 1)



      INSERT INTO [dbo].[CardData] ([CardDataID], [DataMovementHistoryID], [LoginName], [ShiftID], [CardTime], [IsInOrOut], [IsProcessed]) VALUES (11308, 2200, 'C2000', 8, CAST(N'2019-07-06 02:57:00.000' AS DateTime), 0, 1)



      INSERT INTO [dbo].[CardData] ([CardDataID], [DataMovementHistoryID], [LoginName], [ShiftID], [CardTime], [IsInOrOut], [IsProcessed]) VALUES (11309, 2200, 'C2000', 8, CAST(N'2019-07-06 04:24:00.000' AS DateTime), 0, 1)



      INSERT INTO [dbo].[CardData] ([CardDataID], [DataMovementHistoryID], [LoginName], [ShiftID], [CardTime], [IsInOrOut], [IsProcessed]) VALUES (11310, 2200, 'C2000', 8, CAST(N'2019-07-06 05:57:00.000' AS DateTime), 1, 1)



      ---@loginname='C2000'
      ---@startdate='2019-07-01'
      ---@Enddate='2019-07-15'



      --Required output
      loginname, DT, FIrstIN, InFLag, Lastout, Outflag

      C2000, 2019-07-01, 2019-07-01 23:05:00, 0, 2019-07-01 5:05:00, 1

      C2000, 2019-07-05, 2019-07-05 23:24:00, 1, 2019-07-05 05:57:00.000, 1







      sql-server sql-server-2012 stored-procedures





      share












      share










      share



      share










      asked 21 mins ago









      AdityaAditya

      1




      1






















          0






          active

          oldest

          votes












          Your Answer








          StackExchange.ready(function() {
          var channelOptions = {
          tags: "".split(" "),
          id: "182"
          };
          initTagRenderer("".split(" "), "".split(" "), channelOptions);

          StackExchange.using("externalEditor", function() {
          // Have to fire editor after snippets, if snippets enabled
          if (StackExchange.settings.snippets.snippetsEnabled) {
          StackExchange.using("snippets", function() {
          createEditor();
          });
          }
          else {
          createEditor();
          }
          });

          function createEditor() {
          StackExchange.prepareEditor({
          heartbeatType: 'answer',
          autoActivateHeartbeat: false,
          convertImagesToLinks: false,
          noModals: true,
          showLowRepImageUploadWarning: true,
          reputationToPostImages: null,
          bindNavPrevention: true,
          postfix: "",
          imageUploader: {
          brandingHtml: "Powered by u003ca class="icon-imgur-white" href="https://imgur.com/"u003eu003c/au003e",
          contentPolicyHtml: "User contributions licensed under u003ca href="https://creativecommons.org/licenses/by-sa/3.0/"u003ecc by-sa 3.0 with attribution requiredu003c/au003e u003ca href="https://stackoverflow.com/legal/content-policy"u003e(content policy)u003c/au003e",
          allowUrls: true
          },
          onDemand: true,
          discardSelector: ".discard-answer"
          ,immediatelyShowMarkdownHelp:true
          });


          }
          });














          draft saved

          draft discarded


















          StackExchange.ready(
          function () {
          StackExchange.openid.initPostLogin('.new-post-login', 'https%3a%2f%2fdba.stackexchange.com%2fquestions%2f233905%2fregarding-night-shift-employees%23new-answer', 'question_page');
          }
          );

          Post as a guest















          Required, but never shown

























          0






          active

          oldest

          votes








          0






          active

          oldest

          votes









          active

          oldest

          votes






          active

          oldest

          votes
















          draft saved

          draft discarded




















































          Thanks for contributing an answer to Database Administrators Stack Exchange!


          • Please be sure to answer the question. Provide details and share your research!

          But avoid



          • Asking for help, clarification, or responding to other answers.

          • Making statements based on opinion; back them up with references or personal experience.


          To learn more, see our tips on writing great answers.




          draft saved


          draft discarded














          StackExchange.ready(
          function () {
          StackExchange.openid.initPostLogin('.new-post-login', 'https%3a%2f%2fdba.stackexchange.com%2fquestions%2f233905%2fregarding-night-shift-employees%23new-answer', 'question_page');
          }
          );

          Post as a guest















          Required, but never shown





















































          Required, but never shown














          Required, but never shown












          Required, but never shown







          Required, but never shown

































          Required, but never shown














          Required, but never shown












          Required, but never shown







          Required, but never shown







          Popular posts from this blog

          الفوسفات في المغرب

          Four equal circles intersect: What is the area of the small shaded portion and its height

          جامعة ليفربول