October 7, 2021 at 10:14 pm
Hello everyone,
I am trying to combine two columns (Current last receipt) and (Historic last receipt). I basically want to create a third column (MAX Last Process Date) that will be the most current process date from both columns.
So like for this item I need to have this value '20210804' as the value
Here is my code
SELECT w2.RCWHS# AS DC, w2.RCITM# AS 'Item Number', CONCAT(w2.RCWHS#, ' ', w2.RCITM#) AS 'Key', MAX(w1.RCLDTE) AS 'Current last receipt', MAX(w2.RCLDTE) AS 'Historic last receipt'
FROM LEVYDTA.RECTRNH w2
FULL OUTER JOIN LEVYDTA.RECTRNT w1
ON w1.RCWHS#=w2.RCWHS# AND w1.RCITM#=w2.RCITM#
GROUP BY w2.RCWHS#, w2.RCITM#
ORDER BY 'Key'
If someone may know how to do it, I will greatly appreciate.
Thank you!!
October 8, 2021 at 3:18 am
You'd have to use IIF([Date1]>=[Date2],[Date1],[Date2]) Then maybe do an aggregate on that expression.
October 8, 2021 at 12:54 pm
In SQL Server 2019 you can also utilize the GREATEST function.
October 8, 2021 at 2:03 pm
In SQL Server 2019 you can also utilize the GREATEST function.
That documentation seems to suggest that it's only available on Azure instances and not "standalone" instances. Can someone with a "standalone" instance of 2019 please verify if this and the "LEAST" functions are available?
--Jeff Moden
Change is inevitable... Change for the better is not.
October 8, 2021 at 2:22 pm
kaj wrote:In SQL Server 2019 you can also utilize the GREATEST function.
That documentation seems to suggest that it's only available on Azure instances and not "standalone" instances. Can someone with a "standalone" instance of 2019 please verify if this and the "LEAST" functions are available?
I get this error
Msg 195, Level 15, State 10, Line 3
'GREATEST' is not a recognized built-in function name.
October 8, 2021 at 2:25 pm
I kinda figured. Thanks, for the confirmation.
After the performance issues included with the FORMAT function, I also worry about performance anytime MS comes out with a new function. I won't be able to test GREATEST/LEAST because I don't use Azure at all.
--Jeff Moden
Change is inevitable... Change for the better is not.
October 8, 2021 at 2:36 pm
I'm sorry about the misinformation. I didn't pay attention to the list of "Applies to" services.
I remembered some excited chatter some months ago about the functions now being available, but I overlooked that little disclaimer.
October 8, 2021 at 2:37 pm
This should work
SELECT w2.RCWHS# AS DC,
w2.RCITM# AS 'Item Number',
CONCAT(w2.RCWHS#, ' ', w2.RCITM#) AS 'Key',
MAX(w1.RCLDTE) AS 'Current last receipt',
MAX(w2.RCLDTE) AS 'Historic last receipt',
CASE WHEN ISNULL(MAX(w1.RCLDTE),'19000101') > ISNULL(MAX(w2.RCLDTE),'19000101')
THEN MAX(w1.RCLDTE)
ELSE MAX(w2.RCLDTE)
END AS 'Last Process Date'
FROM LEVYDTA.RECTRNH w2
FULL JOIN LEVYDTA.RECTRNT w1
ON w1.RCWHS#=w2.RCWHS# AND w1.RCITM#=w2.RCITM#
GROUP BY w2.RCWHS#, w2.RCITM#
ORDER BY 'Key'
October 8, 2021 at 2:55 pm
[Edit] I posted some code that didn't look right so I updated. Maybe something like this
select w2.RCWHS# as DC, w2.RCITM# as [Item Number],
CONCAT(w2.RCWHS#, ' ', w2.RCITM#) as [Key],
MAX(w1.RCLDTE) as [Current last receipt],
MAX(w2.RCLDTE) as [Historic last receipt],
MAX(mx.RCLDTE_one_col) as [Last Process Date]
from LEVYDTA.RECTRNH w2
full join LEVYDTA.RECTRNT w1 on w1.RCWHS#=w2.RCWHS#
and w1.RCITM#=w2.RCITM#
outer apply (select max(unpvt.RCLDTE_one_col)
from (values (ISNULL(w1.RCLDTE,'19000101')),
(ISNULL(w2.RCLDTE,'19000101')))
unpvt(RCLDTE_one_col)) mx(RCLDTE_one_col)
group by w2.RCWHS#, w2.RCITM#;
Aus dem Paradies, das Cantor uns geschaffen, soll uns niemand vertreiben können
October 8, 2021 at 3:55 pm
I'm sorry about the misinformation. I didn't pay attention to the list of "Applies to" services.
I remembered some excited chatter some months ago about the functions now being available, but I overlooked that little disclaimer.
You're definitely one of the "good guys" for posting that kind of feedback. And, totally understood. Heh... I remember that same kind of excited chatter but couldn't remember the scope and had to look it up myself. 😀
--Jeff Moden
Change is inevitable... Change for the better is not.
Viewing 10 posts - 1 through 9 (of 9 total)
You must be logged in to reply to this topic. Login to reply