MS SQL Change expiry date when inserting value with same name - sql-server

I have a question relating to trigger. I have a table (FoodPrice) with different columns like a start date and an expiry date.
Let's consider that I would like to add a price that could expire like a sale, I would like (ON INSERT) to set the ExpiryDate of the previous value to the current date:
Initial table
Food Value StartDate ExpiryDate
------ ---------- ---------- ----------
Carrot 25.5 24/12/2013 NULL
Apple 44.9 5/1/2014 NULL
Squash 25.6 12/3/2013 NULL
New table with inserted rows:
Food Value StartDate ExpiryDate
------ ---------- ---------- ----------
Carrot 25.5 24/12/2013 28/4/2014
Apple 44.9 5/1/2014 NULL
Squash 25.6 12/3/2013 28/4/2014
Carrot 24 28/4/2014 NULL
Squash 22 28/4/2014 NULL
Dupplicate values for Food column is not a big deal but is it possible to create a trigger to solve this problem ? Thank you !

Here's the code:
-- the table and sample data
create table FoodPrice (
Food varchar(10),
Value decimal(5,2),
StartDate date,
ExpiryDate date
);
go
insert FoodPrice values
('Carrot', 20, '20131124' , '20131224'),
('Apple' , 40, '20140101' , '20140105'),
('Squash', 25, '20130301' , '20130312'),
('Carrot', 25.5, '20131224' , NULL),
('Apple' , 44.9, '20140105' , NULL),
('Squash', 25.6, '20130312' , NULL)
go
-- the trigger
create trigger trFoodPrice_insert
on FoodPrice
after insert
as
;with x as (
select fp.food, fp.startdate as fp_startdate, fp.expirydate as fp_expirydate,
ins.startdate as ins_startdate, ins.expirydate as ins_expirydate,
row_number() over(partition by fp.food order by fp.startdate) as rn
from ins
inner join foodprice fp on ins.food=fp.food
and fp.startdate < ins.startdate
and fp.expirydate is null
),
y as (
select *
from x
where rn = 1
)
--select * from y
update y
set fp_expirydate = ins_startdate
go
-- let's test it
insert foodprice values
('Carrot', 24, '20140428', null),
('Squash', 22, '20140428', null)
go
select * from
foodprice
order by food, startdate
As always, I'm a big fan of first testing the select before the actual update, hence the CTE.

Related

Query Temporal Table and Combine Rows

