SQL Function, get max dates from 2 tables then compare with given paramter to function
Clash Royale CLAN TAG#URR8PPP
.everyoneloves__top-leaderboard:empty,.everyoneloves__mid-leaderboard:empty margin-bottom:0;
up vote
0
down vote
favorite
ALTER FUNCTION [dbo].[GetLastModifiedDate](@ProductURL nvarchar(255), @ProductLastModified datetime)
RETURNS datetime
AS
BEGIN
DECLARE @Result datetime;
DECLARE @NotesLastMod datetime;
DECLARE @DealsLastMod datetime;
SELECT @NotesLastMod = MAX(LastModified) FROM Notes WHERE ListURL LIKE @ProductURL + '%';
SELECT @DealsLastMod = MAX(DealLastModified) FROM Deals WHERE ListURL LIKE @ProductURL + '%';
SELECT @Result = MAX(LastMod) FROM (VALUES(@ProductLastModified),(@NotesLastMod),(@DealsLastMod)) T(LastMod);
RETURN @Result
END
A very simple self explaining function, gets latest dates from two tables and compare it with given datetime.
Usage is like this,
// SQL Query 1
// SQL Query 2
SELECT dbo.LastModified(Products.URL, Products.LastModified) AS LastModified
FROM PRODUCTS
// Some Inner Joins
// Some Where clauses
Now this function takes around 41 seconds if Notes and Deals tables have 25000 and 300000 rows respectively. We are using SQL Server 2012
sql
add a comment |Â
up vote
0
down vote
favorite
ALTER FUNCTION [dbo].[GetLastModifiedDate](@ProductURL nvarchar(255), @ProductLastModified datetime)
RETURNS datetime
AS
BEGIN
DECLARE @Result datetime;
DECLARE @NotesLastMod datetime;
DECLARE @DealsLastMod datetime;
SELECT @NotesLastMod = MAX(LastModified) FROM Notes WHERE ListURL LIKE @ProductURL + '%';
SELECT @DealsLastMod = MAX(DealLastModified) FROM Deals WHERE ListURL LIKE @ProductURL + '%';
SELECT @Result = MAX(LastMod) FROM (VALUES(@ProductLastModified),(@NotesLastMod),(@DealsLastMod)) T(LastMod);
RETURN @Result
END
A very simple self explaining function, gets latest dates from two tables and compare it with given datetime.
Usage is like this,
// SQL Query 1
// SQL Query 2
SELECT dbo.LastModified(Products.URL, Products.LastModified) AS LastModified
FROM PRODUCTS
// Some Inner Joins
// Some Where clauses
Now this function takes around 41 seconds if Notes and Deals tables have 25000 and 300000 rows respectively. We are using SQL Server 2012
sql
You need index on ListURL fields of both tables
â Eralper
Feb 7 at 15:07
add a comment |Â
up vote
0
down vote
favorite
up vote
0
down vote
favorite
ALTER FUNCTION [dbo].[GetLastModifiedDate](@ProductURL nvarchar(255), @ProductLastModified datetime)
RETURNS datetime
AS
BEGIN
DECLARE @Result datetime;
DECLARE @NotesLastMod datetime;
DECLARE @DealsLastMod datetime;
SELECT @NotesLastMod = MAX(LastModified) FROM Notes WHERE ListURL LIKE @ProductURL + '%';
SELECT @DealsLastMod = MAX(DealLastModified) FROM Deals WHERE ListURL LIKE @ProductURL + '%';
SELECT @Result = MAX(LastMod) FROM (VALUES(@ProductLastModified),(@NotesLastMod),(@DealsLastMod)) T(LastMod);
RETURN @Result
END
A very simple self explaining function, gets latest dates from two tables and compare it with given datetime.
Usage is like this,
// SQL Query 1
// SQL Query 2
SELECT dbo.LastModified(Products.URL, Products.LastModified) AS LastModified
FROM PRODUCTS
// Some Inner Joins
// Some Where clauses
Now this function takes around 41 seconds if Notes and Deals tables have 25000 and 300000 rows respectively. We are using SQL Server 2012
sql
ALTER FUNCTION [dbo].[GetLastModifiedDate](@ProductURL nvarchar(255), @ProductLastModified datetime)
RETURNS datetime
AS
BEGIN
DECLARE @Result datetime;
DECLARE @NotesLastMod datetime;
DECLARE @DealsLastMod datetime;
SELECT @NotesLastMod = MAX(LastModified) FROM Notes WHERE ListURL LIKE @ProductURL + '%';
SELECT @DealsLastMod = MAX(DealLastModified) FROM Deals WHERE ListURL LIKE @ProductURL + '%';
SELECT @Result = MAX(LastMod) FROM (VALUES(@ProductLastModified),(@NotesLastMod),(@DealsLastMod)) T(LastMod);
RETURN @Result
END
A very simple self explaining function, gets latest dates from two tables and compare it with given datetime.
Usage is like this,
// SQL Query 1
// SQL Query 2
SELECT dbo.LastModified(Products.URL, Products.LastModified) AS LastModified
FROM PRODUCTS
// Some Inner Joins
// Some Where clauses
Now this function takes around 41 seconds if Notes and Deals tables have 25000 and 300000 rows respectively. We are using SQL Server 2012
sql
asked Feb 6 at 8:44
Mathematics
3581521
3581521
You need index on ListURL fields of both tables
â Eralper
Feb 7 at 15:07
add a comment |Â
You need index on ListURL fields of both tables
â Eralper
Feb 7 at 15:07
You need index on ListURL fields of both tables
â Eralper
Feb 7 at 15:07
You need index on ListURL fields of both tables
â Eralper
Feb 7 at 15:07
add a comment |Â
active
oldest
votes
active
oldest
votes
active
oldest
votes
active
oldest
votes
active
oldest
votes
Sign up or log in
StackExchange.ready(function ()
StackExchange.helpers.onClickDraftSave('#login-link');
);
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Post as a guest
StackExchange.ready(
function ()
StackExchange.openid.initPostLogin('.new-post-login', 'https%3a%2f%2fcodereview.stackexchange.com%2fquestions%2f186893%2fsql-function-get-max-dates-from-2-tables-then-compare-with-given-paramter-to-fu%23new-answer', 'question_page');
);
Post as a guest
Sign up or log in
StackExchange.ready(function ()
StackExchange.helpers.onClickDraftSave('#login-link');
);
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Post as a guest
Sign up or log in
StackExchange.ready(function ()
StackExchange.helpers.onClickDraftSave('#login-link');
);
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Post as a guest
Sign up or log in
StackExchange.ready(function ()
StackExchange.helpers.onClickDraftSave('#login-link');
);
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
You need index on ListURL fields of both tables
â Eralper
Feb 7 at 15:07