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. MYSQL RANKING IN A VIEW

MYSQL RANKING IN A VIEW

Scheduled Pinned Locked Moved Database
mysqlquestion
13 Posts 3 Posters 1 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.
  • K KipkoechE

    is it possible to create a view in mysql with ranking row? I tried the following: CREATE VIEW MYVIEW AS SELECT `SAdmNo`, `Average`, CASE WHEN @PREVRANK=`Average` THEN @CURRANK WHEN @PREVRANK :=`Average` THEN @CURRANK :=@CURRANK+1 END AS RANK FROM `total_termaverage_view`, (SELECT @CURRANK :=0,@PREVRANK :=NULL)R ORDER BY `Average` DESC ; and got the following:#1351 - View's SELECT contains a variable or parameter

    Richard DeemingR Offline
    Richard DeemingR Offline
    Richard Deeming
    wrote on last edited by
    #2

    Does Google not work where you are? First result of searching for "mysql ranking view":

    Row Rank in a MySQL View[^]

    Use:

    SELECT t.id,
    t.variety,
    (SELECT COUNT(*) FROM TABLE WHERE id < t.id) +1 AS NUM
    FROM TABLE t


    "These people looked deep within my soul and assigned me a number based on the order in which I joined." - Homer

    "These people looked deep within my soul and assigned me a number based on the order in which I joined" - Homer

    K 1 Reply Last reply
    0
    • Richard DeemingR Richard Deeming

      Does Google not work where you are? First result of searching for "mysql ranking view":

      Row Rank in a MySQL View[^]

      Use:

      SELECT t.id,
      t.variety,
      (SELECT COUNT(*) FROM TABLE WHERE id < t.id) +1 AS NUM
      FROM TABLE t


      "These people looked deep within my soul and assigned me a number based on the order in which I joined." - Homer

      K Offline
      K Offline
      KipkoechE
      wrote on last edited by
      #3

      that one doesnt work ...thats why i resorted to codeProject...

      G 1 Reply Last reply
      0
      • K KipkoechE

        that one doesnt work ...thats why i resorted to codeProject...

        G Offline
        G Offline
        GuyThiebaut
        wrote on last edited by
        #4

        KipkoechE wrote:

        that one doesnt work

        Have a think about what would help us help you - phrases like 'that doesn't work' don't give us any useful information. What helps us help you are - the query you ran together with any error messages you received ;)

        “That which can be asserted without evidence, can be dismissed without evidence.”

        ― Christopher Hitchens

        K 1 Reply Last reply
        0
        • G GuyThiebaut

          KipkoechE wrote:

          that one doesnt work

          Have a think about what would help us help you - phrases like 'that doesn't work' don't give us any useful information. What helps us help you are - the query you ran together with any error messages you received ;)

          “That which can be asserted without evidence, can be dismissed without evidence.”

          ― Christopher Hitchens

          K Offline
          K Offline
          KipkoechE
          wrote on last edited by
          #5

          SELECT t.id, t.variety, (SELECT COUNT(*) FROM TABLE WHERE id < t.id) +1 AS NUM FROM TABLE t; #1064 - You have an error in your SQL syntax; check the manual that corresponds to your MySQL server version for the right syntax to use near 'TABLE WHERE id < t.id) +1 AS NUM FROM TABLE t LIMIT 0, 25' at line 3

          G Richard DeemingR 2 Replies Last reply
          0
          • K KipkoechE

            SELECT t.id, t.variety, (SELECT COUNT(*) FROM TABLE WHERE id < t.id) +1 AS NUM FROM TABLE t; #1064 - You have an error in your SQL syntax; check the manual that corresponds to your MySQL server version for the right syntax to use near 'TABLE WHERE id < t.id) +1 AS NUM FROM TABLE t LIMIT 0, 25' at line 3

            G Offline
            G Offline
            GuyThiebaut
            wrote on last edited by
            #6

            I take it you have a table in your database called TABLE with the columns id and variety within it? My initial guess is - probably not. You will need to take what Richard has given you and adapt it to your table.

            “That which can be asserted without evidence, can be dismissed without evidence.”

            ― Christopher Hitchens

            1 Reply Last reply
            0
            • K KipkoechE

              SELECT t.id, t.variety, (SELECT COUNT(*) FROM TABLE WHERE id < t.id) +1 AS NUM FROM TABLE t; #1064 - You have an error in your SQL syntax; check the manual that corresponds to your MySQL server version for the right syntax to use near 'TABLE WHERE id < t.id) +1 AS NUM FROM TABLE t LIMIT 0, 25' at line 3

              Richard DeemingR Offline
              Richard DeemingR Offline
              Richard Deeming
              wrote on last edited by
              #7

              So you copied the query from the StackOverflow answer verbatim, without making any effort to adapt it to your real table structure, and you expected it to work? :doh:


              "These people looked deep within my soul and assigned me a number based on the order in which I joined." - Homer

              "These people looked deep within my soul and assigned me a number based on the order in which I joined" - Homer

              K 2 Replies Last reply
              0
              • Richard DeemingR Richard Deeming

                So you copied the query from the StackOverflow answer verbatim, without making any effort to adapt it to your real table structure, and you expected it to work? :doh:


                "These people looked deep within my soul and assigned me a number based on the order in which I joined." - Homer

                K Offline
                K Offline
                KipkoechE
                wrote on last edited by
                #8

                I created a table exactly the way it is for testing purposes before customizing to my needs

                1 Reply Last reply
                0
                • Richard DeemingR Richard Deeming

                  So you copied the query from the StackOverflow answer verbatim, without making any effort to adapt it to your real table structure, and you expected it to work? :doh:


                  "These people looked deep within my soul and assigned me a number based on the order in which I joined." - Homer

                  K Offline
                  K Offline
                  KipkoechE
                  wrote on last edited by
                  #9

                  sorry...i had not understand the structure of the table...modified to : create view vb as SELECT t.id, t.variety, (SELECT COUNT(*) FROM t WHERE id < t.id) +1 AS NUM FROM t t Results: id variety num 1 sss 1 2 sdsdssd 1 3 dfddd 1 need num to be : 1,2,3 instead of 1,1,1

                  G 1 Reply Last reply
                  0
                  • K KipkoechE

                    sorry...i had not understand the structure of the table...modified to : create view vb as SELECT t.id, t.variety, (SELECT COUNT(*) FROM t WHERE id < t.id) +1 AS NUM FROM t t Results: id variety num 1 sss 1 2 sdsdssd 1 3 dfddd 1 need num to be : 1,2,3 instead of 1,1,1

                    G Offline
                    G Offline
                    GuyThiebaut
                    wrote on last edited by
                    #10

                    Your table aliases confused me and they may have confused MySQL too, try:

                    create view vb as SELECT t1.id, t1.variety, (SELECT COUNT(*) FROM t WHERE id < t1.id) +1 AS NUM FROM t t1

                    “That which can be asserted without evidence, can be dismissed without evidence.”

                    ― Christopher Hitchens

                    K 2 Replies Last reply
                    0
                    • G GuyThiebaut

                      Your table aliases confused me and they may have confused MySQL too, try:

                      create view vb as SELECT t1.id, t1.variety, (SELECT COUNT(*) FROM t WHERE id < t1.id) +1 AS NUM FROM t t1

                      “That which can be asserted without evidence, can be dismissed without evidence.”

                      ― Christopher Hitchens

                      K Offline
                      K Offline
                      KipkoechE
                      wrote on last edited by
                      #11

                      thanks so much for taking your time to kind me...God's Blessings. i Have learned a lot

                      G 1 Reply Last reply
                      0
                      • K KipkoechE

                        thanks so much for taking your time to kind me...God's Blessings. i Have learned a lot

                        G Offline
                        G Offline
                        GuyThiebaut
                        wrote on last edited by
                        #12

                        Less of the 'God' please although I am grateful for the Blessings part :)

                        “That which can be asserted without evidence, can be dismissed without evidence.”

                        ― Christopher Hitchens

                        1 Reply Last reply
                        0
                        • G GuyThiebaut

                          Your table aliases confused me and they may have confused MySQL too, try:

                          create view vb as SELECT t1.id, t1.variety, (SELECT COUNT(*) FROM t WHERE id < t1.id) +1 AS NUM FROM t t1

                          “That which can be asserted without evidence, can be dismissed without evidence.”

                          ― Christopher Hitchens

                          K Offline
                          K Offline
                          KipkoechE
                          wrote on last edited by
                          #13

                          finally the ranking is on ascending order and need it to be in such a way that the highest marks/value takes position 1: tried this: create view View_Positioning as SELECT t1.`SAdmNo`, t1.`YearAdmitted`,t1.`TermAdmitted`,t1.`CLASSAdmitted` ,t1.`StreamAdmitted`,t1.`OutOfMarks`,t1.`ENGLISH`,t1.`KISWAHILI`,t1.`MATHEMATICS`,t1.`SCIENCE`,t1.`SSR`,t1.`Average`,(SELECT COUNT(*) FROM total_termaverage_view WHERE `Average` IS NOT NULL AND`Average` < t1.`Average`) +1 AS Position FROM total_termaverage_view t1 order by Average ASC ; which gives: Average position 324 1 345 2

                          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