Let's say I have a temporal table called ProductDetails that using below query return some historical data.
SELECT * FROM ProductDetails
FOR system_time
BETWEEN '1900-01-01 00:00:00' AND '9999-12-31 00:00:00'
WHERE ProductID = 8
ID ProductID(FK) Attribute Value SysStartTime SysEndTime
-- ------------- --------- ----- ------------------- ----------
1 8 Size S 2020-07-06 05:00:00 9999-12-31 23:59:59
2 8 Color Blue 2020-07-06 05:00:01 2020-07-09 11:11:11
2 8 Color Green 2020-07-09 11:11:11 9999-12-31 23:59:59
This means when product with ID = 8 was created at 2020-07-06 05:00:00, 2 attributes were added, and then later one of records was edited to change from "Blue" to "Green". Notice that SysStartTime for second row has 1 second difference when they were saved.
Now I need to write a query to have below results. Basically, it is attribute values in different snapshots of time when changes occurred. Time is down to minute.
Start Time End Time Attributes Values
---------------- ---------------- -----------------
2020-07-06 05:00 2020-07-09 11:11 Size = S, Color = Blue
2020-07-09 11:11 NULL Size = S, Color = Green
How can I achieve that? Each product might have different attributes, but the query is for one product at a time.
Below is a solution that formats your data in one query. Performance is not an issue with a small data set of 4 rows (I added a row to your example), but my guess is that this will not be fast for millions of records.
The solution provided here generates different data sets in the form of common table expressions (CTE) and uses some techniques from other StackOverflow answers to remove the seconds and concatenate the row values. Plus a cross apply at the end.
The approach can be described in steps that correspond with the consecutive CTE's / joins:
Create a set of attributes for each product.
Create a set of period start moments for each product (leaving out the seconds).
Combine the attributes for each product with each period and look for the appropriate value.
Use some XML functions to format the attributes values in a single row.
Use cross apply to fetch the period end.
Full solution:
-- sample data
declare #data table
(
ID int,
ProductId int,
Attribute nvarchar(10),
Value nvarchar(10),
SysStartTime datetime2(0),
SysEndTime datetime2(0)
);
insert into #data (ID, ProductId, Attribute, Value, SysStartTime, SysEndTime) values
(1, 8, 'Size', 'S', '2020-07-06 05:00:00', '9999-12-31 23:59:59'),
(2, 8, 'Color', 'Blue', '2020-07-06 05:00:01', '2020-07-09 11:11:11'),
(2, 8, 'Color', 'Green', '2020-07-09 11:11:11', '9999-12-31 23:59:59'),
(2, 8, 'Weight', 'Light', '2020-07-10 10:11:12', '9999-12-31 23:59:59'); -- additional data to have extra attribute not available from start
-- solution
with prodAttrib as -- attributes per product
(
select d.ProductId, d.Attribute
from #data d
group by d.ProductId, d.Attribute
),
prodPeriod as -- periods per product
(
select d.ProductId,
dateadd(minute, datediff(minute, 0, d.SysStartTime), 0) as 'SysStartTimeNS' -- start time No Seconds
from #data d
group by ProductId, dateadd(minute, datediff(minute, 0, d.SysStartTime), 0)
),
prodResult as -- attribute value per period per product
(
select pp.ProductId,
convert(nvarchar(16), pp.SysStartTimeNS, 120) as 'FromDateTime',
convert(nvarchar(16), coalesce(pe.SysEndTime, '9999-12-31 23:59:59'), 120) as 'ToDateTime',
pa.Attribute,
av.Value
from prodPeriod pp
join prodAttrib pa
on pa.ProductId = pp.ProductId
outer apply ( select top 1 d.Value
from #data d
where d.ProductId = pp.ProductId
and d.Attribute = pa.Attribute
and dateadd(minute, datediff(minute, 0, d.SysStartTime), 0) <= pp.SysStartTimeNS
order by d.SysStartTime desc ) av -- attribute values per product
outer apply ( select top 1 dateadd(second, -1, d.SysStartTime) as 'SysEndTime'
from #data d
where d.ProductId = pp.ProductId
and dateadd(minute, datediff(minute, 0, d.SysStartTime), 0) > pp.SysStartTimeNS
order by d.SysStartTime ) pe -- period end
),
prodResultFormat as -- concatenate attribute values per period
(
select pp.ProductId,
convert(nvarchar(16), pp.SysStartTimeNS, 120) as 'FromDateTime',
(
select pr.Attribute + ' = ' + coalesce(pr.Value,'') + ', ' as [text()]
from prodResult pr
where pr.ProductId = pp.ProductId
and pr.FromDateTime = convert(nvarchar(16), pp.SysStartTimeNS, 120)
order by pr.Attribute
for xml path('')
) as 'Attributes'
from prodPeriod pp
)
select prf.ProductId,
prf.FromDateTime,
x.ToDateTime,
left(prf.Attributes, len(prf.Attributes)-1) as 'Attributes'
from prodResultFormat prf
cross apply ( select top 1 pr.ToDateTime
from prodResult pr
where pr.ProductId = prf.ProductId
and pr.FromDateTime = prf.FromDateTime ) x
order by prf.ProductId, prf.FromDateTime;
Result for extended example data:
ProductId FromDateTime ToDateTime Attributes
----------- ---------------- ---------------- ----------------------------------------
8 2020-07-06 05:00 2020-07-09 11:11 Color = Blue, Size = S, Weight =
8 2020-07-09 11:11 2020-07-10 10:11 Color = Green, Size = S, Weight =
8 2020-07-10 10:11 9999-12-31 23:59 Color = Green, Size = S, Weight = Light
P.S. replace x.EndDateTime with case when x.ToDateTime = '9999-12-31 23:59' then NULL else x.ToDateTime end as 'ToDateTime' if you really need the NULL values.

