Skip to content
  • Categories
  • Recent
  • Tags
  • Popular
  • World
  • Users
  • Groups
Skins
  • Light
  • Cerulean
  • Cosmo
  • Flatly
  • Journal
  • Litera
  • Lumen
  • Lux
  • Materia
  • Minty
  • Morph
  • Pulse
  • Sandstone
  • Simplex
  • Sketchy
  • Spacelab
  • United
  • Yeti
  • Zephyr
  • Dark
  • Cyborg
  • Darkly
  • Quartz
  • Slate
  • Solar
  • Superhero
  • Vapor

  • Default (No Skin)
  • No Skin
Collapse
Code Project
  1. Home
  2. General Programming
  3. Visual Basic
  4. Find maximum value from a record

Find maximum value from a record

Scheduled Pinned Locked Moved Visual Basic
csharpdatabasehelp
4 Posts 2 Posters 0 Views 1 Watching
  • Oldest to Newest
  • Newest to Oldest
  • Most Votes
Reply
  • Reply as topic
Log in to reply
This topic has been deleted. Only users with topic management privileges can see it.
  • M Offline
    M Offline
    mayhem_rules
    wrote on last edited by
    #1

    VB.net / sql Consider a table having A - Z fields storing integer values. Therefore each record in this table will have A - Z fields. I need to find the maximum value for each record from the fields A - Z. Can you plz help me with this. With Best Regards, Mayur

    K 1 Reply Last reply
    0
    • M mayhem_rules

      VB.net / sql Consider a table having A - Z fields storing integer values. Therefore each record in this table will have A - Z fields. I need to find the maximum value for each record from the fields A - Z. Can you plz help me with this. With Best Regards, Mayur

      K Offline
      K Offline
      Kschuler
      wrote on last edited by
      #2

      You can use SQL to get the maximum of each field by doing something like this: SELECT MAX(A) as MaxA, MAX(B) as MaxB, ... I've never tried it but maybe you can do an inner select that would look something like this: SELECT MAX( SELECT MAX(A) as MaxA, MAX(B) as MaxB,...) But if not, you could at least use the first one and then put all the values you get back into a datatable/arraylist and sort them to get the max. Hope this helps a little.

      M 1 Reply Last reply
      0
      • K Kschuler

        You can use SQL to get the maximum of each field by doing something like this: SELECT MAX(A) as MaxA, MAX(B) as MaxB, ... I've never tried it but maybe you can do an inner select that would look something like this: SELECT MAX( SELECT MAX(A) as MaxA, MAX(B) as MaxB,...) But if not, you could at least use the first one and then put all the values you get back into a datatable/arraylist and sort them to get the max. Hope this helps a little.

        M Offline
        M Offline
        mayhem_rules
        wrote on last edited by
        #3

        Thnx Kschuler. the 1st query will give us the max value for each field. Also, the 2nd query, I am not sure if it works, may give us just one value. wat i neede is the max value of all the fields for each record. right now i am getting each field (every record) in an array, sorting it and getting the highest value from it. see the code below. myrow = DS.Tables("psrec").Rows(0) '1 record - 24 fields Dim myIntArray(24) As Integer For i = 0 To 23 myIntArray(i) = myrow(i) Next Array.Sort(myIntArray) maxunits = myIntArray(24) wat leaves me stumped is that since the array starts from 0, it should end at 23. However, the maxvalue can be found at myIntArray(24). when i print the array using the code, For i = 0 To 23 Console.WriteLine(myIntArray(i)) Next I always get 0 for myIntArray(0) for every record and the actual values are displayed from myIntArray(1). Therefore, one value is missed since the array prints till myIntArray(23). The last value is at myIntArray(24). Can you explain this to me... With Best Regards, Mayur

        K 1 Reply Last reply
        0
        • M mayhem_rules

          Thnx Kschuler. the 1st query will give us the max value for each field. Also, the 2nd query, I am not sure if it works, may give us just one value. wat i neede is the max value of all the fields for each record. right now i am getting each field (every record) in an array, sorting it and getting the highest value from it. see the code below. myrow = DS.Tables("psrec").Rows(0) '1 record - 24 fields Dim myIntArray(24) As Integer For i = 0 To 23 myIntArray(i) = myrow(i) Next Array.Sort(myIntArray) maxunits = myIntArray(24) wat leaves me stumped is that since the array starts from 0, it should end at 23. However, the maxvalue can be found at myIntArray(24). when i print the array using the code, For i = 0 To 23 Console.WriteLine(myIntArray(i)) Next I always get 0 for myIntArray(0) for every record and the actual values are displayed from myIntArray(1). Therefore, one value is missed since the array prints till myIntArray(23). The last value is at myIntArray(24). Can you explain this to me... With Best Regards, Mayur

          K Offline
          K Offline
          Kschuler
          wrote on last edited by
          #4

          Okay. I think I got it now. I tested this out and it seems to work, hopefully it will for you too: SELECT MAX(A, B, C, D) FROM myTable This will return the largest value for each record. Then you won't have to worry about doing it in code. As to why your array code wasn't working. I believe that when you declare your array as myIntArray(24) it doesn't create an array with elements of index 0 through 23. It really creates an array of elements 0 through 24. This means that you are not setting that last one, and perhaps it is defaulting to zero. Which would explain that after you sort it, the 0th element is always zero.

          1 Reply Last reply
          0
          Reply
          • Reply as topic
          Log in to reply
          • Oldest to Newest
          • Newest to Oldest
          • Most Votes


          • Login

          • Don't have an account? Register

          • Login or register to search.
          • First post
            Last post
          0
          • Categories
          • Recent
          • Tags
          • Popular
          • World
          • Users
          • Groups