Hi Team,
I am retrieving files and file created date and file modified date from table based on the created date. All files are same file name. My output is looks like below. In the below output, sep 1 file is sending twice a day in File sending date (09/01 & 09/30). I need the latest sending date file instead of duplicate file. Can yo please help on this?
File Name Created Date File sending Date
File_XXXX 1-Sep 9/1/2021
File_XXXX 2-Sep 9/2/2021
File_XXXX 3-Sep 9/3/2021
File_XXXX 6-Sep 9/6/2021
File_XXXX 1-Sep 9/30/2021
File_XXXX 30-Sep 9/30/2021
October 26, 2021 at 3:37 pm
SELECT [File Name], [Created Date], MAX([File sending Date] AS [File sending Date]
FROM <your_table_name>
GROUP BY [File Name], [Created Date]
SQL DBA,SQL Server MVP(07, 08, 09) "It's a dog-eat-dog world, and I'm wearing Milk-Bone underwear." "Norm", on "Cheers". Also from "Cheers", from "Carla": "You need to know 3 things about Tortelli men: Tortelli men draw women like flies; Tortelli men treat women like flies; Tortelli men's brains are in their flies".
October 26, 2021 at 8:39 pm
Hi Team,
I am retrieving files and file created date and file modified date from table based on the created date. All files are same file name. My output is looks like below. In the below output, sep 1 file is sending twice a day in File sending date (09/01 & 09/30). I need the latest sending date file instead of duplicate file. Can yo please help on this?
File Name Created Date File sending Date File_XXXX 1-Sep 9/1/2021 File_XXXX 2-Sep 9/2/2021 File_XXXX 3-Sep 9/3/2021 File_XXXX 6-Sep 9/6/2021 File_XXXX 1-Sep 9/30/2021 File_XXXX 30-Sep 9/30/2021
What is the actual data type for the Created Date column? Don't answer that... instead, read the article located at the first link in my signature line below for how to post "Readily Consumable" test data, which will also perfectly explain everything we need to know about your data.
--Jeff Moden
Change is inevitable... Change for the better is not.
October 27, 2021 at 1:43 pm
Thanks for your reply. This is not working. I need an output like below.
File Name Created Date File sending Date
File_XXXX 2-Sep 9/2/2021
File_XXXX 3-Sep 9/3/2021
File_XXXX 6-Sep 9/6/2021
File_XXXX 1-Sep 9/30/2021
File_XXXX 30-Sep 9/30/2021
October 27, 2021 at 2:19 pm
Scott's solution was correct, with the possible exception of the ordering of results. Here is code to prove it.
DECLARE @x TABLE
(
FileName VARCHAR(50) NOT NULL
,CreatedDate DATE NOT NULL
,FileSendingDate DATE NOT NULL
);
INSERT @x
(
FileName
,CreatedDate
,FileSendingDate
)
VALUES
('File_XXXX', '20210901', '20210901')
,('File_XXXX', '20210902', '20210902')
,('File_XXXX', '20210903', '20210903')
,('File_XXXX', '20210906', '20210906')
,('File_XXXX', '20210901', '20210930')
,('File_XXXX', '20210930', '20210930');
SELECT x.FileName
,x.CreatedDate
,FileSendingDate = MAX(x.FileSendingDate)
FROM @x x
GROUP BY x.FileName
,x.CreatedDate
ORDER BY MAX(x.FileSendingDate)
,x.CreatedDate;
The absence of evidence is not evidence of absence
- Martin Rees
The absence of consumable DDL, sample data and desired results is, however, evidence of the absence of my response
- Phil Parkin
October 27, 2021 at 2:43 pm
Thanks Scott & Phill. It's working fine now. I am added another column as Status and the data looks like in the table now.
File Name Created Date File sending Date Status
File_XXXX 1-Sep 9/1/2021 Pass 02:52 PM
File_XXXX 2-Sep 9/2/2021 Pass 02:42 PM
File_XXXX 3-Sep 9/3/2021 Pass 01:22 PM
File_XXXX 6-Sep 9/6/2021 Pass 12:44 PM
File_XXXX 1-Sep 9/30/2021 Fail 11:41 PM
File_XXXX 30-Sep 9/30/2021 Fail 11:49 PM
If I used the same query, I am getting all the input like above table. I need the output like below.
File Name Created Date File sending Date Status
File_XXXX 2-Sep 9/2/2021 Pass 02:42 PM
File_XXXX 3-Sep 9/3/2021 Pass 01:22 PM
File_XXXX 6-Sep 9/6/2021 Pass 12:44 PM
File_XXXX 1-Sep 9/30/2021 Fail 11:41 PM
File_XXXX 30-Sep 9/30/2021 Fail 11:49 PM
Can you please help me on this?
Thanks!
October 27, 2021 at 2:53 pm
Is this the final version, or are you going to continue gradually drip-feeding additional requirements?
If so, please skip to the end and provide the final version now, to avoid wasting people's time.
The absence of evidence is not evidence of absence
- Martin Rees
The absence of consumable DDL, sample data and desired results is, however, evidence of the absence of my response
- Phil Parkin
October 27, 2021 at 3:01 pm
Sorry, This is the final version.
Here is one way.
DECLARE @x TABLE
(
FileName VARCHAR(50) NOT NULL
,CreatedDate DATE NOT NULL
,FileSendingDate DATE NOT NULL
,Status VARCHAR(50) NOT NULL
);
INSERT @x
(
FileName
,CreatedDate
,FileSendingDate
,Status
)
VALUES
('File_XXXX', '20210901', '20210901', 'Pass 02:52 PM')
,('File_XXXX', '20210902', '20210902', 'Pass 02:42 PM')
,('File_XXXX', '20210903', '20210903', 'Pass 01:22 PM')
,('File_XXXX', '20210906', '20210906', 'Pass 12:44 PM')
,('File_XXXX', '20210901', '20210930', 'Fail 11:41 PM')
,('File_XXXX', '20210930', '20210930', 'Fail 11:49 PM');
WITH ordered
AS (SELECT x.FileName
,x.CreatedDate
,x.FileSendingDate
,x.Status
,rn = ROW_NUMBER() OVER (PARTITION BY x.FileName
,x.CreatedDate
ORDER BY x.FileSendingDate DESC
)
FROM @x x)
SELECT ordered.FileName
,ordered.CreatedDate
,ordered.FileSendingDate
,ordered.Status
FROM ordered
WHERE ordered.rn = 1
ORDER BY ordered.FileSendingDate
,ordered.CreatedDate;
The absence of evidence is not evidence of absence
- Martin Rees
The absence of consumable DDL, sample data and desired results is, however, evidence of the absence of my response
- Phil Parkin
Viewing 9 posts - 1 through 8 (of 8 total)
You must be logged in to reply to this topic. Login to reply