I am getting the last update from two different columns. Either P1DT will be null or P2Dt will be null, and never both. I have successfully merged the two columns into one, but will need to preserve the seconds at the end of the number... what is the best way to go about this?
WITH tblDT AS
(
SELECT DISTINCT
StudyNum,
SerialNum,
PuffCount,
CONCAT((SELECT MAX(P1DT)
FROM P3 P3B
WHERE P3B.SerialNum = P3A.SerialNum
AND P3B.StudyNum = P3A.StudyNum),
(SELECT MAX(P2DT)
FROM P3 P3B
WHERE P3B.SerialNum = P3A.SerialNum
AND P3B.StudyNum = P3A.StudyNum)) AS MaxDate
FROM
P3 P3A
)
SELECT *
FROM tblDT
I tried concatenating the two columns into one. It works successfully, but does not preserve the seconds.
You want
COALESCE()rather thanCONCAT():If you don't really need
PuffCount, know it's always the same for a givenStudyNumandSerialNum, or can otherwise infer it from an aggregate function, we can also remove theJOIN.Hmm... thinking more, I think we can also do this with window functions:
See them work here:
If the original query runs at all using
concat(), with no casting, then you don't really have DateTime values at all. What you have are string/varchar values pretending to be DateTime values, and that's really bad.