Get Flag values based on conditions in sql server - sql-server

I have a question about SQL Server.
Please tell me how to Flag values using time calculation in SQL Server based on conditions.
if out and in time difference is more than 5 hours then flag is 1 else 0
Table : employee
CREATE TABLE [dbo].[employee](
[Emp Id] [float] NULL,
[Area Of Access] [nvarchar](255) NULL,
[Time] [datetime] NULL,
[floor] [nvarchar](255) NULL
) ON [PRIMARY]
GO
INSERT [dbo].[employee] ([Emp Id], [Area Of Access], [Time], [floor]) VALUES (101, N'IN', CAST(N'2018-03-03T03:36:58.000' AS DateTime), N'Common Area')
GO
INSERT [dbo].[employee] ([Emp Id], [Area Of Access], [Time], [floor]) VALUES (101, N'OUT', CAST(N'2018-03-03T03:38:55.000' AS DateTime), N'Common Area')
GO
INSERT [dbo].[employee] ([Emp Id], [Area Of Access], [Time], [floor]) VALUES (101, N'OUT', CAST(N'2018-03-05T18:54:00.000' AS DateTime), N'Common Area')
GO
INSERT [dbo].[employee] ([Emp Id], [Area Of Access], [Time], [floor]) VALUES (101, N'IN', CAST(N'2018-03-05T18:54:13.000' AS DateTime), N'Common Area')
GO
INSERT [dbo].[employee] ([Emp Id], [Area Of Access], [Time], [floor]) VALUES (101, N'IN', CAST(N'2018-03-05T18:54:32.000' AS DateTime), N'Common Area')
GO
INSERT [dbo].[employee] ([Emp Id], [Area Of Access], [Time], [floor]) VALUES (101, N'IN', CAST(N'2018-03-05T18:55:01.000' AS DateTime), N'Production Floor')
GO
INSERT [dbo].[employee] ([Emp Id], [Area Of Access], [Time], [floor]) VALUES (101, N'OUT', CAST(N'2018-03-05T19:58:41.000' AS DateTime), N'Production Floor')
GO
INSERT [dbo].[employee] ([Emp Id], [Area Of Access], [Time], [floor]) VALUES (100, N'IN', CAST(N'2018-02-06T16:03:08.000' AS DateTime), N'Production Floor')
GO
INSERT [dbo].[employee] ([Emp Id], [Area Of Access], [Time], [floor]) VALUES (100, N'OUT', CAST(N'2018-02-06T22:01:40.000' AS DateTime), N'Production Floor')
GO
INSERT [dbo].[employee] ([Emp Id], [Area Of Access], [Time], [floor]) VALUES (100, N'IN', CAST(N'2018-02-06T22:42:15.000' AS DateTime), N'Production Floor')
GO
INSERT [dbo].[employee] ([Emp Id], [Area Of Access], [Time], [floor]) VALUES (100, N'OUT', CAST(N'2018-02-07T00:33:19.000' AS DateTime), N'Production Floor')
GO
INSERT [dbo].[employee] ([Emp Id], [Area Of Access], [Time], [floor]) VALUES (100, N'IN', CAST(N'2018-02-07T10:13:11.000' AS DateTime), N'Production Floor')
GO
INSERT [dbo].[employee] ([Emp Id], [Area Of Access], [Time], [floor]) VALUES (100, N'OUT', CAST(N'2018-02-07T15:13:10.000' AS DateTime), N'Production Floor')
GO
based on above table I want output like below :
EmpId |Area Of Access |Time |floor |Flag
100 |IN |2018-02-06 16:03:08.000 |Production Floor |0
100 |OUT |2018-02-06 22:01:40.000 |Production Floor |0
100 |IN |2018-02-06 22:42:15.000 |Production Floor |0
100 |OUT |2018-02-07 00:33:19.000 |Production Floor |0
100 |IN |2018-02-07 10:13:11.000 |Production Floor |1
100 |OUT |2018-02-07 15:13:10.000 |Production Floor |0
101 |IN |2018-03-03 03:36:58.000 |Common Area |0
101 |OUT |2018-03-03 03:38:55.000 |Common Area |0
101 |OUT |2018-03-05 18:54:00.000 |Common Area |1
101 |IN |2018-03-05 18:54:13.000 |Common Area |0
101 |IN |2018-03-05 18:54:32.000 |Common Area |0
101 |IN |2018-03-05 18:55:01.000 |Production Floor |0
101 |OUT |2018-03-05 19:58:41.000 |Production Floor |0
I tried like below :
select el.*,
case when (datediff(hh, lag(Time) over (partition by [Emp Id] order by Time), Time)) > 5
and el.[Area Of Access]='in' then 1
when (datediff(hh, lag(Time) over (partition by [Emp Id] order by Time), Time)) > 5
then 1 else 0 end Flag from employee el
above query is not given expected result . Please tell me how to wirte query to achieve this task in SQL Server

you can use lag function in case like that:
SELECT el.*,
CASE WHEN ((DATEDIFF(hh, LAG(Time) OVER (PARTITION BY [Emp Id] ORDER BY Time), Time)) > 5
AND LAG([Area Of Access]) OVER (PARTITION BY [Emp Id] ORDER BY Time) = 'out')
THEN 1
ELSE 0
END Flag
FROM employee el

Related

T-SQL - timespan by overlapping datetime columns

I want maximum period of date range that is overlapping each other and if the period is not clashing other date ranges then I want it as it is.
I have this table:
CREATE TABLE [dbo].[table1]
(
[id] [numeric](18, 0) IDENTITY(1,1) NOT NULL,
[StartDate] [datetime] NOT NULL,
[EndDate] [datetime] NOT NULL
)
And their respective values:
INSERT INTO [dbo].[table1]
VALUES (CAST('2013-11-01 00:00:00.000' AS DateTime), CAST('2013-11-10 00:00:00.000' AS DateTime)),
(CAST('2013-11-05 00:00:00.000' AS DateTime), CAST('2013-11-15 00:00:00.000' AS DateTime)),
(CAST('2013-11-10 00:00:00.000' AS DateTime), CAST('2013-11-15 00:00:00.000' AS DateTime)),
(CAST('2013-11-10 00:00:00.000' AS DateTime), CAST('2013-11-25 00:00:00.000' AS DateTime)),
(CAST('2013-11-26 00:00:00.000' AS DateTime), CAST('2013-11-29 00:00:00.000' AS DateTime))
And expected result is:
ID StartDate EndDate
--------------------------------------------------------
1 2013-11-01 00:00:00.000 2013-11-25 00:00:00.000
2 2013-11-26 00:00:00.000 2013-11-29 00:00:00.000
Thanks in advance.
// Edit 1: Thanks.
Works, but there is a new question for breaks in the same table
INSERT INTO [dbo].[table1]
VALUES (CAST('2018-05-03 08:30:00.000' AS DateTime), CAST('2018-05-03 08:45:00.000' AS DateTime)),
(CAST('2018-05-03 08:45:00.000' AS DateTime), CAST('2018-05-03 09:30:00.000' AS DateTime)),
(CAST('2018-05-03 08:45:00.000' AS DateTime), CAST('2018-05-03 11:30:00.000' AS DateTime)),
(CAST('2018-05-03 12:45:00.000' AS DateTime), CAST('2018-05-03 13:00:00.000' AS DateTime)),
(CAST('2018-05-03 14:00:00.000' AS DateTime), CAST('2018-05-03 15:45:00.000' AS DateTime)),
(CAST('2018-05-03 14:15:00.000' AS DateTime), CAST('2018-05-03 15:30:00.000' AS DateTime))
And expected result is:
ID StartDate EndDate
--------------------------------------------------------
1 2018-05-03 08:30:00.000 2018-05-03 11:30:00.000
2 2018-05-03 12:45:00.000 2018-05-03 13:00:00.000
3 2018-05-03 14:00:00.000 2018-05-03 15:45:00.000
Very similar answer, but making use of an index and windowed functions to make the gaps and islands analysis cheaper (faster).
http://sqlfiddle.com/#!18/f19569/3
SELECT
ROW_NUMBER() OVER (ORDER BY MIN(StartDate)),
MIN(StartDate),
MAX(EndDate)
from
(
SELECT
*,
SUM(CASE WHEN PrecedingEndDate >= StartDate THEN 0 ELSE 1 END)
OVER (ORDER BY StartDate, EndDate)
AS GroupID
FROM
(
SELECT
*,
MAX(EndDate)
OVER (ORDER BY StartDate, EndDate
ROWS BETWEEN UNBOUNDED PRECEDING
AND 1 PRECEDING
)
AS PrecedingEndDate
FROM
Table1
)
look_back
)
grouped
GROUP BY
GroupID
This is a form of the gaps and islands problem.
In this case, exists and cumulative sum and group by are the route to the solution:
select row_number() over (order by min(startdate)),
min(startdate), max(enddate)
from (select t1.*, sum(isstart) over (order by startdate) as grp
from (select t1.*,
(case when exists (select 1
from table1 tt1
where tt1.startdate <= t1.enddate and tt1.enddate >= t1.startdate and tt1.id <> t1.id
)
then 0 else 1
end) as isstart
from table1 t1
) t1
) t1
group by grp;

how to retrieve same empid have multiple deptnos with latest date in sql server

