Please start any new threads on our new site at https://forums.sqlteam.com. We've got lots of great SQL Server experts to answer whatever question you can come up with.

 All Forums
 SQL Server 2008 Forums
 Transact-SQL (2008)
 Modify the query to get the correct output

Author  Topic 

sql_server_dba
Posting Yak Master

167 Posts

Posted - 2012-04-10 : 15:55:11
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

52326 Posts

Posted - 2012-04-10 : 16:01:19
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

167 Posts

Posted - 2012-04-10 : 16:07:31
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

167 Posts

Posted - 2012-04-10 : 16:09:26
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

52326 Posts

Posted - 2012-04-10 : 16:13:49
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

167 Posts

Posted - 2012-04-10 : 17:08:11
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

227 Posts

Posted - 2012-04-11 : 04:58:44
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
Master Smack Fu Yak Hacker

3451 Posts

Posted - 2012-04-11 : 07:05:30
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

167 Posts

Posted - 2012-04-11 : 12:35:40
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

52326 Posts

Posted - 2012-04-12 : 00:43:49
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

285 Posts

Posted - 2012-04-13 : 22:37:32
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
   

- Advertisement -