SQL Server Recursive CTE Many-to-Many "Best Match"

I've got a many-to-many relationship. I want to identify the best matches.
A match = #firstParty.StartDate between #thirdParty.MatchRangeStart and #thirdParty.MatchRangeEnd.
best match = the earliest record in #thirdParty that's not a "best match" for an earlier record in #firstParty.
I can't use a loop, for performance reasons. So, I think a recursive CTE will be needed.
Here's my desired output:
FirstPartyId StartDate ThirdPartyId ThirdPartyStartDate
------------ ---------- ------------ -------------------
1 2016-01-01 1 2016-01-10
2 2016-01-02 2 2016-01-11
3 2016-01-03 3 2016-01-12
Script:
declare #firstParty table
(
FirstPartyId integer identity,
StartDate date,
ThirdPartyId integer
);
insert into #firstParty (StartDate)
values
('01/01/2016'), ('01/02/2016'), ('01/03/2016'), ('01/04/2017'), ('01/05/2017');
/*
dates in #firstParty and #thirdParty are not guaranteed to be unique
in all scenarios
*/
declare #thirdParty table
(
ThirdPartyId integer identity,
StartDate date,
MatchRangeStart date,
MatchRangeEnd date
);
insert into #thirdParty (StartDate)
values
('01/10/2016'), ('01/11/2016'), ('01/12/2016');
update #thirdParty set MatchRangeStart = dateadd(d, -31, StartDate), MatchRangeEnd = dateadd(d, 31, StartDate);
declare #matches table
(
Id integer identity,
FirstPartyId integer,
FirstPartyStartDate date,
ThirdPartyId integer,
ThirdPartyStartDate date
);
insert into #matches (FirstPartyId, FirstPartyStartDate, ThirdPartyId, ThirdPartyStartDate)
select
fp.FirstPartyId,
fp.StartDate,
tp.ThirdPartyId,
tp.StartDate
from
#thirdParty tp
join #firstParty fp on
fp.StartDate between tp.MatchRangeStart and tp.MatchRangeEnd
;
select
fp.FirstPartyId,
fp.StartDate,
tpm.ThirdPartyId,
tpm.ThirdPartyStartDate
from
#firstParty fp
cross apply
(
select top 1
m.*
from
#matches m
where
fp.FirstPartyId = m.FirstPartyId
order by
m.ThirdPartyStartDate
) tpm;

SQL Server - Update Column with Handing Duplicate and Unique Rows Based Upon Timestamp

