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. Get Max Number for each department

Get Max Number for each department

Scheduled Pinned Locked Moved Database
databasetutorialquestion
6 Posts 4 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.
  • S Offline
    S Offline
    SatyaKeerthi15
    wrote on last edited by
    #1

    Query for Getting Max number. For Example: I have Department and Employee For DeptId 1: Emp No 12,16,19 and For 2: 18,21,17 For 3: 27,28,16 I have to get Max(EmpNo) For department. If I want to get Max(EmpNo) for Dept(1,2) output will be 19,21. How can I get this using sqlserver2005

    M 1 Reply Last reply
    0
    • S SatyaKeerthi15

      Query for Getting Max number. For Example: I have Department and Employee For DeptId 1: Emp No 12,16,19 and For 2: 18,21,17 For 3: 27,28,16 I have to get Max(EmpNo) For department. If I want to get Max(EmpNo) for Dept(1,2) output will be 19,21. How can I get this using sqlserver2005

      M Offline
      M Offline
      Mycroft Holmes
      wrote on last edited by
      #2

      using row_number and partition

      SELECT DeptId ,EmpNo
      FROM (SELECT
      *,
      ROW_NUMBER() OVER(PARTITION BY DeptId ORDER BY EmpNo DESC)HighID FROM tableName) X
      WHERE X.HighID = 1

      Never underestimate the power of human stupidity RAH

      S G L 3 Replies Last reply
      0
      • M Mycroft Holmes

        using row_number and partition

        SELECT DeptId ,EmpNo
        FROM (SELECT
        *,
        ROW_NUMBER() OVER(PARTITION BY DeptId ORDER BY EmpNo DESC)HighID FROM tableName) X
        WHERE X.HighID = 1

        Never underestimate the power of human stupidity RAH

        S Offline
        S Offline
        SatyaKeerthi15
        wrote on last edited by
        #3

        Thank you so much. Its working fine ...

        M 1 Reply Last reply
        0
        • M Mycroft Holmes

          using row_number and partition

          SELECT DeptId ,EmpNo
          FROM (SELECT
          *,
          ROW_NUMBER() OVER(PARTITION BY DeptId ORDER BY EmpNo DESC)HighID FROM tableName) X
          WHERE X.HighID = 1

          Never underestimate the power of human stupidity RAH

          G Offline
          G Offline
          Goutam Patra
          wrote on last edited by
          #4

          Why not in this simple way

          SELECT DEPTID, MAX(EMPNO) FROM TABLENAME GROUP BY DEPTID

          1 Reply Last reply
          0
          • M Mycroft Holmes

            using row_number and partition

            SELECT DeptId ,EmpNo
            FROM (SELECT
            *,
            ROW_NUMBER() OVER(PARTITION BY DeptId ORDER BY EmpNo DESC)HighID FROM tableName) X
            WHERE X.HighID = 1

            Never underestimate the power of human stupidity RAH

            L Offline
            L Offline
            Luc Pattyn
            wrote on last edited by
            #5

            that looks overly complex. wouldn't the following work (I think it does in MySQL):

            SELECT DeptId,Max(EmpNo) 'maxEmpNo' FROM table1 GROUP BY DeptId ORDER BY DeptId

            :)

            Luc Pattyn [Forum Guidelines] [Why QA sucks] [My Articles] Nil Volentibus Arduum

            Please use <PRE> tags for code snippets, they preserve indentation, and improve readability.

            1 Reply Last reply
            0
            • S SatyaKeerthi15

              Thank you so much. Its working fine ...

              M Offline
              M Offline
              Mycroft Holmes
              wrote on last edited by
              #6

              See the replies from Luc and Goutam, my answer is NOT the simplest way. Comes from applying the wrong tool for the job.

              Never underestimate the power of human stupidity RAH

              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