Results 1 to 5 of 5

Thread: Findout missed numbers

  1. #1

    Thread Starter
    Member
    Join Date
    Oct 2009
    Posts
    58

    Findout missed numbers

    Dear Experts

    Sqlserver Table1holds data as follows

    date---------------sno
    01/01/2010-------1
    01/01/2010-------2
    01/01/2010-------4
    01/01/2010-------5
    01/01/2010-------7
    02/01/2010-------1
    02/01/2010-------2
    02/01/2010-------6
    02/01/2010-------8

    How to findout missed sno in 01/01/2010?

    I want to get this result
    3
    6

    Please help

  2. #2
    INXSIVE Bruce Fox's Avatar
    Join Date
    Sep 2001
    Location
    Melbourne, Australia
    Posts
    7,429

    Re: Findout missed numbers

    Did the sno 3 and 6 ever exist for 01/01/2010?

    In case the above response is true, generaly, in DataBases records should never be 'deleted', just canceled or made inactive etc. With that design (and a Primary Key), you would have an easy way to find out what records were canceled/inactive for any period.
    Last edited by Bruce Fox; Jan 13th, 2010 at 09:43 PM.

  3. #3
    INXSIVE Bruce Fox's Avatar
    Join Date
    Sep 2001
    Location
    Melbourne, Australia
    Posts
    7,429

    Re: Findout missed numbers

    Also, how do you know that sno 8 didn't exist in that period?

  4. #4
    Super Moderator jmcilhinney's Avatar
    Join Date
    May 2005
    Location
    Sydney, Australia
    Posts
    111,221

    Re: Findout missed numbers

    While I'm sure it's possible, I'm not aware of a simple way to do that purely in SQL. I'd probably suggest that you get a list of distinct values from the database, then use LINQ to get the complement. For instance, you could query the database like so:
    SQL Code:
    1. SELECT DISTINCT sno
    2. FROM Table1
    3. WHERE date = @date
    You can then do this in LINQ:
    vb.net Code:
    1. Dim table As New DataTable
    2.  
    3. table.Columns.Add("sno", GetType(Integer))
    4.  
    5. table.Rows.Add(1)
    6. table.Rows.Add(2)
    7. table.Rows.Add(4)
    8. table.Rows.Add(5)
    9. table.Rows.Add(7)
    10.  
    11. '*** The code above simply simulates getting the data from the database.
    12.  
    13. Dim presentValues = (From row In table.AsEnumerable() _
    14.                      Select row.Field(Of Integer)("sno")).ToArray()
    15. Dim maxValue = presentValues.Max()
    16. Dim allValues = Enumerable.Range(1, maxValue)
    17. Dim missingValues = From int In allValues Where Not presentValues.Contains(int)
    18.  
    19. For Each v In missingValues
    20.     MessageBox.Show(v.ToString())
    21. Next
    Why is my data not saved to my database? | MSDN Data Walkthroughs
    VBForums Database Development FAQ
    My CodeBank Submissions: VB | C#
    My Blog: Data Among Multiple Forms (3 parts)
    Beginner Tutorials: VB | C# | SQL

  5. #5
    Super Moderator jmcilhinney's Avatar
    Join Date
    May 2005
    Location
    Sydney, Australia
    Posts
    111,221

    Re: Findout missed numbers

    I should point out that I just answered the question as asked. I have to concur with Bruce though: if you're doing this to reuse IDs then you really shouldn't.
    Why is my data not saved to my database? | MSDN Data Walkthroughs
    VBForums Database Development FAQ
    My CodeBank Submissions: VB | C#
    My Blog: Data Among Multiple Forms (3 parts)
    Beginner Tutorials: VB | C# | SQL

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •  



Click Here to Expand Forum to Full Width