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. Database & SysAdmin
  3. Database
  4. select the max value from the row and its other values

select the max value from the row and its other values

Scheduled Pinned Locked Moved Database
questiondatabase
14 Posts 3 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.
  • T Tobias Schoenig

    Try SELECT g.Maximum, * from Projects, (SELECT MAX(col1) as Maximum FROM projects GROUP BY project)g WHERE MAX(col1) = g.Maximum

    D Offline
    D Offline
    Deques
    wrote on last edited by
    #3

    it looks good, but the column project you have after group by isnt used anywhere. is it something missing before group by? never mind about that. I have tried the query and it results a little too much. it seems it loops for each max value and result for all row

    T 1 Reply Last reply
    0
    • D Deques

      it looks good, but the column project you have after group by isnt used anywhere. is it something missing before group by? never mind about that. I have tried the query and it results a little too much. it seems it loops for each max value and result for all row

      T Offline
      T Offline
      Tobias Schoenig
      wrote on last edited by
      #4

      You're right - it has to be like that: SELECT g.Maximum, * from Projects, (SELECT MAX(col1) as Maximum, project FROM projects GROUP BY project)g WHERE MAX(col1) = g.Maximum

      D 1 Reply Last reply
      0
      • T Tobias Schoenig

        You're right - it has to be like that: SELECT g.Maximum, * from Projects, (SELECT MAX(col1) as Maximum, project FROM projects GROUP BY project)g WHERE MAX(col1) = g.Maximum

        D Offline
        D Offline
        Deques
        wrote on last edited by
        #5

        It still doesnt work. It says an aggregate cant be in WHERE clause An aggregate may not appear in the WHERE clause unless it is in a subquery contained in a HAVING clause or a select list, and the column being aggregated is an outer reference. Tried with Having and it gives a lot other errors

        T 1 Reply Last reply
        0
        • D Deques

          It still doesnt work. It says an aggregate cant be in WHERE clause An aggregate may not appear in the WHERE clause unless it is in a subquery contained in a HAVING clause or a select list, and the column being aggregated is an outer reference. Tried with Having and it gives a lot other errors

          T Offline
          T Offline
          Tobias Schoenig
          wrote on last edited by
          #6

          Sorry my fault, i copied the wrong string: This should be the right thing: SELECT g.Maximum, * from Projects, (SELECT MAX(col1) as Maximum, project FROM projects GROUP BY project)g WHERE col1 = g.Maximum

          D 1 Reply Last reply
          0
          • T Tobias Schoenig

            Sorry my fault, i copied the wrong string: This should be the right thing: SELECT g.Maximum, * from Projects, (SELECT MAX(col1) as Maximum, project FROM projects GROUP BY project)g WHERE col1 = g.Maximum

            D Offline
            D Offline
            Deques
            wrote on last edited by
            #7

            its working :D , just one more thing >.<<br mode="hold" /> some rows in col1 has same value, is it possible to just have one of the values, just take from one row?

            T 1 Reply Last reply
            0
            • D Deques

              its working :D , just one more thing >.<<br mode="hold" /> some rows in col1 has same value, is it possible to just have one of the values, just take from one row?

              T Offline
              T Offline
              Tobias Schoenig
              wrote on last edited by
              #8

              Add a top 1 for example to the first select-statement.

              D 1 Reply Last reply
              0
              • T Tobias Schoenig

                Add a top 1 for example to the first select-statement.

                D Offline
                D Offline
                Deques
                wrote on last edited by
                #9

                top 1 seems not be working good for this I get this result, (partially), from the query you gave me

                3320,9 P110101
                3320,9 P110101
                3220 P110102
                3220 P110102
                529,14 P110301
                3706,2 P111501

                P110102 got two rows, how do i get one of it only?

                T 1 Reply Last reply
                0
                • D Deques

                  top 1 seems not be working good for this I get this result, (partially), from the query you gave me

                  3320,9 P110101
                  3320,9 P110101
                  3220 P110102
                  3220 P110102
                  529,14 P110301
                  3706,2 P111501

                  P110102 got two rows, how do i get one of it only?

                  T Offline
                  T Offline
                  Tobias Schoenig
                  wrote on last edited by
                  #10

                  Add a Group by-statement to the very end of the whole query.

                  D 1 Reply Last reply
                  0
                  • T Tobias Schoenig

                    Add a Group by-statement to the very end of the whole query.

                    D Offline
                    D Offline
                    Deques
                    wrote on last edited by
                    #11

                    it feels like im getting annoying now group by statement might work if i have an aggregate statement somewhere, now there isnt i cant do the group by i am trying distinct now, but i get incorrect syntax SELECT g.Maximum, distinct posnumber from out_pumptable, (SELECT MAX(flow_lpm) as Maximum, posnumber FROM out_pumptable GROUP BY posnumber)g WHERE flow_lpm = g.Maximum this is actual code, posnumber is project

                    T 1 Reply Last reply
                    0
                    • D Deques

                      it feels like im getting annoying now group by statement might work if i have an aggregate statement somewhere, now there isnt i cant do the group by i am trying distinct now, but i get incorrect syntax SELECT g.Maximum, distinct posnumber from out_pumptable, (SELECT MAX(flow_lpm) as Maximum, posnumber FROM out_pumptable GROUP BY posnumber)g WHERE flow_lpm = g.Maximum this is actual code, posnumber is project

                      T Offline
                      T Offline
                      Tobias Schoenig
                      wrote on last edited by
                      #12

                      This works for me: SELECT g.Maximum, out_pumptable.posnumber from out_pumptable, (SELECT MAX(flow_lpm) as Maximum, posnumber FROM out_pumptable GROUP BY posnumber)g WHERE flow_lpm = g.Maximum group by g.Maximum, out_pumptable.posnumber

                      D 1 Reply Last reply
                      0
                      • T Tobias Schoenig

                        This works for me: SELECT g.Maximum, out_pumptable.posnumber from out_pumptable, (SELECT MAX(flow_lpm) as Maximum, posnumber FROM out_pumptable GROUP BY posnumber)g WHERE flow_lpm = g.Maximum group by g.Maximum, out_pumptable.posnumber

                        D Offline
                        D Offline
                        Deques
                        wrote on last edited by
                        #13

                        Thanks a lot and thanks for your time. I appreciate this

                        1 Reply Last reply
                        0
                        • D Deques

                          Here is the table

                          Project col1 col2 col3 col4
                          A 151,46 80 0,29 2
                          A 529,14 83 0,29 1
                          B 3391,3 80 0 2
                          B 3706,2 75 0,06 1
                          C 8685,3 78 2,41 1
                          C 9005,2 71 0 2

                          How do I get the max value for col1 for each project and its other values? ie. project a's max value is 529,14. its others value is 80, 0.29 and 2

                          Project col1 col2 col3 col4
                          A 529,14 83 0,29 1
                          B 3706,2 75 0,06 1
                          C 9005,2 71 0 2

                          I have this query, but it results everything, not only the max value for each project

                          select project, max(col1), col2, col3, col4 from out_pumptable group by project, col2, col3, col4

                          R Offline
                          R Offline
                          Rupert Todd
                          wrote on last edited by
                          #14

                          I believe you need to join the table to a subset of itself. You create a subquery that gives you the values of project and the highest col1 and attribute that to a temptable. You then join you orignal table to your temp table on the values of col1. This should do the trick! SELECT PT.* FROM out_pumptable PT join (select project, max(col1) as MaxVal from out_pumptable group by project) TEMPTABLE1 on TEMPTABLE1.MaxVal = PT.col1

                          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