I'm working with SQL Server 2005 and looking to export some data off of a table I have. However, prior to do that I need to update a status column based upon a field called "VisitNumber", which can contain multiple entries same value entries. I have a table set up in the following manner. There are more columns to it, but I am just putting in what's relevant to my issue
ID Name MyReport VisitNumber DateTimeStamp Status
-- --------- -------- ----------- ----------------------- ------
1 Test John Test123 123 2014-01-01 05.00.00.000
2 Test John Test456 123 2014-01-01 07.00.00.000
3 Test Sue Test123 555 2014-01-02 08.00.00.000
4 Test Ann Test123 888 2014-01-02 09.00.00.000
5 Test Ann Test456 888 2014-01-02 10.00.00.000
6 Test Ann Test789 888 2014-01-02 11.00.00.000
Field Notes
ID column is a unique ID in incremental numbers
MyReport is a text value and can actually be thousands of characters. Shortened for simplicity. In my scenario the text would be completely different
Rest of fields are varchar
My Goal
I need to address putting in a status of "F" for two conditions:
* If there is only one VisitNumber, update the status column of "F"
* If there is more than one visit number, only put "F" for the one based upon the earliest timestamp. For the other ones, put in a status of "A"
So going back to my table, here is the expectation
ID Name MyReport VisitNumber DateTimeStamp Status
-- --------- -------- ----------- ----------------------- ------
1 Test John Test123 123 2014-01-01 05.00.00.000 F
2 Test John Test456 123 2014-01-01 07.00.00.000 A
3 Test Sue Test123 555 2014-01-02 08.00.00.000 F
4 Test Ann Test123 888 2014-01-02 09.00.00.000 F
5 Test Ann Test456 888 2014-01-02 10.00.00.000 A
6 Test Ann Test789 888 2014-01-02 11.00.00.000 A
I was thinking I could handle this by splitting each types of duplicates/triplicates+ (2,3,4,5). Then updating every other (or every 3,4,5 rows). Then delete those from the original table and combine them together to export the data in SSIS. But I am thinking there is a much more efficient way of handling it.
Any thoughts? I can accomplish this by updating the table directly in SQL for this status column and then export normally through SSIS. Or if there is some way I can manipulate the column for the exact conditions I need, I can do it all in SSIS. I am just not sure how to proceed with this.
WITH cte AS
(
SELECT *, ROW_NUMBER() OVER (PARTITION BY VisitNumber ORDER BY DateTimeStamp) rn from MyTable
)
UPDATE cte
SET [status] = (CASE WHEN rn = 1 THEN 'F' ELSE 'A' END)
I put together a test script to check the results. For your purposes, use the update statements and replace the temp table with your table name.
create table #temp1 (id int, [name] varchar(50), myreport varchar(50), visitnumber varchar(50), dts datetime, [status] varchar(1))
insert into #temp1 (id,[name],myreport,visitnumber, dts) values (1,'Test John','Test123','123','2014-01-01 05:00')
insert into #temp1 (id,[name],myreport,visitnumber, dts) values (2,'Test John','Test456','123','2014-01-01 07:00')
insert into #temp1 (id,[name],myreport,visitnumber, dts) values (3,'Test Sue','Test123','555','2014-01-01 08:00')
insert into #temp1 (id,[name],myreport,visitnumber, dts) values (4,'Test Ann','Test123','888','2014-01-01 09:00')
insert into #temp1 (id,[name],myreport,visitnumber, dts) values (5,'Test Ann','Test456','888','2014-01-01 10:00')
insert into #temp1 (id,[name],myreport,visitnumber, dts) values (6,'Test Ann','Test789','888','2014-01-01 11:00')
select * from #temp1;
update #temp1 set status = 'F'
where id in (
select id from #temp1 t1
join (select min(dts) as mindts, visitnumber
from #temp1
group by visitNumber) t2
on t1.visitnumber = t2.visitnumber
and t1.dts = t2.mindts)
update #temp1 set status = 'A'
where id not in (
select id from #temp1 t1
join (select min(dts) as mindts, visitnumber
from #temp1
group by visitNumber) t2
on t1.visitnumber = t2.visitnumber
and t1.dts = t2.mindts)
select * from #temp1;
drop table #temp1
Hope this helps

Checking next row in table is incremented by 1 minute in datetime column

