Click here to Skip to main content
15,887,334 members
Please Sign up or sign in to vote.
1.00/5 (2 votes)
See more:
I have result of two table as below column I need to have , custm date where we have all date(starting from next month till end of dec 2024), if one month also missing then it should not be in custm date column.

In example below: custm date='8/1/2023' then it should check for Cetstart date. is data available for sep 2023 till dec 2024 in Cetstart date, as in we dont have data for oct( we have data for november) so that custm date should not be picked up date.In cetstart date if any month is missing between next month of custm date till dec 2024, then it should not be right custm date.
so in short custm date should check if Cetstart date is avaiable for next month till dec 2024(all months should be available, if anyone month also missing then it should not be in custm date column)

is it possible to get like this?
custm date	CetStart				
8/1/2023	10/1/2023				
8/1/2023	11/1/2023				
8/1/2023	12/1/2023				
8/1/2023	1/1/2024				
8/1/2023	2/1/2024				
8/1/2023	3/1/2024				
8/1/2023	4/1/2024				
8/1/2023	5/1/2024				
8/1/2023	6/1/2024				
8/1/2023	7/1/2024				
8/1/2023	8/1/2024				
8/1/2023	9/1/2024				
8/1/2023	10/1/2024				
8/1/2023	11/1/2024				
8/1/2023	12/1/2024				
7/31/2023	8/1/2023				
7/31/2023	9/1/2023				
7/31/2023	10/1/2023				
7/31/2023	11/1/2023				
7/31/2023	12/1/2023				
7/31/2023	1/1/2024				
7/31/2023	2/1/2024				
7/31/2023	3/1/2024				
7/31/2023	4/1/2024				
7/31/2023	5/1/2024				
7/31/2023	6/1/2024				
7/31/2023	7/1/2024				
7/31/2023	8/1/2024				
7/31/2023	9/1/2024				
7/31/2023	10/1/2024				
7/31/2023	11/1/2024				
7/31/2023	12/1/2024				
7/28/2023	8/1/2023				
7/28/2023	9/1/2023				
7/28/2023	10/1/2023				
7/28/2023	11/1/2023				
7/28/2023	12/1/2023				
7/28/2023	1/1/2024				
7/28/2023	2/1/2024
7/28/2023	3/1/2024
7/28/2023	4/1/2024
7/28/2023	5/1/2024
7/28/2023	6/1/2024
7/28/2023	7/1/2024
7/28/2023	8/1/2024
7/28/2023	9/1/2024
7/28/2023	10/1/2024
7/28/2023	11/1/2024
7/28/2023	12/1/2024

-------------------------				


output I needed:
Output:
custm date
7/31/2023
7/28/2023


What I have tried:

SSMS
select custm date, CetStart from table
where DATENAME(month,mtuCetStart)=DATENAME(month,DATEADD(Month,+1,tradeDate)) and DATENAME(Year,mtuCetStart)=DATENAME(Year,tradeDate)


but here it is not checking for whole mtucetstart till 2024 dec, it is only checking for one next month only.
Posted
Updated 3-Aug-23 4:26am
v2

1 solution

Not sure what you call your data table, I named it 'your_table'. You can use a query as in my code -
SQL
SELECT custm_date
FROM your_table t1
WHERE NOT EXISTS (
    SELECT 1
    FROM your_table t2
    WHERE t2.custm_date = t1.custm_date
    AND NOT EXISTS (
        SELECT 1
        FROM your_table t3
        WHERE t3.custm_date = t1.custm_date
        AND t3.CetStart = DATEADD(MONTH, n.n, DATEADD(DAY, 1, EOMONTH(t1.custm_date, 1)))
    )
)


The subquery above with the alias 'n' generates a sequence of numbers from 1 to the number of months between the next month of the 'custm_date' and December 2024. It then checks for each month in the sequence whether there exists a corresponding 'CetStart' date in your table for the given 'custm_date.' If any month is missing, the 'NOT EXISTS' condition will be true, and that 'custm_date' will not be included in your output result.