I have a question about SQL Server: how to get same empid and name have different deptno values (more than one deptnos) ,again
if we found more than one deptno then consider latest entrydate in SQL Server?
example:
empid |name |deptno |entrydate |deptname
6 |x |90 |2018-01-29 |PM
6 |x |80 |2018-01-29 |lead
6 |x |150 |2018-02-09 |tech
6 |y |170 |2015-03-09 |jn
6 |y |110 |2017-12-01 |Tester
6 |y |120 |2017-12-01 |analyst
6 |z |130 |2016-10-08 |support
Here empid:6 and name: x and entrydate: 2018-01-29 have multiple deptnos but we donot need this record because latest entry date is : 2018-02-09 for same empidandname
another one : empid:6 and name: y and entrydate : 2017-12-01 have multiple deptnos .this recrods we need because entry date is latest one is 2017-12-01 for thie id and name.
another one: empid:6 and name: z and entrydate: 2016-10-08 have donot have multiple deptnos then no need to show output.
Table :
CREATE TABLE [dbo].[empcnt](
[empid] [int] NULL,
[name] [varchar](50) NULL,
[deptno] [int] NULL,
[entrydate] [date] NULL,
[deptname] [varchar](50) NULL
)
INSERT [dbo].[empcnt] ([empid], [name], [deptno], [entrydate], [deptname]) VALUES (1, N'a', 10, CAST(N'2016-12-24' AS Date), N'HR')
GO
INSERT [dbo].[empcnt] ([empid], [name], [deptno], [entrydate], [deptname]) VALUES (1, N'b', 20, CAST(N'2017-10-29' AS Date), N'HR')
GO
INSERT [dbo].[empcnt] ([empid], [name], [deptno], [entrydate], [deptname]) VALUES (3, N'd', 40, CAST(N'2015-10-10' AS Date), N'IT')
GO
INSERT [dbo].[empcnt] ([empid], [name], [deptno], [entrydate], [deptname]) VALUES (3, N'd', 40, CAST(N'2015-10-10' AS Date), N'IT')
GO
INSERT [dbo].[empcnt] ([empid], [name], [deptno], [entrydate], [deptname]) VALUES (4, N'e', 59, CAST(N'2016-12-17' AS Date), N'Finance')
GO
INSERT [dbo].[empcnt] ([empid], [name], [deptno], [entrydate], [deptname]) VALUES (3, N'u', 40, CAST(N'2016-12-15' AS Date), N'CE')
GO
INSERT [dbo].[empcnt] ([empid], [name], [deptno], [entrydate], [deptname]) VALUES (5, N'h', 60, CAST(N'2017-12-27' AS Date), N'Sales')
GO
INSERT [dbo].[empcnt] ([empid], [name], [deptno], [entrydate], [deptname]) VALUES (1, N'a', 10, CAST(N'2016-12-24' AS Date), N'HR')
GO
INSERT [dbo].[empcnt] ([empid], [name], [deptno], [entrydate], [deptname]) VALUES (1, N'b', 50, CAST(N'2017-10-29' AS Date), N'Manager')
GO
INSERT [dbo].[empcnt] ([empid], [name], [deptno], [entrydate], [deptname]) VALUES (6, N'x', 90, CAST(N'2018-01-29' AS Date), N'PM')
GO
INSERT [dbo].[empcnt] ([empid], [name], [deptno], [entrydate], [deptname]) VALUES (6, N'x', 80, CAST(N'2018-01-29' AS Date), N'lead')
GO
INSERT [dbo].[empcnt] ([empid], [name], [deptno], [entrydate], [deptname]) VALUES (6, N'y', 110, CAST(N'2017-12-01' AS Date), N'Tester')
GO
INSERT [dbo].[empcnt] ([empid], [name], [deptno], [entrydate], [deptname]) VALUES (6, N'y', 120, CAST(N'2017-12-01' AS Date), N'analyst')
GO
INSERT [dbo].[empcnt] ([empid], [name], [deptno], [entrydate], [deptname]) VALUES (6, N'z', 130, CAST(N'2016-10-08' AS Date), N'support')
GO
INSERT [dbo].[empcnt] ([empid], [name], [deptno], [entrydate], [deptname]) VALUES (6, N'x', 150, CAST(N'2018-02-09' AS Date), N'tech')
GO
INSERT [dbo].[empcnt] ([empid], [name], [deptno], [entrydate], [deptname]) VALUES (6, N'y', 170, CAST(N'2015-03-09' AS Date), N'jn')
GO
based on above data I want output like below:
empid |name |deptno |entrydate |deptname
1 |b |50 |2017-10-29 |Manager
1 |b |20 |2017-10-29 |HR
6 |y |110 |2017-12-01 |Tester
6 |y |120 |2017-12-01 |analyst
I tried like below :
SELECT *
FROM empcnt a
WHERE EXISTS
(
select empid,name
from empcnt b
WHERE a.empid = b.empid AND a.name = b.Name and a.entrydate=b.entrydate and
group by empid,name ,entrydate
having count(distinct deptno)>1
)
but above query not given expected result.
please tell me how to write query to achive this task in sql server.
using a common table expression with dense_rank() and count() over() and a distinct subquery:
;with cte as (
select
c = count(empid) over (partition by empid, name, entrydate)
, dr = dense_rank() over (partition by empid, name order by entrydate desc)
, *
from (select distinct * from empcnt) x
)
select empid, name, deptno, entrydate, deptname
from cte
where c > 1
and dr = 1
rextester demo: http://rextester.com/LTN71730
returns:
+-------+------+--------+------------+----------+
| empid | name | deptno | entrydate | deptname |
+-------+------+--------+------------+----------+
| 1 | b | 20 | 2017-10-29 | HR |
| 1 | b | 50 | 2017-10-29 | Manager |
| 6 | y | 110 | 2017-12-01 | Tester |
| 6 | y | 120 | 2017-12-01 | analyst |
+-------+------+--------+------------+----------+
Another way using exists() and not exists()
select *
from empcnt o
where exists (
select 1
from empcnt i
where i.empid = o.empid
and i.name=o.name
and i.entrydate=o.entrydate
and i.deptno<>o.deptno
)
and not exists (
select 1
from empcnt i
where i.empid=o.empid
and i.name=o.name
and i.entrydate>o.entrydate
)

Time difference calculation in SQL Server

I have a question about SQL Server: how to calculate time difference with order by empid and time. If time difference is more than 5 hours, then status show 1 else 0.
CREATE TABLE [dbo].[Timecal]
(
[Emp ID] [float] NULL,
[time] [datetime] NULL
) ON [PRIMARY]
GO
INSERT [dbo].[Timecal] ([Emp ID], [time]) VALUES (1, CAST(N'2017-08-02T09:00:00.000' AS DateTime))
GO
INSERT [dbo].[Timecal] ([Emp ID], [time]) VALUES (1, CAST(N'2017-08-02T10:30:00.000' AS DateTime))
GO
INSERT [dbo].[Timecal] ([Emp ID], [time]) VALUES (1, CAST(N'2017-08-03T09:30:00.000' AS DateTime))
GO
INSERT [dbo].[Timecal] ([Emp ID], [time]) VALUES (1, CAST(N'2017-08-03T12:30:00.000' AS DateTime))
GO
INSERT [dbo].[Timecal] ([Emp ID], [time]) VALUES (1, CAST(N'2017-08-03T12:40:00.000' AS DateTime))
GO
INSERT [dbo].[Timecal] ([Emp ID], [time]) VALUES (1, CAST(N'2017-08-03T17:10:00.000' AS DateTime))
GO
INSERT [dbo].[Timecal] ([Emp ID], [time]) VALUES (1, CAST(N'2017-08-03T06:30:00.000' AS DateTime))
GO
INSERT [dbo].[Timecal] ([Emp ID], [time]) VALUES (1, CAST(N'2017-08-03T08:30:00.000' AS DateTime))
GO
INSERT [dbo].[Timecal] ([Emp ID], [time]) VALUES (1, CAST(N'2017-08-05T23:30:00.000' AS DateTime))
GO
INSERT [dbo].[Timecal] ([Emp ID], [time]) VALUES (1, CAST(N'2017-08-06T01:55:00.000' AS DateTime))
GO
INSERT [dbo].[Timecal] ([Emp ID], [time]) VALUES (1, CAST(N'2017-08-06T02:15:00.000' AS DateTime))
GO
INSERT [dbo].[Timecal] ([Emp ID], [time]) VALUES (1, CAST(N'2017-08-06T06:10:00.000' AS DateTime))
GO
INSERT [dbo].[Timecal] ([Emp ID], [time]) VALUES (1, CAST(N'2017-08-02T11:00:00.000' AS DateTime))
GO
INSERT [dbo].[Timecal] ([Emp ID], [time]) VALUES (1, CAST(N'2017-08-02T12:00:00.000' AS DateTime))
GO
INSERT [dbo].[Timecal] ([Emp ID], [time]) VALUES (1, CAST(N'2017-08-02T13:00:00.000' AS DateTime))
GO
INSERT [dbo].[Timecal] ([Emp ID], [time]) VALUES (1, CAST(N'2017-08-06T14:01:00.000' AS DateTime))
GO
INSERT [dbo].[Timecal] ([Emp ID], [time]) VALUES (1, CAST(N'2017-08-06T15:01:00.000' AS DateTime))
GO
INSERT [dbo].[Timecal] ([Emp ID], [time]) VALUES (1, CAST(N'2017-08-06T15:20:00.000' AS DateTime))
GO
INSERT [dbo].[Timecal] ([Emp ID], [time]) VALUES (1, CAST(N'2017-08-06T20:01:00.000' AS DateTime))
GO
INSERT [dbo].[Timecal] ([Emp ID], [time]) VALUES (5, CAST(N'2017-08-02T23:30:00.000' AS DateTime))
GO
INSERT [dbo].[Timecal] ([Emp ID], [time]) VALUES (5, CAST(N'2017-08-03T01:30:00.000' AS DateTime))
GO
INSERT [dbo].[Timecal] ([Emp ID], [time]) VALUES (5, CAST(N'2017-08-03T01:40:00.000' AS DateTime))
GO
INSERT [dbo].[Timecal] ([Emp ID], [time]) VALUES (5, CAST(N'2017-08-03T04:00:00.000' AS DateTime))
GO
INSERT [dbo].[Timecal] ([Emp ID], [time]) VALUES (5, CAST(N'2017-08-03T04:30:00.000' AS DateTime))
GO
INSERT [dbo].[Timecal] ([Emp ID], [time]) VALUES (5, CAST(N'2017-08-03T06:00:00.000' AS DateTime))
GO
Based on above data I want output like below:
Emp ID |time |status
--------+-------------------------------+------
1 | 2017-08-02 09:00:00.000 | 1
1 | 2017-08-02 10:30:00.000 | 0
1 | 2017-08-02 11:00:00.000 | 0
1 | 2017-08-02 12:00:00.000 | 0
1 | 2017-08-02 13:00:00.000 | 0
1 | 2017-08-03 06:30:00.000 | 1
1 | 2017-08-03 08:30:00.000 | 0
1 | 2017-08-03 09:30:00.000 | 0
1 | 2017-08-03 12:30:00.000 | 0
1 | 2017-08-03 12:40:00.000 | 0
1 | 2017-08-03 17:10:00.000 | 0
1 | 2017-08-05 23:30:00.000 | 1
1 | 2017-08-06 01:55:00.000 | 0
1 | 2017-08-06 02:15:00.000 | 0
1 | 2017-08-06 06:10:00.000 | 0
1 | 2017-08-06 14:01:00.000 | 1
1 | 2017-08-06 15:01:00.000 | 0
1 | 2017-08-06 15:20:00.000 | 0
1 | 2017-08-06 20:01:00.000 | 0
5 | 2017-08-02 23:30:00.000 | 1
5 | 2017-08-03 01:30:00.000 | 0
5 | 2017-08-03 01:40:00.000 | 0
5 | 2017-08-03 04:00:00.000 | 0
5 | 2017-08-03 04:30:00.000 | 0
5 | 2017-08-03 06:00:00.000 | 0
I tried like below :
select
i.*,
case
when datediff(hh, i.Time , o.Time) <= 5
then 0
else 1
end status
from
Timecal o
join
Timecal i on i.[emp id] = o.[emp id] and o.time < i.time
order by
i.time
But its not returning the expected output.
Please tell me how to achieve this task in SQL Server.
Use Row_number to order your table. Then join with previous row to compare
with cte as (
select
*, row_number() over (partition by [Emp ID] order by time) rn
from
Timecal
)
select
a.[Emp ID], a.time, status = case when datediff(hh, b.time, a.time) < 5 then 0 else 1 end
from
cte a
left join cte b on a.[Emp ID] = b.[Emp ID] and a.rn - 1 = b.rn

