June 6, 2013 at 1:51 pm
create table #MyTable (RecID INT IDENTITY(1,1),trxnNum int,Site varchar(10),Company VARCHAR(100))
INSERT INTO #MyTable (trxnNum,Site,Company)
SELECT 14418,'MySite','MyCompany'
UNION ALL
SELECT 14420,'MySite','MyCompany'
UNION ALL
SELECT 14431,'MySite','MyCompany'
UNION ALL
SELECT 14432,'MySite','MyCompany'
UNION ALL
SELECT 14436,'MySite','MyCompany'
select * from #MyTable
DROP TABLE #MyTable
I have to return rows where TrxnNum = 14418 and 14420 because the gap is only 1 . If the gap is > than 1 example 14432 and 14436, I dont want to return those. If there is no gap as well 14431 and 14432 I dont want to return as well.
In short where gap is = 1.
Thanks,
June 6, 2013 at 2:13 pm
I think you should be able to tweak the logic in this article to achieve what you are looking for.
http://www.sqlservercentral.com/articles/SQL+Puzzles/findinggapsinasequentialnumbersequence/2336/[/url]
Here is another article covering this topic.
http://www.sqlservercentral.com/articles/Advanced+Querying/anefficientsetbasedsolutionforislandsandgaps/1619/[/url]
_______________________________________________________________
Need help? Help us help you.
Read the article at http://www.sqlservercentral.com/articles/Best+Practices/61537/ for best practices on asking questions.
Need to split a string? Try Jeff Modens splitter http://www.sqlservercentral.com/articles/Tally+Table/72993/.
Cross Tabs and Pivots, Part 1 – Converting Rows to Columns - http://www.sqlservercentral.com/articles/T-SQL/63681/
Cross Tabs and Pivots, Part 2 - Dynamic Cross Tabs - http://www.sqlservercentral.com/articles/Crosstab/65048/
Understanding and Using APPLY (Part 1) - http://www.sqlservercentral.com/articles/APPLY/69953/
Understanding and Using APPLY (Part 2) - http://www.sqlservercentral.com/articles/APPLY/69954/
Viewing 2 posts - 1 through 1 (of 1 total)
You must be logged in to reply to this topic. Login to reply
This website stores cookies on your computer.
These cookies are used to improve your website experience and provide more personalized services to you, both on this website and through other media.
To find out more about the cookies we use, see our Privacy Policy