SQL Server Forums
Profile | Register | Active Topics | Members | Search | Forum FAQ
 
Register Now and get your question answered!
Username:
Password:
Save Password
Forgot your Password?

 All Forums
 SQL Server 2008 Forums
 Transact-SQL (2008)
 Modify the query to get the correct output
 New Topic  Reply to Topic
 Printer Friendly
Author Previous Topic Topic Next Topic  

sql_server_dba
Posting Yak Master

137 Posts

Posted - 04/10/2012 :  15:55:11  Show Profile  Reply with Quote
All,

I am stuck with a table where we have multiple records and i need to eliminate some records. Below is the example...

columnA columnB Start End ColumnC
111 1 1/1/2011 1/2/2011 11
111 1 1/2/2011 1/6/2011 11
111 2 1/6/2011 1/7/2011 22
111 1 1/7/2011 12/31/9999 11

But i need the output as
columnA columnB Start End ColumnC
111 1 1/1/2011 1/6/2011 11
111 2 1/6/2011 1/7/2011 22
111 1 1/7/2011 12/31/9999 11

I can write the below query...
Select columnA, columnB, min(start), max(end) From Table

But i will end up with...
columnA columnB Start End ColumnC
111 1 1/1/2011 12/31/9999 11
111 2 1/6/2011 1/7/2011 22

which will mess up my table. Can anyone please let me know the logic behind this?

visakh16
Very Important crosS Applying yaK Herder

India
47023 Posts

Posted - 04/10/2012 :  16:01:19  Show Profile  Reply with Quote
can there be more than one overlaps?

like

111 1 1/1/2011 1/2/2011 11
111 1 1/2/2011 1/6/2011 11
111 1 1/6/2011 1/12/2011 11


in that case what should be output?

------------------------------------------------------------------------------------------------------
SQL Server MVP
http://visakhm.blogspot.com/

Go to Top of Page

sql_server_dba
Posting Yak Master

137 Posts

Posted - 04/10/2012 :  16:07:31  Show Profile  Reply with Quote
Yes..there will be and i need only one record with the start and end dates.
Go to Top of Page

sql_server_dba
Posting Yak Master

137 Posts

Posted - 04/10/2012 :  16:09:26  Show Profile  Reply with Quote
111 1 1/1/2011 1/2/2011 11
111 1 1/2/2011 1/6/2011 11
111 1 1/6/2011 1/12/2011 11
111 2 1/12/2011 1/20/2011 11
111 1 1/20/2011 12/31/9999 11

then the output should be

111 1 1/1/2011 1/12/201 11
111 2 1/12/2011 1/20/2011 11
111 1 1/20/2011 12/31/9999 11
Go to Top of Page

visakh16
Very Important crosS Applying yaK Herder

India
47023 Posts

Posted - 04/10/2012 :  16:13:49  Show Profile  Reply with Quote
then you need to use a iterative logic like below.

http://visakhm.blogspot.com/2012/03/iterative-queries-using-common-table.html

make a start using above and in case you face any difficulty, I'll help you out



------------------------------------------------------------------------------------------------------
SQL Server MVP
http://visakhm.blogspot.com/

Go to Top of Page

sql_server_dba
Posting Yak Master

137 Posts