I need to check alot of data in a Table to make sure my feed has not skipped anything.
Basically the table has the following columns
ID Datetime Price
The data in DateTime column is incremented by 1 minute in each successive row. I need to check the next row of the current one to see if is 1 minute above the one being queries in that specific context.
The query will probably need some sort of loop, then grab a copy of the next row and compare it to the datetime row of the current to make sure it is incremented by 1 minute.
I created a test-table to match your description, and inserted 100 rows with 1 minute between each row like this:
CREATE TABLE [Test] ([Id] int IDENTITY(1,1), [Date] datetime, [Price] int);
WITH [Tally] AS (
SELECT GETDATE() AS [Date]
UNION ALL
SELECT DATEADD(minute, -1, [Date]) FROM [Tally] WHERE [Date] > DATEADD(minute, -99, GETDATE())
)
INSERT INTO [Test] ([Date], [Price])
SELECT [Date], 123 AS [Price]
FROM [Tally]
Then i deleted a record in the middle to simulate a missing minute:
DELETE FROM [Test]
WHERE Id = 50
Now we can use this query to find missing records:
SELECT
a.*
,CASE WHEN b.[Id] IS NULL THEN 'Next record is Missing!' ELSE CAST(b.[Id] as varchar) END AS NextId
FROM
[Test] AS a
LEFT JOIN [Test] AS b ON a.[Date] = DATEADD(minute,1,b.[Date])
WHERE
b.[Id] IS NULL
The resullt will look like this:
Id Date Price NextId
----------- ----------------------- ----------- ------------------------------
49 2013-05-11 22:42:56.440 123 Next record is Missing!
100 2013-05-11 21:51:56.440 123 Next record is Missing!
(2 row(s) affected)
The key solution to the problem is to join the table with itself, but use datediff to find the record that is supposed to be found on the next minute. The last record of the table will of course report that the next row is missing, since it hasn't been inserted yet.
Borrowing TheQ's sample data you can use
WITH T
AS (SELECT *,
DATEDIFF(MINUTE, '20000101', [Date]) -
DENSE_RANK() OVER (ORDER BY [Date]) AS G
FROM Test)
SELECT MIN([Date]) AS StartIsland,
MAX([Date]) AS EndIsland
FROM T
GROUP BY G

Updating a table from another table in SQL Server 2008

I have a temp table variable with a bunch of columns:
Declare #GearTemp table
(
ItemNumber varchar(20),
VendorNumber varchar(6),
ItemStatus varchar(20),
Style varchar(20),
ItemName varchar(100),
ItemDescription varchar(1000),
Color varchar(50),
[Size] varchar(50),
ItemCost decimal(9,4),
IsQuickShipFl bit,
IsEmbroiderable bit,
IsBackOrderable bit,
LoadDate smalldatetime
)
It gets filled with data from another table via an insert statement, and I want to take that data and update my Products table. If possible, I would like to do something like this:
Update Products blah blah blah all columns where itemnumbers match up
SELECT * FROM #GearTemp FT
WHERE EXISTS (SELECT P.ItemNumber FROM Products P WHERE FT.ItemNumber = P.ItemNumber)
Is that possible to do? If it's not, please point me in the right direction.
If I understand you correctly, you can use something like this:
UPDATE p SET X = gt.X, Y = gt.Y -- etc... (not sure whether your column names match up)
FROM Products p
INNER JOIN #GearTemp gt ON p.ItemNumber = gt.ItemNumber
Note that this will only work if, as you stated in the comments above, there is only ever one entry in #GearTemp for each ItemNumber.
Since you are working on SQL Server 2008 you can use the MERGE statement:
-- Target Table
DECLARE #tgt TABLE (OrdID INT, ItemID INT, Qty INT, Price MONEY);
INSERT INTO #tgt (OrdID, ItemID, Qty, Price)
SELECT 1, 100, 10, 10.00 UNION ALL
SELECT 1, 101, 10, 12.00
OrdID ItemID Qty Price
----------- ----------- ----------- ---------------------
1 100 10 10.00
1 101 10 12.00
-- Source Table
DECLARE #src TABLE (OrdID INT, ItemID INT, Qty INT, Price MONEY);
INSERT INTO #src (OrdID, ItemID, Qty, Price)
SELECT 1, 100, 12, 10.00 UNION ALL
SELECT 1, 102, 10, 12.00 UNION ALL
SELECT 1, 103, 5, 7.00
OrdID ItemID Qty Price
----------- ----------- ----------- ---------------------
1 100 12 13.00
1 102 10 12.00
1 103 5 7.00
MERGE #tgt AS t
USING #src AS s
ON t.OrdID = s.OrdID AND t.ItemID = s.ItemID
WHEN MATCHED THEN
UPDATE SET
t.Qty = s.Qty,
t.Price = s.Price;
Content of the target table after the MERGE operation:
OrdID ItemID Qty Price
----------- ----------- ----------- ---------------------
1 100 12 13.00

Resources