Calculate multiple shifts time difference in SQL Server

I have a question about SQL Server. Please tell me how to solve login and logout time calculation in SQL Server based on conditions.
if same empid will work multiple shifts, multiple shifts calculation for same date must be time difference is 5 hours then that date consider as multiple shifts for that emp
OnFloor time how much time he spend
OffFloor time how much time he spend
if logout is missed then consider as taligate is 1 or -1
Sample input data :
CREATE TABLE [dbo].[emplogindetails]
(
[Emp ID] [float] NULL,
[Area Of Access] [nvarchar](255) NULL,
[Time] [datetime] NULL
) ON [PRIMARY]
GO
INSERT [dbo].[emplogindetails] ([Emp ID], [Area Of Access], [Time])
VALUES (1, N'K4F White Rm IN', CAST(N'2017-08-02T09:00:00.000' AS DateTime))
INSERT [dbo].[emplogindetails] ([Emp ID], [Area Of Access], [Time])
VALUES (1, N'K4FWhite Rm OUT', CAST(N'2017-08-02T10:30:00.000' AS DateTime))
INSERT [dbo].[emplogindetails] ([Emp ID], [Area Of Access], [Time])
VALUES (1, N'K4F White Rm IN', CAST(N'2017-08-03T09:30:00.000' AS DateTime))
INSERT [dbo].[emplogindetails] ([Emp ID], [Area Of Access], [Time])
VALUES (1, N'K4FWhite Rm OUT', CAST(N'2017-08-03T12:30:00.000' AS DateTime))
INSERT [dbo].[emplogindetails] ([Emp ID], [Area Of Access], [Time])
VALUES (1, N'K4F White Rm IN', CAST(N'2017-08-03T12:40:00.000' AS DateTime))
INSERT [dbo].[emplogindetails] ([Emp ID], [Area Of Access], [Time])
VALUES (1, N'K4FWhite Rm OUT', CAST(N'2017-08-03T17:10:00.000' AS DateTime))
INSERT [dbo].[emplogindetails] ([Emp ID], [Area Of Access], [Time])
VALUES (1, N'K4F White Rm IN', CAST(N'2017-08-03T06:30:00.000' AS DateTime))
INSERT [dbo].[emplogindetails] ([Emp ID], [Area Of Access], [Time])
VALUES (1, N'K4FWhite Rm OUT', CAST(N'2017-08-03T08:30:00.000' AS DateTime))
INSERT [dbo].[emplogindetails] ([Emp ID], [Area Of Access], [Time])
VALUES (1, N'K4F White Rm IN', CAST(N'2017-08-05T23:30:00.000' AS DateTime))
INSERT [dbo].[emplogindetails] ([Emp ID], [Area Of Access], [Time])
VALUES (1, N'K4FWhite Rm OUT', CAST(N'2017-08-06T01:55:00.000' AS DateTime))
INSERT [dbo].[emplogindetails] ([Emp ID], [Area Of Access], [Time])
VALUES (1, N'K4F White Rm IN', CAST(N'2017-08-06T02:15:00.000' AS DateTime))
INSERT [dbo].[emplogindetails] ([Emp ID], [Area Of Access], [Time])
VALUES (1, N'K4FWhite Rm OUT', CAST(N'2017-08-06T06:10:00.000' AS DateTime))
INSERT [dbo].[emplogindetails] ([Emp ID], [Area Of Access], [Time])
VALUES (1, N'K4F White Rm IN', CAST(N'2017-08-02T11:00:00.000' AS DateTime))
INSERT [dbo].[emplogindetails] ([Emp ID], [Area Of Access], [Time])
VALUES (1, N'K4FWhite Rm OUT', CAST(N'2017-08-02T12:00:00.000' AS DateTime))
INSERT [dbo].[emplogindetails] ([Emp ID], [Area Of Access], [Time])
VALUES (1, N'K4F White Rm IN', CAST(N'2017-08-02T13:00:00.000' AS DateTime))
INSERT [dbo].[emplogindetails] ([Emp ID], [Area Of Access], [Time])
VALUES (1, N'K4F White Rm IN', CAST(N'2017-08-06T14:01:00.000' AS DateTime))
INSERT [dbo].[emplogindetails] ([Emp ID], [Area Of Access], [Time])
VALUES (1, N'K4FWhite Rm OUT', CAST(N'2017-08-06T15:01:00.000' AS DateTime))
INSERT [dbo].[emplogindetails] ([Emp ID], [Area Of Access], [Time])
VALUES (1, N'K4F White Rm IN', CAST(N'2017-08-06T15:20:00.000' AS DateTime))
INSERT [dbo].[emplogindetails] ([Emp ID], [Area Of Access], [Time])
VALUES (1, N'K4FWhite Rm OUT', CAST(N'2017-08-06T20:01:00.000' AS DateTime))
GO
Based on above data 4 th dates is holiday and he entered 5th date night shift and logout 6th date and same 6 th date he did another shift.
I want an output like below:
ShiftDate |ShitStartTime |ShiftEndTime |Total_Time |OnFloor |OffFloor |EmpID |Incount |OutCount |Tailgate
08/05/2017 |2017-08-05 23:30:00.000 |2017-08-06 06:10:00.000 |6:40:00 |06:00:00 |00:40:00 |1 |2 | 2 | 0
08/02/2017 |2017-08-02 09:00:00.000 |2017-08-02 13:00:00.000 |04:00:00 |02:30:00 |01:30:00 |1 |3 | 2 | 1
08/03/2017 |2017-08-03 06:30:00.000 |2017-08-03 17:10:00.000 |10:40:00 |09:30:00 |01:10:00 |1 |3 | 3 | 0
08/06/2017 |2017-08-06 14:01:00.000 |2017-08-06 20:01:00.000 |6:00:00 |05:10:00 |00:50:00 |1 |2 | 2 | 0
I tried like this:
select
isnull( ShiftDate ,'1900-01-01')as ShiftDate ,
isnull( min(logintime) ,'1900-01-01') as ShitStartTime,
max( case when logouttime is null then '1900-01-01' else logouttime end )ShiftEndTime ,
convert(varchar(8),dateadd(ss,sum(datediff(second,0,dateadd(day,0,Total_Time))),0),108) Total_Time,
convert(varchar(8),dateadd(ss,sum(datediff(second,0,dateadd(day,0,OnFloor))),0),108) OnFloor,
convert(varchar(8),dateadd(ss,sum(datediff(second,0,dateadd(day,0,OffFloor))),0),108) OffFloor,
EmpID ,Incount ,OutCount, Tailgate
from (
select
CONVERT(VARCHAR(12), ( OffFloor + cast ( OnFloor as int)) / 60 / 60 % 24)
+':'+ CONVERT(VARCHAR(2), (OffFloor + cast ( OnFloor as int)) / 60 % 60)
+':'+ CONVERT(VARCHAR(2), (OffFloor + cast ( OnFloor as int)) % 60) as Total_Time
,case when convert( varchar(10),OnFloor )='0' then '0:0:0' else OnFloor end OnFloor
, CONVERT(VARCHAR(12), (OffFloor) / 60 / 60 % 24) +':'+ CONVERT(VARCHAR(2), (OffFloor) / 60 % 60)
+':'+ CONVERT(VARCHAR(2), (OffFloor) % 60) AS OffFloor
,[Emp ID] ,[Area Of Access],status,logintime,logouttime
from (
select isnull( DATEDIFF(SECOND, a.logintime, a.logouttime) ,0) OffFloor , cast ( '0' as varchar) as OnFloor
,[Emp ID] ,[Area Of Access],status,logintime,logouttime
from (
SELECT o.time logouttime,i.Time logintime,i.[Emp ID]
,substring ( i.[Area Of Access] ,charindex('out',i.[Area Of Access]),len(i.[Area Of Access])) status
,i.[Area Of Access]
FROM test.dbo.emplogindetails i left join test.dbo.emplogindetails o
on i.[emp id] = o.[emp id]
AND CONVERT(date, i.time) = CONVERT(date, o.time)
AND o.time > i.time
AND substring ( o.[Area Of Access] ,charindex('in',o.[Area Of Access]),len(o.[Area Of Access]))='in'
and substring ( i.[Area Of Access] ,charindex('out',i.[Area Of Access]),len(i.[Area Of Access]))='out'
and o.Time=(SELECT MIN(o2.time)
FROM test.dbo.emplogindetails o2
WHERE o2.time > i.time
and o2.[Emp ID]=i.[Emp ID]
---and [emp id]='105828'
)
--where i.[emp id]='105828'
)a where a.status='out')a
union all
select CONVERT(VARCHAR(12), (OffFloor + OnFloor) / 60 / 60 % 24)
+':'+ CONVERT(VARCHAR(2), (OffFloor + OnFloor) / 60 % 60)
+':'+ CONVERT(VARCHAR(2), (OffFloor + OnFloor) % 60) as Calculated_Time
, CONVERT(VARCHAR(12), (OnFloor) / 60 / 60 % 24)
+':'+ CONVERT(VARCHAR(2), (OnFloor) / 60 % 60)+':'+ CONVERT(VARCHAR(2), (OnFloor) % 60) AS OnFloor
, case when convert( varchar(10),OffFloor) ='0' then '0:0:0' else OffFloor end OffFloor
,[Emp ID] ,[Area Of Access],
status,logintime,logouttime
from (
select '0' as OffFloor, isnull( DATEDIFF(SECOND, a.logintime, a.logouttime) ,0) OnFloor
,[Emp ID] ,[Area Of Access],status
,logintime,logouttime
from (
SELECT o.time logouttime,i.Time logintime,i.[Emp ID]
,substring ( i.[Area Of Access] ,charindex('in',i.[Area Of Access]),len(i.[Area Of Access])) status
,i.[Area Of Access]
FROM test.dbo.emplogindetails i left join test.dbo.emplogindetails o
on i.[emp id] = o.[emp id]
AND CONVERT(date, i.time) = CONVERT(date, o.time)
AND o.time > i.time
AND substring ( o.[Area Of Access] ,charindex('out',o.[Area Of Access]),len(o.[Area Of Access]))='out'
and substring ( i.[Area Of Access] ,charindex('in',i.[Area Of Access]),len(i.[Area Of Access]))='in'
and o.Time=(SELECT MIN(o2.time)
FROM test.dbo.emplogindetails o2
WHERE o2.time > i.time
and o2.[Emp ID]=i.[Emp ID]
---and [emp id]='105828'
)
---where i.[emp id]='105828'
)a where a.status='in')stag)stag
join
----get incount and outcount and tailgate information
(select [emp id]as empid,incount,outcount,
isnull( incount-outcount ,0) as Tailgate ,Date as ShiftDate
from (
select
i.[Emp ID] ,convert(varchar(10),time,101) as Date,
count( case when substring ( i.[Area Of Access] ,charindex('in',i.[Area Of Access]),len(i.[Area Of Access]))='in'
then 'in' end )Incount
,count( case when substring ( i.[Area Of Access] ,charindex('out',i.[Area Of Access]),len(i.[Area Of Access]))='out'
then 'out' end )outcount
FROM test.dbo.emplogindetails i
--where [emp id]='105828'
group by i.[Emp ID],convert(varchar(10),time,101)
)cnt)cnt
on stag.[Emp ID]=cnt.empid and convert(varchar(10),stag.logintime,101)=cnt.ShiftDate
group by EmpID ,Incount ,OutCount, Tailgate
,isnull( ShiftDate ,'1900-01-01')
This query is not returning the expected result when same date have multiple shifts calculation if logout and login time difference more than 5 hours then consider as next shift.
as per given below logic not working for empid=5 and data looking like below.
Hi ,one records is failed as per our logic .some changes is required in logic. I did not get expected ouput for below example.
INSERT [dbo].[emplogindetails] ([Emp ID], [Area Of Access], [Time]) VALUES (5, N'K4F White Rm IN', CAST(N'2017-08-02T23:30:00.000' AS DateTime))
GO
INSERT [dbo].[emplogindetails] ([Emp ID], [Area Of Access], [Time]) VALUES (5, N' K4FWhite Rm OUT', CAST(N'2017-08-03T01:30:00.000' AS DateTime))
GO
INSERT [dbo].[emplogindetails] ([Emp ID], [Area Of Access], [Time]) VALUES (5, N'K4F White Rm IN', CAST(N'2017-08-03T01:40:00.000' AS DateTime))
GO
INSERT [dbo].[emplogindetails] ([Emp ID], [Area Of Access], [Time]) VALUES (5, N'K4F White Rm OUT', CAST(N'2017-08-03T04:00:00.000' AS DateTime))
GO
INSERT [dbo].[emplogindetails] ([Emp ID], [Area Of Access], [Time]) VALUES (5, N'K4F White Rm IN', CAST(N'2017-08-03T04:30:00.000' AS DateTime))
GO
INSERT [dbo].[emplogindetails] ([Emp ID], [Area Of Access], [Time]) VALUES (5, N'K4F White Rm OUT', CAST(N'2017-08-03T06:00:00.000' AS DateTime))
GO
Please tell me how to achieve this task in SQL Server
This query returns your expected output
select
cast(ShitStartTime as date) ShiftDate, ShitStartTime, ShiftEndTime
, concat(right(concat('0', tTime/60), 2), ':', right(concat('0',tTime%60), 2)) Total_Time
, concat(right(concat('0', onF/60), 2), ':', right(concat('0',onF%60), 2)) OnFloor
, concat(right(concat('0', offF/60), 2), ':', right(concat('0',offF%60), 2)) OffFloor
, [Emp Id], Incount, OutCount, Tailgate
from (
select
[Emp Id], min(Time) ShitStartTime, max(Time) ShiftEndTime, sum(iif([Area Of Access] = 'K4F White Rm IN', diff, 0)) onF
, sum(iif([Area Of Access] = 'K4FWhite Rm OUT', diff, 0)) offF, sum(diff) tTime
, sum(iif([Area Of Access] = 'K4F White Rm IN', 1, 0)) Incount
, sum(iif([Area Of Access] = 'K4FWhite Rm OUT', 1, 0)) OutCount
, max(Tailgate) Tailgate
from (
select
*, datediff(mi, Time, lead(Time) over (partition by [Emp Id], group_ order by Time)) diff
, iif(Time = max(Time) over (partition by [Emp Id], group_) and [Area Of Access] = 'K4F White Rm IN', 1, 0) Tailgate
from (
select
*, sum(gr) over (partition by [Emp Id] order by Time rows unbounded preceding) group_
from (
select
*, iif(datediff(hh, lag(Time) over (partition by [Emp Id] order by Time), Time) <= 5, 0, 1) gr
from
emplogindetails
) t
) t
) t
group by [Emp Id], group_
) t

