1

I am relatively new to SQL and have written a bunch of various views. I have been asked to created a report for our monthly payroll using clockings from SQL server.

Currently SQL formats things:

Date | Name | Clock In | Clock Out
---
01/02/2018 | Name | 2018-02-01 07:55:08.563 | 2018-02-01 10:21:42.183

01/02/2018 | Name | 2018-02-01 10:32:37.153 | 2018-02-01 13:12:33.773

01/02/2018 | Name | 2018-02-01 13:33:10.123 | 2018-02-01 15:04:16.880

However I would like it something like this:

Date | Name | Clock In | Clock Out | Clock In | Clock Out | Clock In | Clock Out
--
01/02/2018 | Name | 2018-02-01 07:55:08.563 | 2018-02-01 10:21:42.183 | 2018-02-01 10:32:37.153 | 2018-02-01 13:12:33.773 | 2018-02-01 13:33:10.123 | 2018-02-01 15:04:16.880

I have things currently working in excel using the formula:

{=INDEX(Payroll_Clockings[Start Time], SMALL(IF(1 = ((--($A9=Payroll_Clockings[Date Value])) * (--($J$4=Payroll_Clockings[Name]))), ROW(Payroll_Clockings[Start Time]) -1, ""), COLUMN() -1))}

But being an Array formula this can take a long time to refresh when there are 50+ records.

so it would be ideal to reaarange things through SQL.

the SQL view i am using is:

SELECT        TOP (100) PERCENT CONVERT(VARCHAR(19), ShopFloor.EmployeeActivity.StartEvent, 103) AS Date, Admin.Employee.FirstName + ' ' + Admin.Employee.Surname AS Name, ShopFloor.EmployeeActivity.StartEvent AS [Start Time],
                          ShopFloor.EmployeeActivity.FinishEvent AS [Finish Time], ShopFloor.EmployeeActivity.ClockNumber AS [Clock No.]
FROM            ShopFloor.EmployeeActivity LEFT OUTER JOIN
                         Admin.Employee ON ShopFloor.EmployeeActivity.ClockNumber = Admin.Employee.ClockNumber
WHERE        (ShopFloor.EmployeeActivity.StartEvent BETWEEN GETDATE() - 180 AND GETDATE()) AND (NOT (ShopFloor.EmployeeActivity.ClockNumber IN (100, 10090, 10000, 777, 999, 10001, 10098, 10002))) AND 
                         (ShopFloor.EmployeeActivity.ActivityTypeCode = 1)
ORDER BY Date, Name, [Start Time]

Hopefully this makes sense and any help would be appreciated!


I don't normally create tables, only deal with creating views from current tables but I think this would be an example of our table.

IF EXISTS(SELECT 1 FROM sys.tables WHERE object_id = OBJECT_ID('myTable'))
BEGIN;
    DROP TABLE [myTable];
END;
GO

CREATE TABLE [myTable] (
    [myTableID] INTEGER NOT NULL IDENTITY(1, 1),
    [Date] VARCHAR(255),
    [Name] VARCHAR(255) NULL,
    [StartEvent] VARCHAR(255),
    [FinishEvent] VARCHAR(255),
    [ClockNumber] VARCHAR(255) NULL,
    PRIMARY KEY ([myTableID])
);
GO

INSERT INTO myTable([Date],[Name],[StartEvent],[FinishEvent],[ClockNumber]) 
VALUES('03/03/2019','Abdul Pugh','2018-09-12 10:50:36','2017-09-11 
05:21:44','CN-102');
INSERT INTO myTable([Date],[Name],[StartEvent],[FinishEvent],[ClockNumber]) 
VALUES('08/23/2017','Kiayada Knight','2018-09-18 17:22:47','2017-06-15 
05:03:34','CN-101');
1

0