I have the data below:
DECLARE @tbl TABLE (
ID VARCHAR(8)
,WeekDayName VARCHAR(15)
,StartDate VARCHAR(15)
,EndDate VARCHAR(15)
,A_Type VARCHAR(3)
,A_Days VARCHAR(10)
,A_Hours VARCHAR(10)
)
INSERT INTO @tbl (ID, WeekDayName, StartDate, EndDate, A_Type, A_Days, A_Hours)
VALUES
('150017', 'Monday', '2019-12-23', '2019-12-23', '430', 1.00, 8.20)
,('150017', 'Tuesday', '2019-12-24', '2019-12-24', '430', 1.00, 4.10)
,('150017', 'Friday', '2019-12-27', '2019-12-27', '430', 1.00, 8.20)
,('150017', 'Monday', '2019-12-30', '2019-12-30', '430', 1.00, 8.20)
,('150017', 'Tuesday', '2019-12-31', '2019-12-31', '430', 1.00, 4.10)
,('150035', 'Tuesday', '2019-03-12', '2019-03-12', '430', 0.66, 5.45)
,('150041', 'Thursday', '2019-01-17', '2019-01-17', '430', 1.00, 8.20)
,('150041', 'Tuesday', '2019-08-20', '2019-08-20', '430', 1.00, 8.20)
,('150041', 'Friday', '2019-08-21', '2019-08-21', '430', 1.00, 8.20)
,('150045', 'Monday', '2019-05-13', '2019-05-13', '430', 1.00, 8.20)
,('150045', 'Tuesday', '2019-05-14', '2019-05-14', '430', 1.00, 8.20)
,('150045', 'Wednesday', '2019-05-15', '2019-05-15', '430', 1.00, 8.20)
,('150045', 'Monday', '2019-11-25', '2019-11-25', '430', 1.00, 8.20)
,('150045', 'Tuesday', '2019-11-26', '2019-11-26', '430', 1.00, 8.20)
,('150045', 'Wednesday', '2019-11-27', '2019-11-27', '430', 1.00, 8.20)
,('150045', 'Thursday', '2019-11-28', '2019-11-28', '430', 1.00, 8.20)
,('150045', 'Friday', '2019-11-29', '2019-11-29', '430', 1.00, 8.20)
,('150046', 'Monday', '2019-03-11', '2019-03-11', '430', 1.00, 8.20)
,('150048', 'Tuesday', '2019-10-08', '2019-10-08', '430', 0.30, 2.50)
,('150048', 'Monday', '2019-10-28', '2019-10-28', '430', 1.00, 8.20)
The StartDate and EndDate are always identical and they are only working days (Monday to Friday). The WeekDayName is the name of the StartDate. ID is an integer. A_Type is (here) always 430. A_Days (always lower than or equal to 1), A_Hours (always lower than or equal to 8.20).
The desired output is for each ID a StartDate, EndDate, Sum of A_Days, Sum of A_Hours, SumDays. SumDays is the number of working days between StartDate and EndDate. EndDate is the day that which is the last date of a "block" of following dates, grouped by the ID.
E.g.
ID StartDate EndDate A_Days A_Hours SumDays
150017 2019-12-23 2019-12-31 5.00 32.80 5
150035 2019-03-12 2019-03-12 0.66 5.45 1
150041 2019-01-17 2019-01-17 1.00 8.20 1
150041 2019-08-20 2019-08-21 2.00 16.40 2
...
150041 has two records, since there are two "blocks" of following dates. The first, 2019-01-17, and the seconds from 2019-08-20 to 2019-08-21.
Could someone please help me with that? (As a bonus, it would be great if it is possible to be able to group by the column A_Type as well). I could not get it working.
Any help is appreciated.
Thank you in advance,
Best
SumDays
? – Suraj KumarSumDays
is the number of working days betweenStartDate
andEndDate
. If it is the same day,SumDays
shall be 1. – Json150017
, that has rows on Monday, Tuesday, Friday, Monday, Tuesday? – GMB