SQL Query with multiple PIVOT

I have 3 tables as Register, Revision, Issue. table structure and some datas are as follows,
CREATE TABLE [dbo].[Issue](
[ID] [int] IDENTITY(1,1) NOT NULL,
[ProjectID] [int] NULL,
[Drawing ID] [int] NULL,
[Revision ID] [int] NULL,
[Issue Number] [int] NULL,
[Weight1] [float] NULL,
[Weight2] [float] NULL,
[Weight3] [float] NULL,
CONSTRAINT [PK_DrawingIssue] PRIMARY KEY CLUSTERED
(
[ID] ASC
)WITH (PAD_INDEX = OFF, STATISTICS_NORECOMPUTE = OFF, IGNORE_DUP_KEY = OFF, ALLOW_ROW_LOCKS = ON, ALLOW_PAGE_LOCKS = ON) ON [PRIMARY]
) ON [PRIMARY]
GO
CREATE TABLE [dbo].[Register](
[ID] [int] NOT NULL,
[ProjectID] [int] NULL,
[Number] [nvarchar](255) NULL,
CONSTRAINT [PK_Drawing_Register] PRIMARY KEY CLUSTERED
(
[ID] ASC
)WITH (PAD_INDEX = OFF, STATISTICS_NORECOMPUTE = OFF, IGNORE_DUP_KEY = OFF, ALLOW_ROW_LOCKS = ON, ALLOW_PAGE_LOCKS = ON) ON [PRIMARY]
) ON [PRIMARY]
GO
CREATE TABLE [dbo].[Revision](
[ID] [int] IDENTITY(1,1) NOT NULL,
[ProjectID] [int] NULL,
[Drawing ID] [int] NULL,
[Revision] [nvarchar](255) NULL,
[SDate] [datetime] NULL,
[EDate] [datetime] NULL,
CONSTRAINT [PK_DrawingRevision] PRIMARY KEY CLUSTERED
(
[ID] ASC
)WITH (PAD_INDEX = OFF, STATISTICS_NORECOMPUTE = OFF, IGNORE_DUP_KEY = OFF, ALLOW_ROW_LOCKS = ON, ALLOW_PAGE_LOCKS = ON) ON [PRIMARY]
) ON [PRIMARY]
GO
SET IDENTITY_INSERT [dbo].[Issue] ON
GO
INSERT [dbo].[Issue] ([ID], [ProjectID], [Drawing ID], [Revision ID], [Issue Number], [Weight1], [Weight2], [Weight3]) VALUES (9, 23, 3, 5, 2, 12, NULL, NULL)
GO
INSERT [dbo].[Issue] ([ID], [ProjectID], [Drawing ID], [Revision ID], [Issue Number], [Weight1], [Weight2], [Weight3]) VALUES (10, 23, 4, 6, 9, NULL, 32, NULL)
GO
INSERT [dbo].[Issue] ([ID], [ProjectID], [Drawing ID], [Revision ID], [Issue Number], [Weight1], [Weight2], [Weight3]) VALUES (19, 23, 7, 12, 2, 24, NULL, 24)
GO
INSERT [dbo].[Issue] ([ID], [ProjectID], [Drawing ID], [Revision ID], [Issue Number], [Weight1], [Weight2], [Weight3]) VALUES (164, 23, 12, 61, 2, NULL, NULL, 42)
GO
INSERT [dbo].[Issue] ([ID], [ProjectID], [Drawing ID], [Revision ID], [Issue Number], [Weight1], [Weight2], [Weight3]) VALUES (165, 23, 13, 62, 1, 24, NULL, NULL)
GO
INSERT [dbo].[Issue] ([ID], [ProjectID], [Drawing ID], [Revision ID], [Issue Number], [Weight1], [Weight2], [Weight3]) VALUES (167, 23, 13, 62, 0, NULL, 42, NULL)
GO
INSERT [dbo].[Issue] ([ID], [ProjectID], [Drawing ID], [Revision ID], [Issue Number], [Weight1], [Weight2], [Weight3]) VALUES (168, 23, 13, 62, 2, NULL, 43, NULL)
GO
INSERT [dbo].[Issue] ([ID], [ProjectID], [Drawing ID], [Revision ID], [Issue Number], [Weight1], [Weight2], [Weight3]) VALUES (169, 23, 13, 64, 0, NULL, NULL, 24)
GO
INSERT [dbo].[Issue] ([ID], [ProjectID], [Drawing ID], [Revision ID], [Issue Number], [Weight1], [Weight2], [Weight3]) VALUES (170, 23, 13, 64, 1, NULL, 42, 42)
GO
INSERT [dbo].[Issue] ([ID], [ProjectID], [Drawing ID], [Revision ID], [Issue Number], [Weight1], [Weight2], [Weight3]) VALUES (171, 23, 13, 64, 3, NULL, 24, NULL)
GO
INSERT [dbo].[Issue] ([ID], [ProjectID], [Drawing ID], [Revision ID], [Issue Number], [Weight1], [Weight2], [Weight3]) VALUES (254, 23, 5, 86, 4, 24, NULL, NULL)
GO
INSERT [dbo].[Issue] ([ID], [ProjectID], [Drawing ID], [Revision ID], [Issue Number], [Weight1], [Weight2], [Weight3]) VALUES (256, 23, 13, 88, 2, 24, NULL, NULL)
GO
INSERT [dbo].[Issue] ([ID], [ProjectID], [Drawing ID], [Revision ID], [Issue Number], [Weight1], [Weight2], [Weight3]) VALUES (257, 23, 13, 89, 1, NULL, 24, NULL)
GO
INSERT [dbo].[Issue] ([ID], [ProjectID], [Drawing ID], [Revision ID], [Issue Number], [Weight1], [Weight2], [Weight3]) VALUES (258, 23, 13, 64, 2, 8, 7, 6)
GO
INSERT [dbo].[Issue] ([ID], [ProjectID], [Drawing ID], [Revision ID], [Issue Number], [Weight1], [Weight2], [Weight3]) VALUES (259, 23, 16, 91, 1, NULL, 4, 6)
GO
INSERT [dbo].[Issue] ([ID], [ProjectID], [Drawing ID], [Revision ID], [Issue Number], [Weight1], [Weight2], [Weight3]) VALUES (260, 23, 16, 91, 2, NULL, NULL, 4)
GO
INSERT [dbo].[Issue] ([ID], [ProjectID], [Drawing ID], [Revision ID], [Issue Number], [Weight1], [Weight2], [Weight3]) VALUES (297, 2, 17, 108, 1, NULL, 7, NULL)
GO
INSERT [dbo].[Issue] ([ID], [ProjectID], [Drawing ID], [Revision ID], [Issue Number], [Weight1], [Weight2], [Weight3]) VALUES (298, 2, 17, 108, 2, NULL, 68, NULL)
GO
INSERT [dbo].[Issue] ([ID], [ProjectID], [Drawing ID], [Revision ID], [Issue Number], [Weight1], [Weight2], [Weight3]) VALUES (299, 2, 17, 108, 6, 67, NULL, 86)
GO
INSERT [dbo].[Issue] ([ID], [ProjectID], [Drawing ID], [Revision ID], [Issue Number], [Weight1], [Weight2], [Weight3]) VALUES (300, 2, 17, 109, 2, NULL, 68, NULL)
GO
INSERT [dbo].[Issue] ([ID], [ProjectID], [Drawing ID], [Revision ID], [Issue Number], [Weight1], [Weight2], [Weight3]) VALUES (301, 2, 17, 109, 1, NULL, NULL, 68)
GO
INSERT [dbo].[Issue] ([ID], [ProjectID], [Drawing ID], [Revision ID], [Issue Number], [Weight1], [Weight2], [Weight3]) VALUES (302, 2, 17, 109, 3, 68, NULL, NULL)
GO
INSERT [dbo].[Issue] ([ID], [ProjectID], [Drawing ID], [Revision ID], [Issue Number], [Weight1], [Weight2], [Weight3]) VALUES (303, 2, 17, 110, 1, NULL, 86, NULL)
GO
INSERT [dbo].[Issue] ([ID], [ProjectID], [Drawing ID], [Revision ID], [Issue Number], [Weight1], [Weight2], [Weight3]) VALUES (304, 2, 17, 110, 2, 68, NULL, 68)
GO
INSERT [dbo].[Issue] ([ID], [ProjectID], [Drawing ID], [Revision ID], [Issue Number], [Weight1], [Weight2], [Weight3]) VALUES (305, 2, 17, 110, 7, NULL, 68, NULL)
GO
INSERT [dbo].[Issue] ([ID], [ProjectID], [Drawing ID], [Revision ID], [Issue Number], [Weight1], [Weight2], [Weight3]) VALUES (306, 2, 17, 110, 8, NULL, NULL, 68)
GO
INSERT [dbo].[Issue] ([ID], [ProjectID], [Drawing ID], [Revision ID], [Issue Number], [Weight1], [Weight2], [Weight3]) VALUES (307, 2, 17, 110, 6, NULL, 68, NULL)
GO
INSERT [dbo].[Issue] ([ID], [ProjectID], [Drawing ID], [Revision ID], [Issue Number], [Weight1], [Weight2], [Weight3]) VALUES (308, 23, 18, 111, 1, 68, NULL, NULL)
GO
INSERT [dbo].[Issue] ([ID], [ProjectID], [Drawing ID], [Revision ID], [Issue Number], [Weight1], [Weight2], [Weight3]) VALUES (309, 23, 19, 112, 1, NULL, 68, 8)
GO
INSERT [dbo].[Issue] ([ID], [ProjectID], [Drawing ID], [Revision ID], [Issue Number], [Weight1], [Weight2], [Weight3]) VALUES (310, 23, 20, 113, 1, NULL, 6, NULL)
GO
INSERT [dbo].[Issue] ([ID], [ProjectID], [Drawing ID], [Revision ID], [Issue Number], [Weight1], [Weight2], [Weight3]) VALUES (311, 23, 21, 114, 1, 3, NULL, 68)
GO
INSERT [dbo].[Issue] ([ID], [ProjectID], [Drawing ID], [Revision ID], [Issue Number], [Weight1], [Weight2], [Weight3]) VALUES (312, 23, 22, 115, 1, NULL, 5, NULL)
GO
INSERT [dbo].[Issue] ([ID], [ProjectID], [Drawing ID], [Revision ID], [Issue Number], [Weight1], [Weight2], [Weight3]) VALUES (313, 23, 23, 116, 1, NULL, 4, 34)
GO
INSERT [dbo].[Issue] ([ID], [ProjectID], [Drawing ID], [Revision ID], [Issue Number], [Weight1], [Weight2], [Weight3]) VALUES (314, 23, 24, 117, 1, 5, 46, 436)
GO
INSERT [dbo].[Issue] ([ID], [ProjectID], [Drawing ID], [Revision ID], [Issue Number], [Weight1], [Weight2], [Weight3]) VALUES (315, 23, 25, 118, 1, NULL, NULL, 6)
GO
INSERT [dbo].[Issue] ([ID], [ProjectID], [Drawing ID], [Revision ID], [Issue Number], [Weight1], [Weight2], [Weight3]) VALUES (316, 23, 26, 119, 1, 46, 45, NULL)
GO
INSERT [dbo].[Issue] ([ID], [ProjectID], [Drawing ID], [Revision ID], [Issue Number], [Weight1], [Weight2], [Weight3]) VALUES (317, 23, 27, 120, 1, NULL, 6, NULL)
GO
INSERT [dbo].[Issue] ([ID], [ProjectID], [Drawing ID], [Revision ID], [Issue Number], [Weight1], [Weight2], [Weight3]) VALUES (318, 23, 28, 121, 1, NULL, NULL, 4)
GO
INSERT [dbo].[Issue] ([ID], [ProjectID], [Drawing ID], [Revision ID], [Issue Number], [Weight1], [Weight2], [Weight3]) VALUES (319, 23, 29, 122, 1, NULL, 45, NULL)
GO
INSERT [dbo].[Issue] ([ID], [ProjectID], [Drawing ID], [Revision ID], [Issue Number], [Weight1], [Weight2], [Weight3]) VALUES (320, 23, 30, 123, 1, 36, NULL, NULL)
GO
INSERT [dbo].[Issue] ([ID], [ProjectID], [Drawing ID], [Revision ID], [Issue Number], [Weight1], [Weight2], [Weight3]) VALUES (321, 26, 31, 124, 1, NULL, 36, NULL)
GO
INSERT [dbo].[Issue] ([ID], [ProjectID], [Drawing ID], [Revision ID], [Issue Number], [Weight1], [Weight2], [Weight3]) VALUES (322, 26, 32, 125, 1, 36, NULL, 36)
GO
INSERT [dbo].[Issue] ([ID], [ProjectID], [Drawing ID], [Revision ID], [Issue Number], [Weight1], [Weight2], [Weight3]) VALUES (323, 26, 33, 126, 1, NULL, 36, NULL)
GO
INSERT [dbo].[Issue] ([ID], [ProjectID], [Drawing ID], [Revision ID], [Issue Number], [Weight1], [Weight2], [Weight3]) VALUES (324, 26, 34, 127, 1, NULL, NULL, 36)
GO
INSERT [dbo].[Issue] ([ID], [ProjectID], [Drawing ID], [Revision ID], [Issue Number], [Weight1], [Weight2], [Weight3]) VALUES (325, 26, 35, 128, 1, 36, NULL, 45)
GO
INSERT [dbo].[Issue] ([ID], [ProjectID], [Drawing ID], [Revision ID], [Issue Number], [Weight1], [Weight2], [Weight3]) VALUES (326, 2, 36, 129, 1, NULL, 36, NULL)
GO
INSERT [dbo].[Issue] ([ID], [ProjectID], [Drawing ID], [Revision ID], [Issue Number], [Weight1], [Weight2], [Weight3]) VALUES (327, 26, 37, 130, 1, NULL, NULL, NULL)
GO
INSERT [dbo].[Issue] ([ID], [ProjectID], [Drawing ID], [Revision ID], [Issue Number], [Weight1], [Weight2], [Weight3]) VALUES (328, 2, 38, 131, 1, NULL, 7, NULL)
GO
INSERT [dbo].[Issue] ([ID], [ProjectID], [Drawing ID], [Revision ID], [Issue Number], [Weight1], [Weight2], [Weight3]) VALUES (329, 23, 39, 132, 1, NULL, 56, NULL)
GO
SET IDENTITY_INSERT [dbo].[Issue] OFF
GO
INSERT [dbo].[Register] ([ID], [ProjectID], [Number]) VALUES (3, 23, N'1')
GO
INSERT [dbo].[Register] ([ID], [ProjectID], [Number]) VALUES (4, 23, N'7')
GO
INSERT [dbo].[Register] ([ID], [ProjectID], [Number]) VALUES (7, 23, N'3333')
GO
INSERT [dbo].[Register] ([ID], [ProjectID], [Number]) VALUES (12, 23, N'D1')
GO
INSERT [dbo].[Register] ([ID], [ProjectID], [Number]) VALUES (13, 23, N'DT1')
GO
INSERT [dbo].[Register] ([ID], [ProjectID], [Number]) VALUES (16, 23, N'Dwg1')
GO
INSERT [dbo].[Register] ([ID], [ProjectID], [Number]) VALUES (17, 2, N'D1')
GO
INSERT [dbo].[Register] ([ID], [ProjectID], [Number]) VALUES (18, 23, N'23')
GO
INSERT [dbo].[Register] ([ID], [ProjectID], [Number]) VALUES (19, 23, N'983')
GO
INSERT [dbo].[Register] ([ID], [ProjectID], [Number]) VALUES (20, 23, N'100')
GO
INSERT [dbo].[Register] ([ID], [ProjectID], [Number]) VALUES (21, 23, N'11112')
GO
INSERT [dbo].[Register] ([ID], [ProjectID], [Number]) VALUES (22, 23, N'555')
GO
INSERT [dbo].[Register] ([ID], [ProjectID], [Number]) VALUES (23, 23, N'666666')
GO
INSERT [dbo].[Register] ([ID], [ProjectID], [Number]) VALUES (24, 23, N'77')
GO
INSERT [dbo].[Register] ([ID], [ProjectID], [Number]) VALUES (25, 23, N'88')
GO
INSERT [dbo].[Register] ([ID], [ProjectID], [Number]) VALUES (26, 23, N'99')
GO
INSERT [dbo].[Register] ([ID], [ProjectID], [Number]) VALUES (27, 23, N'2')
GO
INSERT [dbo].[Register] ([ID], [ProjectID], [Number]) VALUES (28, 23, N'3')
GO
INSERT [dbo].[Register] ([ID], [ProjectID], [Number]) VALUES (29, 23, N'4')
GO
INSERT [dbo].[Register] ([ID], [ProjectID], [Number]) VALUES (30, 23, N'Dwg12345')
GO
INSERT [dbo].[Register] ([ID], [ProjectID], [Number]) VALUES (31, 26, N'1')
GO
INSERT [dbo].[Register] ([ID], [ProjectID], [Number]) VALUES (32, 26, N'2')
GO
INSERT [dbo].[Register] ([ID], [ProjectID], [Number]) VALUES (33, 26, N'3')
GO
INSERT [dbo].[Register] ([ID], [ProjectID], [Number]) VALUES (34, 26, N'4')
GO
INSERT [dbo].[Register] ([ID], [ProjectID], [Number]) VALUES (35, 26, N'5')
GO
INSERT [dbo].[Register] ([ID], [ProjectID], [Number]) VALUES (36, 2, N'DT123')
GO
INSERT [dbo].[Register] ([ID], [ProjectID], [Number]) VALUES (37, 26, N'DTApr04')
GO
INSERT [dbo].[Register] ([ID], [ProjectID], [Number]) VALUES (38, 2, N'DTApr05')
GO
INSERT [dbo].[Register] ([ID], [ProjectID], [Number]) VALUES (39, 23, N'DTA05')
GO
SET IDENTITY_INSERT [dbo].[Revision] ON
GO
INSERT [dbo].[Revision] ([ID], [ProjectID], [Drawing ID], [Revision], [SDate], [EDate]) VALUES (5, 23, 3, N'0', CAST(N'2017-04-21 00:00:00.000' AS DateTime), NULL)
GO
INSERT [dbo].[Revision] ([ID], [ProjectID], [Drawing ID], [Revision], [SDate], [EDate]) VALUES (6, 23, 4, N'0', NULL, CAST(N'2017-04-18 00:00:00.000' AS DateTime))
GO
INSERT [dbo].[Revision] ([ID], [ProjectID], [Drawing ID], [Revision], [SDate], [EDate]) VALUES (12, 23, 7, N'3', CAST(N'2017-04-18 00:00:00.000' AS DateTime), NULL)
GO
INSERT [dbo].[Revision] ([ID], [ProjectID], [Drawing ID], [Revision], [SDate], [EDate]) VALUES (61, 23, 12, N'0', NULL, CAST(N'2017-04-10 00:00:00.000' AS DateTime))
GO
INSERT [dbo].[Revision] ([ID], [ProjectID], [Drawing ID], [Revision], [SDate], [EDate]) VALUES (62, 23, 13, N'0', CAST(N'2017-04-18 00:00:00.000' AS DateTime), CAST(N'2017-04-08 00:00:00.000' AS DateTime))
GO
INSERT [dbo].[Revision] ([ID], [ProjectID], [Drawing ID], [Revision], [SDate], [EDate]) VALUES (64, 23, 13, N'1', CAST(N'2017-04-18 00:00:00.000' AS DateTime), NULL)
GO
INSERT [dbo].[Revision] ([ID], [ProjectID], [Drawing ID], [Revision], [SDate], [EDate]) VALUES (88, 23, 13, N'1', CAST(N'2017-04-18 00:00:00.000' AS DateTime), NULL)
GO
INSERT [dbo].[Revision] ([ID], [ProjectID], [Drawing ID], [Revision], [SDate], [EDate]) VALUES (89, 23, 13, N'2', NULL, CAST(N'2017-04-18 00:00:00.000' AS DateTime))
GO
INSERT [dbo].[Revision] ([ID], [ProjectID], [Drawing ID], [Revision], [SDate], [EDate]) VALUES (90, 23, 13, N'1', CAST(N'2017-04-18 00:00:00.000' AS DateTime), CAST(N'2017-04-18 00:00:00.000' AS DateTime))
GO
INSERT [dbo].[Revision] ([ID], [ProjectID], [Drawing ID], [Revision], [SDate], [EDate]) VALUES (91, 23, 16, N'0', CAST(N'2017-04-18 00:00:00.000' AS DateTime), NULL)
GO
INSERT [dbo].[Revision] ([ID], [ProjectID], [Drawing ID], [Revision], [SDate], [EDate]) VALUES (108, 2, 17, N'0', CAST(N'2017-04-18 00:00:00.000' AS DateTime), NULL)
GO
INSERT [dbo].[Revision] ([ID], [ProjectID], [Drawing ID], [Revision], [SDate], [EDate]) VALUES (109, 2, 17, N'1', CAST(N'2017-04-18 00:00:00.000' AS DateTime), NULL)
GO
INSERT [dbo].[Revision] ([ID], [ProjectID], [Drawing ID], [Revision], [SDate], [EDate]) VALUES (110, 2, 17, N'2', NULL, CAST(N'2017-04-18 00:00:00.000' AS DateTime))
GO
INSERT [dbo].[Revision] ([ID], [ProjectID], [Drawing ID], [Revision], [SDate], [EDate]) VALUES (111, 23, 18, N'0', CAST(N'2017-04-18 00:00:00.000' AS DateTime), NULL)
GO
INSERT [dbo].[Revision] ([ID], [ProjectID], [Drawing ID], [Revision], [SDate], [EDate]) VALUES (112, 23, 19, N'0', NULL, CAST(N'2017-04-18 00:00:00.000' AS DateTime))
GO
INSERT [dbo].[Revision] ([ID], [ProjectID], [Drawing ID], [Revision], [SDate], [EDate]) VALUES (113, 23, 20, N'0', CAST(N'2017-04-18 00:00:00.000' AS DateTime), NULL)
GO
INSERT [dbo].[Revision] ([ID], [ProjectID], [Drawing ID], [Revision], [SDate], [EDate]) VALUES (114, 23, 21, N'0', NULL, CAST(N'2017-04-18 00:00:00.000' AS DateTime))
GO
INSERT [dbo].[Revision] ([ID], [ProjectID], [Drawing ID], [Revision], [SDate], [EDate]) VALUES (115, 23, 22, N'0', CAST(N'2017-04-18 00:00:00.000' AS DateTime), NULL)
GO
INSERT [dbo].[Revision] ([ID], [ProjectID], [Drawing ID], [Revision], [SDate], [EDate]) VALUES (116, 23, 23, N'0', CAST(N'2017-04-18 00:00:00.000' AS DateTime), NULL)
GO
INSERT [dbo].[Revision] ([ID], [ProjectID], [Drawing ID], [Revision], [SDate], [EDate]) VALUES (117, 23, 24, N'0', CAST(N'2017-04-18 00:00:00.000' AS DateTime), NULL)
GO
INSERT [dbo].[Revision] ([ID], [ProjectID], [Drawing ID], [Revision], [SDate], [EDate]) VALUES (118, 23, 25, N'0', CAST(N'2017-04-18 00:00:00.000' AS DateTime), NULL)
GO
INSERT [dbo].[Revision] ([ID], [ProjectID], [Drawing ID], [Revision], [SDate], [EDate]) VALUES (119, 23, 26, N'0', CAST(N'2017-04-18 00:00:00.000' AS DateTime), CAST(N'2017-04-18 00:00:00.000' AS DateTime))
GO
INSERT [dbo].[Revision] ([ID], [ProjectID], [Drawing ID], [Revision], [SDate], [EDate]) VALUES (120, 23, 27, N'0', CAST(N'2017-04-18 00:00:00.000' AS DateTime), CAST(N'2017-04-18 00:00:00.000' AS DateTime))
GO
INSERT [dbo].[Revision] ([ID], [ProjectID], [Drawing ID], [Revision], [SDate], [EDate]) VALUES (121, 23, 28, N'0', NULL, CAST(N'2017-04-18 00:00:00.000' AS DateTime))
GO
INSERT [dbo].[Revision] ([ID], [ProjectID], [Drawing ID], [Revision], [SDate], [EDate]) VALUES (122, 23, 29, N'0', CAST(N'2017-04-18 00:00:00.000' AS DateTime), CAST(N'2017-04-18 00:00:00.000' AS DateTime))
GO
INSERT [dbo].[Revision] ([ID], [ProjectID], [Drawing ID], [Revision], [SDate], [EDate]) VALUES (123, 23, 30, N'0', NULL, CAST(N'2017-04-18 00:00:00.000' AS DateTime))
GO
INSERT [dbo].[Revision] ([ID], [ProjectID], [Drawing ID], [Revision], [SDate], [EDate]) VALUES (124, 26, 31, N'0', CAST(N'2017-04-18 00:00:00.000' AS DateTime), CAST(N'2017-04-18 00:00:00.000' AS DateTime))
GO
INSERT [dbo].[Revision] ([ID], [ProjectID], [Drawing ID], [Revision], [SDate], [EDate]) VALUES (125, 26, 32, N'0', NULL, CAST(N'2017-04-18 00:00:00.000' AS DateTime))
GO
INSERT [dbo].[Revision] ([ID], [ProjectID], [Drawing ID], [Revision], [SDate], [EDate]) VALUES (126, 26, 33, N'0', CAST(N'2017-04-18 00:00:00.000' AS DateTime), CAST(N'2017-04-18 00:00:00.000' AS DateTime))
GO
INSERT [dbo].[Revision] ([ID], [ProjectID], [Drawing ID], [Revision], [SDate], [EDate]) VALUES (127, 26, 34, N'0', CAST(N'2017-04-18 00:00:00.000' AS DateTime), CAST(N'2017-04-18 00:00:00.000' AS DateTime))
GO
INSERT [dbo].[Revision] ([ID], [ProjectID], [Drawing ID], [Revision], [SDate], [EDate]) VALUES (128, 26, 35, N'0', CAST(N'2017-04-18 00:00:00.000' AS DateTime), CAST(N'2017-04-18 00:00:00.000' AS DateTime))
GO
INSERT [dbo].[Revision] ([ID], [ProjectID], [Drawing ID], [Revision], [SDate], [EDate]) VALUES (129, 2, 36, N'0', CAST(N'2017-04-18 00:00:00.000' AS DateTime), CAST(N'2017-04-18 00:00:00.000' AS DateTime))
GO
INSERT [dbo].[Revision] ([ID], [ProjectID], [Drawing ID], [Revision], [SDate], [EDate]) VALUES (130, 26, 37, N'0', CAST(N'2017-04-18 00:00:00.000' AS DateTime), NULL)
GO
INSERT [dbo].[Revision] ([ID], [ProjectID], [Drawing ID], [Revision], [SDate], [EDate]) VALUES (131, 2, 38, N'0', NULL, CAST(N'2017-04-18 00:00:00.000' AS DateTime))
GO
INSERT [dbo].[Revision] ([ID], [ProjectID], [Drawing ID], [Revision], [SDate], [EDate]) VALUES (132, 23, 39, N'0', CAST(N'2017-04-18 00:00:00.000' AS DateTime), NULL)
GO
SET IDENTITY_INSERT [dbo].[Revision] OFF
GO
i need a query which will return one row per drawing with latest 4 issues which is grouped by desc of RevID ( Revision Table ) then desc by ISsue ID ( Issue Table ) it needs to select top 4 records for each drawings and all the four records are to be combined together with pivot as like,
DNo | ISsue1 | Issue2 | Issue3 | Issue4 | SDate1 | SDate2 | SDate3 | Sdate4 | EDate1 | EDate2 | EDate3 | EDate4 | Total1 | Total2 | Total3 | Total4
I have tried something as follows,
CREATE FUNCTION [dbo].[Dwg_logQuery_Test](#ProjectID int)
RETURNS TABLE
as
return
(
SELECT ID,Number,
Issue1,
Issue2,
Issue3,
Issue4,
Tonnage1,
Tonnage2,
Tonnage3,
Tonnage4,
SubmittedDate1,
SubmittedDate2,
SubmittedDate3,
SubmittedDate4,
EORReturnDate1,
EORReturnDate2,
EORReturnDate3,
EORReturnDate4,
Revision1,
Revision2,
Revision3,
Revision4
FROM (SELECT ID,
[Issue Number],
[Number],
[SubmittedDate],
[EORReturnDate],
Revision,
[Tonnage],
'Issue' + Cast(rn AS CHAR(10)) AS Issue,
'Tonnage' + Cast (rn AS CHAR(10)) AS TonnageHeader,
'SubmittedDate'+Cast (rn AS CHAR(10)) AS SubmittedDateHeader,
'EORReturnDate'+Cast (rn AS CHAR(10)) AS EORReturnDateHeader,
'Revision'+Cast (rn AS CHAR(10)) AS RevisionHeader
FROM (SELECT dwgReg.ID,
dwgIssue.[Issue Number],
Row_number()
OVER (
partition BY dwgIssue.[Drawing ID]
ORDER BY dwgReg.[ID], dwgRev.[Revision]
DESC, dwgIssue.[Issue Number] DESC)
AS rn,
dwgRev.Revision,
isnull(dwgIssue.weight1,0)+isnull(dwgIssue.weight2,0)+isnull(dwgIssue.weight3,0) as Tonnage,
dwgRev.[SDate] AS 'SubmittedDate',
dwgRev.[EDate] AS 'EORReturnDate'
,dwgReg.Number
FROM Issue dwgIssue
INNER JOIN Register dwgReg
ON dwgIssue.[Drawing ID] = dwgReg.ID
INNER JOIN Revision dwgRev
ON dwgRev.ID=dwgIssue.[Revision ID]
AND dwgIssue.[Drawing ID]=dwgReg.ID
where dwgReg.ProjectID=#ProjectID)dwgIssue
WHERE rn <= 4)dwgReg
PIVOT ( Max([Issue Number])
FOR Issue IN (Issue1,
Issue2,
Issue3,
Issue4)) pv
PIVOT (
MAX([Tonnage])
FOR TonnageHeader IN (Tonnage1, Tonnage2, Tonnage3, Tonnage4 )
) pv1
PIVOT ( Max([SubmittedDate])
FOR SubmittedDateHeader IN (SubmittedDate1,SubmittedDate2,SubmittedDate3,SubmittedDate4 )
) pv2
PIVOT ( Max([EORReturnDate])
FOR EORReturnDateHeader IN (EORReturnDate1,EORReturnDate2,EORReturnDate3,EORReturnDate4 )
) pv3
PIVOT ( Max([Revision])
FOR RevisionHeader IN (Revision1,Revision2,Revision3,Revision4 )
) pv4
)
GO
but i am failing in multiple PIVOT
So please some help me to resolve this.
Thanks in Advance.
Does this cover your desired result?
DECLARE #ProjectID int = 23;
WITH DataSrc as
(
SELECT ID, [Issue Number] INumber,
[Number] NNumber,
[SubmittedDate],
[EORReturnDate],
Revision,
[Tonnage],
'Issue' + Cast(rn AS CHAR(10)) AS Issue,
'Tonnage' + Cast (rn AS CHAR(10)) AS TonnageHeader,
'SubmittedDate'+Cast (rn AS CHAR(10)) AS SubmittedDateHeader,
'EORReturnDate'+Cast (rn AS CHAR(10)) AS EORReturnDateHeader,
'Revision'+Cast (rn AS CHAR(10)) AS RevisionHeader
FROM (SELECT dwgReg.ID,
dwgIssue.[Issue Number],
Row_number()
OVER (
partition BY dwgIssue.[Drawing ID]
ORDER BY dwgReg.[ID], dwgRev.[Revision]
DESC, dwgIssue.[Issue Number] DESC)
AS rn,
dwgRev.Revision,
isnull(dwgIssue.weight1,0)+isnull(dwgIssue.weight2,0)+isnull(dwgIssue.weight3,0) as Tonnage,
dwgRev.[SDate] AS 'SubmittedDate',
dwgRev.[EDate] AS 'EORReturnDate'
,dwgReg.Number
FROM Issue dwgIssue
INNER JOIN Register dwgReg
ON dwgIssue.[Drawing ID] = dwgReg.ID
INNER JOIN Revision dwgRev
ON dwgRev.ID=dwgIssue.[Revision ID]
AND dwgIssue.[Drawing ID]=dwgReg.ID
where dwgReg.ProjectID=#ProjectID)dwgIssue
WHERE rn <= 4
)
, DataSrc2 as
(
SELECT NNumber, INumber, Issue, Tonnage, TonnageHeader, SubmittedDate, SubmittedDateHeader,Revision, RevisionHeader
FROM DataSrc
)
SELECT * from DataSrc2
PIVOT (MAX(INumber) FOR Issue in ([Issue1], [Issue2], [Issue3], [Issue4])) pv1
PIVOT (MAX([Tonnage]) FOR TonnageHeader IN ([Tonnage1], [Tonnage2], [Tonnage3], [Tonnage4])) pv2
PIVOT (Max([SubmittedDate]) FOR SubmittedDateHeader IN ([SubmittedDate1],[SubmittedDate2],[SubmittedDate3],[SubmittedDate4])) pv3
PIVOT (Max([Revision]) FOR RevisionHeader IN ([Revision1], [Revision2], [Revision3], [Revision4])) pv4
GO
NNumber | Issue1 | Issue2 | Issue3 | Issue4 | Tonnage1 | Tonnage2 | Tonnage3 | Tonnage4 | SubmittedDate1 | SubmittedDate2 | SubmittedDate3 | SubmittedDate4 | Revision1 | Revision2 | Revision3 | Revision4
:------- | -----: | -----: | -----: | -----: | -------: | -------: | -------: | -------: | :------------------ | :------------------ | :------------------ | :------------------ | :-------- | :-------- | :-------- | :--------
1 | 2 | null | null | null | 12 | null | null | null | 21/04/2017 00:00:00 | null | null | null | 0 | null | null | null
100 | 1 | null | null | null | 6 | null | null | null | 18/04/2017 00:00:00 | null | null | null | 0 | null | null | null
11112 | 1 | null | null | null | 71 | null | null | null | null | null | null | null | 0 | null | null | null
2 | 1 | null | null | null | 6 | null | null | null | 18/04/2017 00:00:00 | null | null | null | 0 | null | null | null
23 | 1 | null | null | null | 68 | null | null | null | 18/04/2017 00:00:00 | null | null | null | 0 | null | null | null
3 | 1 | null | null | null | 4 | null | null | null | null | null | null | null | 0 | null | null | null
3333 | 2 | null | null | null | 48 | null | null | null | 18/04/2017 00:00:00 | null | null | null | 3 | null | null | null
4 | 1 | null | null | null | 45 | null | null | null | 18/04/2017 00:00:00 | null | null | null | 0 | null | null | null
555 | 1 | null | null | null | 5 | null | null | null | 18/04/2017 00:00:00 | null | null | null | 0 | null | null | null
666666 | 1 | null | null | null | 38 | null | null | null | 18/04/2017 00:00:00 | null | null | null | 0 | null | null | null
7 | 9 | null | null | null | 32 | null | null | null | null | null | null | null | 0 | null | null | null
77 | 1 | null | null | null | 487 | null | null | null | 18/04/2017 00:00:00 | null | null | null | 0 | null | null | null
88 | 1 | null | null | null | 6 | null | null | null | 18/04/2017 00:00:00 | null | null | null | 0 | null | null | null
983 | 1 | null | null | null | 76 | null | null | null | null | null | null | null | 0 | null | null | null
99 | 1 | null | null | null | 91 | null | null | null | 18/04/2017 00:00:00 | null | null | null | 0 | null | null | null
D1 | 2 | null | null | null | 42 | null | null | null | null | null | null | null | 0 | null | null | null
DT1 | null | null | null | 2 | null | null | null | 21 | null | null | null | 18/04/2017 00:00:00 | null | null | null | 1
DT1 | null | null | 2 | null | null | null | 24 | null | null | null | 18/04/2017 00:00:00 | null | null | null | 1 | null
DT1 | null | 3 | null | null | null | 24 | null | null | null | 18/04/2017 00:00:00 | null | null | null | 1 | null | null
DT1 | 1 | null | null | null | 24 | null | null | null | null | null | null | null | 2 | null | null | null
DTA05 | 1 | null | null | null | 56 | null | null | null | 18/04/2017 00:00:00 | null | null | null | 0 | null | null | null
Dwg1 | null | 1 | null | null | null | 10 | null | null | null | 18/04/2017 00:00:00 | null | null | null | 0 | null | null
Dwg1 | 2 | null | null | null | 4 | null | null | null | 18/04/2017 00:00:00 | null | null | null | 0 | null | null | null
Dwg12345 | 1 | null | null | null | 36 | null | null | null | null | null | null | null | 0 | null | null | null
dbfiddle here
Update
Ok, if you want to UNION and GROUP all pivot results you can do it on this way:
NOTE: I've do it the work with the two first PIVOT results, because you must manually add all returned columns with null values to all the PIVOT queries.
, DataSrc2 as
(
SELECT NNumber, INumber, Issue, Tonnage, TonnageHeader, SubmittedDate, SubmittedDateHeader,Revision, RevisionHeader
FROM DataSrc where NNumber = 'DT1'
)
, Pvt as
(
SELECT NNumber,SubmittedDate1,SubmittedDate2,SubmittedDate3,SubmittedDate4, null as Issue1, null as Issue2, null as Issue3, null Issue4
from (select NNumber, SubmittedDateHeader, SubmittedDate from DataSrc2) src
PIVOT (Max([SubmittedDate]) FOR SubmittedDateHeader IN ([SubmittedDate1],[SubmittedDate2],[SubmittedDate3],[SubmittedDate4])) pv3
UNION ALL
SELECT NNumber, null as SubmittedDate1, null as SubmittedDate2, null as SubmittedDate3, null as SubmittedDate4, Issue1, Issue2, Issue3, Issue4
from (select NNumber, Issue, INumber from DataSrc2) src
PIVOT (MAX(INumber) FOR Issue in ([Issue1], [Issue2], [Issue3], [Issue4])) pv1
)
select NNumber, MAX(SubmittedDate1) SD1, MAX(SubmittedDate2) SD2, MAX(SubmittedDate3) SD3, MAX(SubmittedDate4) SD4,
MAX(Issue1) Iss1, MAX(Issue2) Iss2, MAX(Issue3) Iss3, MAX(Issue4) Iss4
from Pvt
group by Nnumber;
And this is the result, but I'd recommend you to build a temporary table for this job.
NNumber | SD1 | SD2 | SD3 | SD4 | Iss1 | Iss2 | Iss3 | Iss4
:------ | :--- | :------------------ | :------------------ | :------------------ | ---: | ---: | ---: | ---:
DT1 | null | 18/04/2017 00:00:00 | 18/04/2017 00:00:00 | 18/04/2017 00:00:00 | 1 | 3 | 2 | 2
Warning: Null value is eliminated by an aggregate or other SET operation.
dbfiddle here
The following query uses the dwgIssue derived table from your query and pivots the results directly from it using conditional aggregation:
SELECT
ID,
Number,
Issue1 = MAX(CASE rn WHEN 1 THEN [Issue Number] END),
Issue2 = MAX(CASE rn WHEN 2 THEN [Issue Number] END),
Issue3 = MAX(CASE rn WHEN 3 THEN [Issue Number] END),
Issue4 = MAX(CASE rn WHEN 4 THEN [Issue Number] END),
Tonnage1 = MAX(CASE rn WHEN 1 THEN Tonnage END),
Tonnage2 = MAX(CASE rn WHEN 2 THEN Tonnage END),
Tonnage3 = MAX(CASE rn WHEN 3 THEN Tonnage END),
Tonnage4 = MAX(CASE rn WHEN 4 THEN Tonnage END),
SubmittedDate1 = MAX(CASE rn WHEN 1 THEN SubmittedDate END),
SubmittedDate2 = MAX(CASE rn WHEN 2 THEN SubmittedDate END),
SubmittedDate3 = MAX(CASE rn WHEN 3 THEN SubmittedDate END),
SubmittedDate4 = MAX(CASE rn WHEN 4 THEN SubmittedDate END),
EORReturnDate1 = MAX(CASE rn WHEN 1 THEN EORReturnDate END),
EORReturnDate2 = MAX(CASE rn WHEN 2 THEN EORReturnDate END),
EORReturnDate3 = MAX(CASE rn WHEN 3 THEN EORReturnDate END),
EORReturnDate4 = MAX(CASE rn WHEN 4 THEN EORReturnDate END),
Revision1 = MAX(CASE rn WHEN 1 THEN Revision END),
Revision2 = MAX(CASE rn WHEN 2 THEN Revision END),
Revision3 = MAX(CASE rn WHEN 3 THEN Revision END),
Revision4 = MAX(CASE rn WHEN 4 THEN Revision END)
FROM
(
SELECT
dwgReg.ID,
dwgIssue.[Issue Number],
rn = ROW_NUMBER() OVER (PARTITION BY dwgIssue.[Drawing ID]
ORDER BY dwgReg.[ID], dwgRev.[Revision] DESC, dwgIssue.[Issue Number] DESC),
dwgRev.Revision,
Tonnage = isnull(dwgIssue.weight1,0)+isnull(dwgIssue.weight2,0)+isnull(dwgIssue.weight3,0),
SubmittedDate = dwgRev.[SDate],
EORReturnDate = dwgRev.[EDate],
dwgReg.Number
FROM
dbo.Issue AS dwgIssue
INNER JOIN Register AS dwgReg
ON dwgIssue.[Drawing ID] = dwgReg.ID
INNER JOIN Revision dwgRev
ON dwgRev.ID=dwgIssue.[Revision ID]
AND dwgIssue.[Drawing ID]=dwgReg.ID
WHERE dwgReg.ProjectID=#ProjectID
) AS dwgIssue
WHERE
rn <= 4
;
Conditional aggregation may have become less common as a pivoting method after the PIVOT operator was introduced. I, however, still find it preferable when I need to pivot more than one data column at once, like in your case.

Resources