I have a table with 7 columns and I am trying to unpivot to rows.
All the Columns starting P1Start should be a new column in new Unpivoted table.
The table should like sample_Unpivot table: P1Start, P1End, P2Start, P2End ... should be a row value.
I have attached the insert script below.
Can you please provide me the code?
CREATE TABLE [dbo].[sample](
[id] [int] NULL,
[P1StartOrderDate] [datetime] NULL,
[P1StartReceiveDate] [datetime] NULL,
[P1EndOrderDate] [datetime] NULL,
[P1EndReceiveDate] [datetime] NULL,
[P2StartOrderDate] [datetime] NULL,
[P2StartReceiveDate] [datetime] NULL,
[P2EndOrderDate] [datetime] NULL,
[P2EndReceiveDate] [datetime] NULL
)
CREATE TABLE [dbo].[sample_unpivot](
[id] [int] NULL,
[Phase] [char](30) NULL,
[OrderDate] [datetime] NULL,
[ReceiveDate] [datetime] NULL
)
INSERT [dbo].[sample] ([id], [P1StartOrderDate], [P1StartReceiveDate], [P1EndOrderDate], [P1EndReceiveDate], [P2StartOrderDate], [P2StartReceiveDate], [P2EndOrderDate], [P2EndReceiveDate]) VALUES (1, CAST(N'2020-01-01 00:00:00.000' AS DateTime), CAST(N'2020-01-03 00:00:00.000' AS DateTime), CAST(N'2020-05-01 00:00:00.000' AS DateTime), CAST(N'2010-01-01 00:00:00.000' AS DateTime), CAST(N'2020-08-01 00:00:00.000' AS DateTime), CAST(N'2020-01-11 00:00:00.000' AS DateTime), CAST(N'2020-05-03 00:00:00.000' AS DateTime), CAST(N'2020-01-04 00:00:00.000' AS DateTime))
GO
INSERT [dbo].[sample_unpivot] ([id], [Phase], [OrderDate], [ReceiveDate]) VALUES (1, N'P1Start ', CAST(N'2020-01-01 00:00:00.000' AS DateTime), CAST(N'2020-01-03 00:00:00.000' AS DateTime))
GO
INSERT [dbo].[sample_unpivot] ([id], [Phase], [OrderDate], [ReceiveDate]) VALUES (1, N'P1End ', CAST(N'2020-05-01 00:00:00.000' AS DateTime), CAST(N'2020-01-01 00:00:00.000' AS DateTime))
GO
INSERT [dbo].[sample_unpivot] ([id], [Phase], [OrderDate], [ReceiveDate]) VALUES (1, N'P2Start ', CAST(N'2020-08-01 00:00:00.000' AS DateTime), CAST(N'2020-01-11 00:00:00.000' AS DateTime))
GO
INSERT [dbo].[sample_unpivot] ([id], [Phase], [OrderDate], [ReceiveDate]) VALUES (1, N'P2End ', CAST(N'2020-05-03 00:00:00.000' AS DateTime), CAST(N'2020-01-04 00:00:00.000' AS DateTime))
GO
Your table structure suggests SQL Server syntax, so i would do :
select s.id, ss.*
from sample s cross apply
( values ('P1Start', P1StartOrderDate, P1StartReceiveDate),
('P1End', P1EndOrderDate, P1EndReceiveDate),
('P2Start', P2StartOrderDate, P2StartReceiveDate),
('P2End', P2EndOrderDate, P2EndReceiveDate)
) ss(Phase, OrderDate, ReceiveDate);
Another way you can try to use UNION ALL to make it.
select id,'P1Start' Phase, P1StartOrderDate OrderDate, P1StartReceiveDate ReceiveDate
from sample s
UNION ALL
select id,'P1End' Phase,P1EndOrderDate, P1EndReceiveDate
from sample s
UNION ALL
select id,'P2Start' Phase, P2StartOrderDate, P2StartReceiveDate
from sample s
UNION ALL
select id,'P2End' Phase, P2EndOrderDate, P2EndReceiveDate
from sample s
sqlfiddle
Related
I have some data that looks like:
SET ANSI_NULLS ON
GO
SET QUOTED_IDENTIFIER ON
GO
CREATE TABLE [dbo].[tblMyLog]
(
[Pkid] [bigint] IDENTITY(1,1) NOT NULL,
[JobId] [int] NOT NULL,
[log_recorded] [datetime] NOT NULL,
[status] [nvarchar](max) NULL
)
GO
SET IDENTITY_INSERT [dbo].[tblMyLog] ON
GO
INSERT [dbo].[tblMyLog] ([Pkid], [JobId], [log_recorded], [status])
VALUES (1, 1, CAST(N'2021-06-29T15:35:09.917' AS DateTime), N'Started')
GO
INSERT [dbo].[tblMyLog] ([Pkid], [JobId], [log_recorded], [status])
VALUES (2, 1, CAST(N'2021-06-29T15:36:08.810' AS DateTime), N'Ended')
GO
INSERT [dbo].[tblMyLog] ([Pkid], [JobId], [log_recorded], [status])
VALUES (3, 2, CAST(N'2021-06-29T15:33:41.133' AS DateTime), N'Started')
GO
INSERT [dbo].[tblMyLog] ([Pkid], [JobId], [log_recorded], [status])
VALUES (4, 2, CAST(N'2021-06-29T15:35:09.917' AS DateTime), N'Ended')
GO
I would like to get in a format like so I do some joins to tblMyLog from another table:
JobId StartTime EndTime
------------------------------------------------------
1 2021-06-29 15:35:09.917 2021-06-29 15:36:08.810
2 2021-06-29 15:33:41.133 2021-06-29 15:35:09.917
but I can't figure out the pivot table syntax to do this.
Assuming a JobID can have multiple start/end times, we can use the window function sum() over() to create an ad-hoc group Grp
Example
Select JobID
,StartTime = min( case when status='Started' then log_recorded end)
,EndTime = max( case when status='Ended' then log_recorded end)
From (
Select *
,Grp = sum( case when status='Started' then 1 end) over ( partition by JobID order by log_recorded)
From [tblMyLog]
) A
Group By JobID,Grp
Results
JobID StartTime EndTime
1 2021-06-29 15:35:09.917 2021-06-29 15:36:08.810
2 2021-06-29 15:33:41.133 2021-06-29 15:35:09.917
I have a question that I dont know If there is solution to in sql. Made an small example
SET ANSI_NULLS ON
GO
SET QUOTED_IDENTIFIER ON
GO
CREATE TABLE [dbo].[Events](
[Id] [int] IDENTITY(1,1) NOT NULL,
[EventTime] [datetime] NOT NULL,
[Event] [nvarchar](50) NOT NULL
) ON [PRIMARY]
GO
/****** Object: Table [dbo].[Logging] Script Date: 2020-07-07 14:35:17 ******/
SET ANSI_NULLS ON
GO
SET QUOTED_IDENTIFIER ON
GO
CREATE TABLE [dbo].[Logging](
[Id] [int] IDENTITY(1,1) NOT NULL,
[FromTime] [datetime] NOT NULL,
[ToTime] [datetime] NOT NULL,
[Status] [nvarchar](50) NULL
) ON [PRIMARY]
GO
SET IDENTITY_INSERT [dbo].[Events] ON
GO
INSERT [dbo].[Events] ([Id], [EventTime], [Event]) VALUES (1, CAST(N'2020-07-07T14:14:00.000' AS DateTime), N'str')
GO
INSERT [dbo].[Events] ([Id], [EventTime], [Event]) VALUES (2, CAST(N'2020-07-07T07:01:00.000' AS DateTime), N'testcall')
GO
INSERT [dbo].[Events] ([Id], [EventTime], [Event]) VALUES (3, CAST(N'2020-07-07T15:22:00.000' AS DateTime), N'ipfail')
GO
SET IDENTITY_INSERT [dbo].[Events] OFF
GO
SET IDENTITY_INSERT [dbo].[Logging] ON
GO
INSERT [dbo].[Logging] ([Id], [FromTime], [ToTime], [Status]) VALUES (1, CAST(N'2020-07-07T01:00:00.000' AS DateTime), CAST(N'2020-07-07T13:00:00.000' AS DateTime), N'All well')
GO
INSERT [dbo].[Logging] ([Id], [FromTime], [ToTime], [Status]) VALUES (2, CAST(N'2020-07-07T13:01:00.000' AS DateTime), CAST(N'2020-07-07T15:00:00.000' AS DateTime), N'All well')
GO
INSERT [dbo].[Logging] ([Id], [FromTime], [ToTime], [Status]) VALUES (3, CAST(N'2020-07-07T15:33:00.000' AS DateTime), CAST(N'2020-07-07T20:00:00.000' AS DateTime), N'All well')
GO
INSERT [dbo].[Logging] ([Id], [FromTime], [ToTime], [Status]) VALUES (4, CAST(N'2020-07-07T20:01:00.000' AS DateTime), CAST(N'2020-07-07T23:00:00.000' AS DateTime), N'All well')
GO
SET IDENTITY_INSERT [dbo].[Logging] OFF
GO
When doing an inner join of this table I want to find the Event in Event table that doesn't have a matching timeframe in Logging. The timeframe that's missing is 15:00 to 15:33 and the event that happens that happens is the Event with ID 3. Everything is measured in minutes. Is this possible? Or have to do it some other way?
If I understand correctly, you want not exists:
select l.*
from logging l
where not exists (select 1
from events e
where e.eventtime >= l.fromtime and
e.eventtime <= l.totime
);
However, this returns two timeframes -- ones with ids 3 and 4.
Here is a db<>fiddle.
I have a SQL table which holds transactions of products and cumulative total of packed products each minute from a machine.
I'm trying to summarize this table (in a sql query) into product runs.
The start times are denoted by the first record of the product where cumulative is > 0
The finish times are denoted by the last record of the product run - but ignore if the cumulative didn't change (i.e. no more products where actually packed)
As in the example table, the same product can have multiple runs a day but the summary should show each individual product run.
Create table and insert data....
CREATE TABLE [dbo].[Test](
[ID] [nchar](10) NULL,
[Product] [varchar](20) NULL,
[datetime] [datetime] NULL,
[Cumulative_Packed] [int] NULL
) ON [PRIMARY]
GO
INSERT [dbo].[Test] ([ID], [Product], [datetime], [Cumulative_Packed]) VALUES (N'1 ', N'Item1', CAST(N'2019-11-14T14:15:00.000' AS DateTime), 10)
INSERT [dbo].[Test] ([ID], [Product], [datetime], [Cumulative_Packed]) VALUES (N'2 ', N'Item1', CAST(N'2019-11-14T14:16:00.000' AS DateTime), 22)
INSERT [dbo].[Test] ([ID], [Product], [datetime], [Cumulative_Packed]) VALUES (N'3 ', N'Item1', CAST(N'2019-11-14T14:17:00.000' AS DateTime), 35)
INSERT [dbo].[Test] ([ID], [Product], [datetime], [Cumulative_Packed]) VALUES (N'4 ', N'Item1', CAST(N'2019-11-14T14:18:00.000' AS DateTime), 40)
INSERT [dbo].[Test] ([ID], [Product], [datetime], [Cumulative_Packed]) VALUES (N'5 ', N'Item1', CAST(N'2019-11-14T14:19:00.000' AS DateTime), 40)
INSERT [dbo].[Test] ([ID], [Product], [datetime], [Cumulative_Packed]) VALUES (N'6 ', N'Item1', CAST(N'2019-11-14T14:20:00.000' AS DateTime), 40)
INSERT [dbo].[Test] ([ID], [Product], [datetime], [Cumulative_Packed]) VALUES (N'7 ', N'Item1', CAST(N'2019-11-14T14:21:00.000' AS DateTime), 0)
INSERT [dbo].[Test] ([ID], [Product], [datetime], [Cumulative_Packed]) VALUES (N'8 ', N'Item1', CAST(N'2019-11-14T14:22:00.000' AS DateTime), 0)
INSERT [dbo].[Test] ([ID], [Product], [datetime], [Cumulative_Packed]) VALUES (N'9 ', N'Item1', CAST(N'2019-11-14T14:23:00.000' AS DateTime), 0)
INSERT [dbo].[Test] ([ID], [Product], [datetime], [Cumulative_Packed]) VALUES (N'10 ', N'Item2', CAST(N'2019-11-14T14:24:00.000' AS DateTime), 12)
INSERT [dbo].[Test] ([ID], [Product], [datetime], [Cumulative_Packed]) VALUES (N'11 ', N'Item2', CAST(N'2019-11-14T14:25:00.000' AS DateTime), 18)
INSERT [dbo].[Test] ([ID], [Product], [datetime], [Cumulative_Packed]) VALUES (N'12 ', N'Item2', CAST(N'2019-11-14T14:26:00.000' AS DateTime), 18)
INSERT [dbo].[Test] ([ID], [Product], [datetime], [Cumulative_Packed]) VALUES (N'13 ', N'Item2', CAST(N'2019-11-14T14:27:00.000' AS DateTime), 22)
INSERT [dbo].[Test] ([ID], [Product], [datetime], [Cumulative_Packed]) VALUES (N'14 ', N'Item2', CAST(N'2019-11-14T14:28:00.000' AS DateTime), 22)
INSERT [dbo].[Test] ([ID], [Product], [datetime], [Cumulative_Packed]) VALUES (N'15 ', N'Item1', CAST(N'2019-11-14T14:29:00.000' AS DateTime), 0)
INSERT [dbo].[Test] ([ID], [Product], [datetime], [Cumulative_Packed]) VALUES (N'16 ', N'Item1', CAST(N'2019-11-14T14:30:00.000' AS DateTime), 7)
INSERT [dbo].[Test] ([ID], [Product], [datetime], [Cumulative_Packed]) VALUES (N'17 ', N'Item1', CAST(N'2019-11-14T14:31:00.000' AS DateTime), 30)
INSERT [dbo].[Test] ([ID], [Product], [datetime], [Cumulative_Packed]) VALUES (N'18 ', N'Item1', CAST(N'2019-11-14T14:32:00.000' AS DateTime), 38)
INSERT [dbo].[Test] ([ID], [Product], [datetime], [Cumulative_Packed]) VALUES (N'19 ', N'Item1', CAST(N'2019-11-14T14:33:00.000' AS DateTime), 38)
INSERT [dbo].[Test] ([ID], [Product], [datetime], [Cumulative_Packed]) VALUES (N'20 ', N'Item1', CAST(N'2019-11-14T14:34:00.000' AS DateTime), 38)
INSERT [dbo].[Test] ([ID], [Product], [datetime], [Cumulative_Packed]) VALUES (N'21 ', N'Item1', CAST(N'2019-11-14T14:35:00.000' AS DateTime), 38)
I think you want lead() and lag() to identify the starts and stops. For your data:
select row_number() over (order by min(datetime)) as run_order,
product, min(datetime), max(datetime),
max(cumulative_packed)
from (select t.*,
sum(case when prev_cp = 0 or prev_cp is null then 1 else 0 end) over (partition by product order by datetime) as rungrp
from (select t.*,
lag(cumulative_packed) over (partition by product order by datetime) as prev_cp,
lead(cumulative_packed) over (partition by product order by datetime) as next_cp
from t
) t
where (prev_cp = 0 or prev_cp is null) or -- is start
(next_cp = cp or next_cp = 0 or next_cp is null) -- is stop
) t
group by rungrp, product
order by rungrp, product, min(datetime);
EDIT:
Your data seems to require duplicate removal. From what I follow, you just need the first time the value changes. This can be handled with one additional layer of subqueries:
select row_number() over (order by min(datetime)) as run_order,
product, min(datetime), max(datetime),
max(cumulative_packed)
from (select t.*,
sum(case when prev_cp = 0 or prev_cp is null then 1 else 0 end) over (partition by product order by datetime) as rungrp
from (select t.*,
lag(cumulative_packed) over (partition by product order by datetime) as prev_cp,
lead(cumulative_packed) over (partition by product order by datetime) as next_cp
from (select t.*,
lag(cumulative_packed) over (partition by product order by datetime) as first_prev_cp
from t
) t
where first_prev_cp is null or first_prev_cp <> cumulative_packed
) t
where (prev_cp = 0 or prev_cp is null) or -- is start
(next_cp = cp or next_cp = 0 or next_cp is null) -- is stop
) t
group by rungrp, product
order by rungrp, product, min(datetime);
I have a simple view created in VS 2017. Here it is:
CREATE VIEW [dbo].[ApplicantStat]
AS SELECT ISNULL(CONVERT(VARCHAR(50), NEWID()), '') AS ID,
ISNULL(AVG(ApplicationTime), 0) AS 'AvgApplicationTime',
ISNULL(AVG(ResponseTime), 0) AS 'AvgResponseTime',
ISNULL(CAST(COUNT(CASE WHEN [IsAccepted] = 1 THEN 1 END) / COUNT(CASE WHEN [IsValid] = 1 THEN 1 END) AS float), 0) AS 'PctAccepted'
FROM [Application]
WHERE CreatedOn BETWEEN CAST(GETDATE()-30 AS date) AND CAST(GETDATE()-1 AS date)
As you can see, it gets data between 2 dates and does some simple aggregation.
The idea of the cast is that I want to ignore the time and get everything for the date range regardless - so as of today, 15th Mar, I would it to fetch everything for 14th March 00:00:00 - 23:59:59 and 29 days previous.
This does not happen - it picks up 3 rows (13th) - it should pick up all 5 rows. And yes, my system date is currently 15/03/2018 14:44 (UK time).
Here's, the table and data:
CREATE TABLE [dbo].[Application] (
[Id] INT NOT NULL,
[ApplicantId] INT NOT NULL,
[LoanAmount] INT NOT NULL,
[LoanTerm] SMALLINT NOT NULL,
[EmailAddress] VARCHAR (254) NOT NULL,
[MobilePhone] VARCHAR (11) NOT NULL,
[House] VARCHAR (25) NOT NULL,
[Street] VARCHAR (50) NOT NULL,
[TownCity] VARCHAR (50) NOT NULL,
[Postcode] VARCHAR (7) NOT NULL,
[IpAddress] VARCHAR (39) NOT NULL,
[IsValid] BIT NOT NULL,
[IsAccepted] BIT NOT NULL,
[Commission] DECIMAL (9, 2) NOT NULL,
[Processors] VARCHAR (500) NOT NULL,
[ResponseTime] SMALLINT NOT NULL,
[ApplicationTime] SMALLINT NOT NULL,
[CreatedOn] DATETIME NOT NULL,
PRIMARY KEY CLUSTERED ([Id] ASC)
);
INSERT INTO [dbo].[Application] ([Id], [ApplicantId], [LoanAmount], [LoanTerm], [EmailAddress], [MobilePhone], [House], [Street], [TownCity], [Postcode], [IpAddress], [IsValid], [IsAccepted], [Commission], [Processors], [ResponseTime], [ApplicationTime], [CreatedOn]) VALUES (1, 1, 300, 3, N'john.doe#tmail.com', N'07957000000', N'1', N'Acacia Avenue', N'Suburbia', N'SB1 2RB', N'100.100.100.100', 1, 1, CAST(3.20 AS Decimal(9, 2)), N'1,2,3,4,5', 90, 600, N'2018-03-13 08:00:00')
INSERT INTO [dbo].[Application] ([Id], [ApplicantId], [LoanAmount], [LoanTerm], [EmailAddress], [MobilePhone], [House], [Street], [TownCity], [Postcode], [IpAddress], [IsValid], [IsAccepted], [Commission], [Processors], [ResponseTime], [ApplicationTime], [CreatedOn]) VALUES (2, 2, 500, 12, N'a#b.com', N'0', N'1', N'a', N's', N's', N'1', 0, 1, CAST(5.00 AS Decimal(9, 2)), N'1', 60, 300, N'2018-03-14 16:00:00')
INSERT INTO [dbo].[Application] ([Id], [ApplicantId], [LoanAmount], [LoanTerm], [EmailAddress], [MobilePhone], [House], [Street], [TownCity], [Postcode], [IpAddress], [IsValid], [IsAccepted], [Commission], [Processors], [ResponseTime], [ApplicationTime], [CreatedOn]) VALUES (3, 3, 1000, 6, N'a#b.com', N'0', N'1', N'a', N's', N's', N'1', 1, 1, CAST(7.00 AS Decimal(9, 2)), N'1', 75, 360, N'2018-03-13 10:00:00')
INSERT INTO [dbo].[Application] ([Id], [ApplicantId], [LoanAmount], [LoanTerm], [EmailAddress], [MobilePhone], [House], [Street], [TownCity], [Postcode], [IpAddress], [IsValid], [IsAccepted], [Commission], [Processors], [ResponseTime], [ApplicationTime], [CreatedOn]) VALUES (4, 4, 2000, 24, N'a#b.com', N'0', N'1', N'a', N's', N's', N'1', 1, 1, CAST(20.00 AS Decimal(9, 2)), N'1', 30, 365, N'2018-03-14 11:00:00')
INSERT INTO [dbo].[Application] ([Id], [ApplicantId], [LoanAmount], [LoanTerm], [EmailAddress], [MobilePhone], [House], [Street], [TownCity], [Postcode], [IpAddress], [IsValid], [IsAccepted], [Commission], [Processors], [ResponseTime], [ApplicationTime], [CreatedOn]) VALUES (5, 5, 3000, 18, N'a#b.com', N'0', N'1', N'a', N's', N's', N'1', 1, 1, CAST(40.00 AS Decimal(9, 2)), N'1', 45, 330, N'2018-03-13 12:00:00')
Try this out:
WHERE
CreatedOn >= CAST(GETDATE()-30 AS date) AND
CreatedOn < CAST(GETDATE() AS date)
The problem is your converting to date the day before today.
You can CAST your CreatedOn field as DATE to remove the time portion, which is getting in your way here...
Perhaps
WHERE CAST(CreatedOn AS DATE) BETWEEN CAST(GETDATE()-30 AS date) AND CAST(GETDATE()-1 AS date)
BUT - CASTing a field in the WHERE expression may make it non SARGable. See here. So avoid this solution for large or production environments unless you know the expression will be SARGable. Use only as a test to refine your logic and options. (Even if there is no explicit index on CreatedOn - it may still suffer as SQL builds its own indexes all the time if no index exists explicitly.
Always worth confirming whether it is SARGable so you know for sure.)
To see what is happening - view your values in your SELECT - just to get an idea of what is working
For example:
SELECT TOP 1000
CreatedOn
,CAST(GETDATE()-30 AS date)
,CAST(GETDATE()-1 AS date)
FROM [Application]
Or see the other options for removing time values from datatime fields here
as you may want to coerce or round the time value instead
Instead of trying to ignore the time value, just make sure that your search terms are accurate for it. Also, don't blindly add things like ISNULL to every column. Spend a few seconds thinking if it's relevant or not. NEWID() for example, is never going to return a NULL value to you. Adding that kind of code is poor programming which will lead to less legible code.
Here's how I would write it to account for the time portions:
CREATE VIEW dbo.ApplicantStat
AS
SELECT
CONVERT(VARCHAR(50), NEWID()) AS ID,
COALESCE(AVG(ApplicationTime), 0) AS AvgApplicationTime,
COALESCE(AVG(ResponseTime), 0) AS AvgResponseTime,
COALESCE(CAST(COUNT(CASE WHEN [IsAccepted] = 1 THEN 1 END) / COUNT(CASE WHEN [IsValid] = 1 THEN 1 END) AS float), 0) AS PctAccepted
FROM
dbo.Application
WHERE
CreatedOn >= DATEADD(DAY, -30, CAST(GETDATE() AS DATE)) AND
CreatedOn < CAST(GETDATE() AS DATE)
I have a simple table
CREATE TABLE [dbo].[Tooling](
[Id] [int] IDENTITY(1,1) NOT NULL,
[Name] [nvarchar](50) NOT NULL,
[Status] [int] NOT NULL,
[DateFinished] [datetime] NULL,
[Tooling] [nvarchar](50) NULL,
[Updated] [datetime] NULL,
) ON [PRIMARY]
with following values
SET IDENTITY_INSERT [dbo].[Tooling] ON
GO
INSERT [dbo].[Tooling] ([Id], [Name], [Status], [DateFinished], [Tooling], [Updated]) VALUES (1, N'Large', 0, NULL, NULL, CAST(N'2015-05-05 00:00:00.000' AS DateTime))
GO
INSERT [dbo].[Tooling] ([Id], [Name], [Status], [DateFinished], [Tooling], [Updated]) VALUES (2, N'Large', 1, NULL, N'1', CAST(N'2015-05-10 00:00:00.000' AS DateTime))
GO
INSERT [dbo].[Tooling] ([Id], [Name], [Status], [DateFinished], [Tooling], [Updated]) VALUES (3, N'Small', 0, NULL, N'2', CAST(N'2015-05-11 00:00:00.000' AS DateTime))
GO
INSERT [dbo].[Tooling] ([Id], [Name], [Status], [DateFinished], [Tooling], [Updated]) VALUES (4, N'Large', 2, NULL, N'1', CAST(N'2015-05-12 00:00:00.000' AS DateTime))
GO
INSERT [dbo].[Tooling] ([Id], [Name], [Status], [DateFinished], [Tooling], [Updated]) VALUES (5, N'Large', 2, NULL, N'2', CAST(N'2015-05-12 00:00:00.000' AS DateTime))
GO
INSERT [dbo].[Tooling] ([Id], [Name], [Status], [DateFinished], [Tooling], [Updated]) VALUES (6, N'Large', 1, NULL, N'1', CAST(N'2015-05-13 00:00:00.000' AS DateTime))
GO
INSERT [dbo].[Tooling] ([Id], [Name], [Status], [DateFinished], [Tooling], [Updated]) VALUES (7, N'Large', 1, NULL, N'2', CAST(N'2015-05-14 00:00:00.000' AS DateTime))
GO
INSERT [dbo].[Tooling] ([Id], [Name], [Status], [DateFinished], [Tooling], [Updated]) VALUES (8, N'Small', 1, CAST(N'2015-05-15 00:00:00.000' AS DateTime), N'2', CAST(N'2015-05-15 00:00:00.000' AS DateTime))
GO
SET IDENTITY_INSERT [dbo].[Tooling] OFF
I want to create a view of the table that looks like its an entirely own table.
SELECT Id, Name, Status, DateFinished
FROM Tooling t order by t.id
If i run that I would like that the records with id 5 and 7 should be excluded since they dont change in the selected set from the previous row.
I had an idea to solve this by using, ROW_NUMBER() over partition
And by using group by but thats seems to incorrect(Could not to get it to work at all)
How do I group it when the should be grouped on that the value change and not the value it contains?
Any suggestions to solve this?
My end goal is also try to convert this to linq to entities...
OK, supposing it's ordered by id
select *
from (
select *, rng = row_number() over (partition by grp order by id)
from (
select *, grp = row_number() over (order by id) - row_number() over (partition by Name, Status, DateFinished order by id)
from tooling ) g
) gn
where rng = 1
order by id