To test this, I have done the following -
SQL
CREATE TABLE your_table (
    custm_date DATE,
    CetStart DATE
);

INSERT INTO your_table (custm_date, CetStart) VALUES
('8/1/2023', '10/1/2023'),
('8/1/2023', '11/1/2023'),
('8/1/2023', '12/1/2023'),
('8/1/2023', '1/1/2024'),
('8/1/2023', '2/1/2024'),
('8/1/2023', '3/1/2024'),
('8/1/2023', '4/1/2024'),
('8/1/2023', '5/1/2024'),
('8/1/2023', '6/1/2024'),
('8/1/2023', '7/1/2024'),
('8/1/2023', '8/1/2024'),
('8/1/2023', '9/1/2024'),
('8/1/2023', '10/1/2024'),
('8/1/2023', '11/1/2024'),
('8/1/2023', '12/1/2024'),
('7/31/2023', '8/1/2023'),
('7/31/2023', '9/1/2023'),
('7/31/2023', '10/1/2023'),
('7/31/2023', '11/1/2023'),
('7/31/2023', '12/1/2023'),
('7/31/2023', '1/1/2024'),
('7/31/2023', '2/1/2024'),
('7/31/2023', '3/1/2024'),
('7/31/2023', '4/1/2024'),
('7/31/2023', '5/1/2024'),
('7/31/2023', '6/1/2024'),
('7/31/2023', '7/1/2024'),
('7/31/2023', '8/1/2024'),
('7/31/2023', '9/1/2024'),
('7/31/2023', '10/1/2024'),
('7/31/2023', '11/1/2024'),
('7/31/2023', '12/1/2024'),
('7/28/2023', '8/1/2023'),
('7/28/2023', '9/1/2023'),
('7/28/2023', '10/1/2023'),
('7/28/2023', '11/1/2023'),
('7/28/2023', '12/1/2023'),
('7/28/2023', '1/1/2024'),
('7/28/2023', '2/1/2024'),
('7/28/2023', '3/1/2024'),
('7/28/2023', '4/1/2024'),
('7/28/2023', '5/1/2024'),
('7/28/2023', '6/1/2024'),
('7/28/2023', '7/1/2024'),
('7/28/2023', '8/1/2024'),
('7/28/2023', '9/1/2024'),
('7/28/2023', '10/1/2024'),
('7/28/2023', '11/1/2024'),
('7/28/2023', '12/1/2024');


If you run the sample code I gave you, the output result will be -
Output
custm_date
----------
2023-07-31
2023-07-28


These are the 'custm_date' values for which all the months from August 2023 to December 2024 have corresponding 'CetStart' dates in your table. Any other 'custm_date' with missing months would not be included in your output result set.
 
Share this answer
 
v2
Comments
Prads12 3-Aug-23 17:37pm    
@Andre Oosthuizen: Thank you for your quick reply and solution. what is here n.n? AS I didnt find any subquery name in code is n. would it be dynamic?(I dont want I should enter each time when I run query). Would it be possible? and thanks in advance for your help :)
Andre Oosthuizen 4-Aug-23 6:39am    
You're welcome.

First, have a look at what a derived table is -
Quote:A derived table is an expression that generates a table within the scope of a query FROM clause. For example, a subquery in a SELECT statement FROM clause is a derived table:

'n' Is an alias used to represent a derived table that generates a sequence of numbers (Your case the months calculations). The sequence is generated using the 'VALUES' table constructor, and the alias 'n' is used to refer to each element in that sequence.

'n' Is used in your example to create a sequence of numbers from 1 to the number of months between the next month of the 'custm_date' and December 2024. The 'n.n' part in the query represents each element in this sequence.
Prads12 4-Aug-23 10:00am    
okay understood now, and thanks alot for your help. I really appreciate your help :)
Andre Oosthuizen 4-Aug-23 11:39am    
Only a pleasure.

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)



CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900