I have the following SQL query from SSMS and it is returning CompanyCase column values like
Sample Data:1
MH 9090
MH 9089
MH 9088
MH 9087
MH 9086
MH 9085
So as you can see, CompanyCase are in descending orders and in a sequence as far as numbers are concerned. Now, there can be a scenario when
some of them might not be in a sequence and it can be like the following:
Sample Data:2
MH 10005
MH 10001
MH 9099
MH 9090
MH 9089
MH 9088
MH 9087
MH 9086
MH 9085
In such scenarios, I want to write a SQL query which can find out 3 or 5 sequential values and based on that I can warn the user about the next
sequentia value. In above example, since MH9085 - MH9090 are in sequential, I would like to suggest the user that you should use MH9091 as the next value
I think even if I can get MH9090 in this case, I can suggest by adding 1 to 9090 that MH9091 is the next CompanyCase which coud be used.
Is there a way to modify the below query to achieve the same? The datatype of CompanyCase column is varchar(16) in the Employee table.
/****** Script for SelectTopNRows command from SSMS ******/
SELECT TOP (1000) [employeeID]
,[clientID]
,[speciesID]
,[CaseID]
,[CompanyCase]
,[entryDate]
,[lastUpdate]
,[checkInDate]
,[endDate]
FROM [mydatabase].[dbo].[Employee]
where CompanyCase LIKE 'MH %'
ANd endDate IS NULL
order by checkInDate desc, CompanyCase desc
Edit:
What I’m looking for:
I am looking to find a pattern of sequence if it exists. For example, in Sample Data:1
, it’s very clear that all numerical data is sequential so if I have to go and pick up the case that has the largest numerical value, I can just go and grab the top one, which is MH9090
. However, in case of Sample Data:2
, I would still like to grab MH9099
since that has the sequential pattern of numerical values and MH10001 and MH10005 are not sequential.