Posted - 04/10/2012 :  17:08:11  Show Profile  Reply with Quote
I tried little and i was unable to. I may need more development experience :(. This is the 1st time i am dealing with RANK
Go to Top of Page

vinu.vijayan
Posting Yak Master

India
227 Posts

Posted - 04/11/2012 :  04:58:44  Show Profile  Reply with Quote
quote:
Originally posted by sql_server_dba

111 1 1/1/2011 1/2/2011 11
111 1 1/2/2011 1/6/2011 11
111 1 1/6/2011 1/12/2011 11
111 2 1/12/2011 1/20/2011 11
111 1 1/20/2011 12/31/9999 11

then the output should be

111 1 1/1/2011 1/12/201 11
111 2 1/12/2011 1/20/2011 11
111 1 1/20/2011 12/31/9999 11



The logic is still pretty unclear. The output dates are still overlapping.
Are you trying to delete the records which are monthly and only trying to keep the ones that are yearly/half-yearly??
Please be a little more precise on the logic here.

Vinu Vijayan
Go to Top of Page

Transact Charlie
Flowing Fount of Yak Knowledge

United Kingdom
3425 Posts

Posted - 04/11/2012 :  07:05:30  Show Profile  Visit Transact Charlie's Homepage  Reply with Quote
quote:
Originally posted by visakh16

then you need to use a iterative logic like below.

http://visakhm.blogspot.com/2012/03/iterative-queries-using-common-table.html

make a start using above and in case you face any difficulty, I'll help you out

------------------------------------------------------------------------------------------------------
SQL Server MVP
http://visakhm.blogspot.com/



I don't think you need to be iterative.

How about this?

DECLARE @sample TABLE (
	[columnA] INT
	, [columnB] INT
	, [Start] DATE
	, [End] DATE
	, [ColumnC] INT
	)
INSERT @sample
VALUES
	 (111, 1, '20110101', '20110201', 11)
	,(111, 1, '20110201', '20110401', 11) -- This should dissapear
	,(111, 1, '20110401', '20110601', 11)
	,(111, 2, '20110601', '20110701', 22)
	,(111, 1, '20110701', '99991230', 11) -- This is an example of an outlier
	
	

SELECT * FROM @sample

; WITH

	startDateRanges AS (
		SELECT *
		FROM @sample AS s1
		WHERE NOT EXISTS (
			SELECT 1
			FROM @sample AS s2
			WHERE
				s2.[columnA] = s1.[columnA]
				AND s2.[columnB] = s1.[columnB]
				AND s2.[ColumnC] = s1.[ColumnC]
				AND s2.[End] = s1.[Start]
				)
		)
	
	, endDateRanges AS (
		SELECT *
		FROM @sample AS s1
		WHERE NOT EXISTS (
			SELECT 1
			FROM @sample AS s2
			WHERE
				s2.[columnA] = s1.[columnA]
				AND s2.[columnB] = s1.[columnB]
				AND s2.[ColumnC] = s1.[ColumnC]
				AND s2.[Start] = s1.[End]
				)
		)
		
SELECT
	sdr.[ColumnA]
	, sdr.[columnB]
	, sdr.[Start]
	, edr.[End]
	, sdr.[ColumnC]
FROM
	startDateRanges AS sdr
	CROSS APPLY ( 
		SELECT TOP 1 [End]
		FROM endDateRanges AS edr
		WHERE edr.[columnA] = sdr.[columnA]
		  AND edr.[columnB] = sdr.[columnB]
		  AND edr.[ColumnC] = sdr.[ColumnC]
		  AND edr.[Start] >= sdr.[Start]
		ORDER BY
			edr.[End] ASC
		)
		AS edr


Idea -- find all possible start candidates and end candidates.

Then match them up.

This would get good index use on the columns in question.

Meets your sample and output:

Output:

ColumnA     columnB     Start      End        ColumnC
----------- ----------- ---------- ---------- -----------
111         1           2011-01-01 2011-06-01 11
111         2           2011-06-01 2011-07-01 22
111         1           2011-07-01 9999-12-30 11




Charlie
===============================================================
Msg 3903, Level 16, State 1, Line 1736
The ROLLBACK TRANSACTION request has no corresponding BEGIN TRANSACTION
Go to Top of Page

sql_server_dba
Posting Yak Master

137 Posts

Posted - 04/11/2012 :  12:35:40  Show Profile  Reply with Quote
Great Charlie...this should fix it. I will try it out.... Amazing work. Thanks a ton.
Go to Top of Page

visakh16
Very Important crosS Applying yaK Herder

India
47023 Posts

Posted - 04/12/2012 :  00:43:49  Show Profile  Reply with Quote
yep..that's a good set based technique Charlie. Good catch!

------------------------------------------------------------------------------------------------------
SQL Server MVP
http://visakhm.blogspot.com/

Go to Top of Page

namman
Constraint Violating Yak Guru

USA
258 Posts

Posted - 04/13/2012 :  22:37:32  Show Profile  Reply with Quote
Good work Charlie!

However I am little concerned about performance. Your solution requires to access the source 4 times. Look like that is more than neccerasary.

Anyone having solution better than that?

Go to Top of Page
  Previous Topic Topic Next Topic  
 New Topic  Reply to Topic
 Printer Friendly
Jump To:
SQL Server Forums © 2000-2009 SQLTeam Publishing, LLC Go To Top Of Page
This page was generated in 0.09 seconds. Powered By: Snitz Forums 2000