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. how to write query

how to write query

Scheduled Pinned Locked Moved Database
databasesql-serversysadminhelptutorial
10 Posts 5 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.
  • H Offline
    H Offline
    harithadotnet
    wrote on last edited by
    #1

    Hi I have created one table in sql server as sno name desc 5 suma dckdfjdskfldkf;dsf 3 suma lklkjdkjfkjfdjbbbbb 8 suma jsakdksladksadks 4 manu nsdskdjsdsadl 2 manu klswkqewr 1 manu shdjsdksd like this.... now i want to dispaly one record fro each user (i.e the record which is having max sno for each user) i.e 8 suma jsakdksladksadks 4 manu nsdskdjsdsadl how to write query for this plz help me Thanks in advance

    Haritha

    C P D 5 Replies Last reply
    0
    • H harithadotnet

      Hi I have created one table in sql server as sno name desc 5 suma dckdfjdskfldkf;dsf 3 suma lklkjdkjfkjfdjbbbbb 8 suma jsakdksladksadks 4 manu nsdskdjsdsadl 2 manu klswkqewr 1 manu shdjsdksd like this.... now i want to dispaly one record fro each user (i.e the record which is having max sno for each user) i.e 8 suma jsakdksladksadks 4 manu nsdskdjsdsadl how to write query for this plz help me Thanks in advance

      Haritha

      C Offline
      C Offline
      Colin Angus Mackay
      wrote on last edited by
      #2

      Here is the bit that will get the highest sno for each user SELECT MAX(sno) AS sno, name FROM MyTable GROUP BY name Add that as a subquery and join on to it like another table. Like this:

      SELECT s.sno, s.name, m.desc
      FROM MyTable AS m
      INNER JOIN (SELECT MAX(sno) AS sno, name FROM MyTable GROUP BY name) AS s
      ON s.sno = m.sno AND s.name = m.name


      Upcoming events: * Glasgow: Mock Objects, SQL Server CLR Integration, Reporting Services, db4o, Dependency Injection with Spring ... "I wouldn't say boo to a goose. I'm not a coward, I just realise that it would be largely pointless." Ready to Give up - Your help will be much appreciated. My website

      1 Reply Last reply
      0
      • H harithadotnet

        Hi I have created one table in sql server as sno name desc 5 suma dckdfjdskfldkf;dsf 3 suma lklkjdkjfkjfdjbbbbb 8 suma jsakdksladksadks 4 manu nsdskdjsdsadl 2 manu klswkqewr 1 manu shdjsdksd like this.... now i want to dispaly one record fro each user (i.e the record which is having max sno for each user) i.e 8 suma jsakdksladksadks 4 manu nsdskdjsdsadl how to write query for this plz help me Thanks in advance

        Haritha

        P Offline
        P Offline
        PrakashBhaskar
        wrote on last edited by
        #3

        for this you have to use corolated queries following the example SELECT sno, name, desc FROM WHERE sno=(SELECT MAX(sno) FROM

        Thanks Warm Regards Prakash-B

        C 1 Reply Last reply
        0
        • P PrakashBhaskar

          for this you have to use corolated queries following the example SELECT sno, name, desc FROM WHERE sno=(SELECT MAX(sno) FROM

          Thanks Warm Regards Prakash-B

          C Offline
          C Offline
          Colin Angus Mackay
          wrote on last edited by
          #4

          PrakashBhaskar wrote:

          for this you have to use corolated queries following the example SELECT sno, name, desc FROM <table name> WHERE sno=(SELECT MAX(sno) FROM <table mame) I think ur problem has solved

          That will return the row "8 suma jsakdksladksadks" only.


          Upcoming events: * Glasgow: Mock Objects, SQL Server CLR Integration, Reporting Services, db4o, Dependency Injection with Spring ... "I wouldn't say boo to a goose. I'm not a coward, I just realise that it would be largely pointless." Ready to Give up - Your help will be much appreciated. My website

          P 1 Reply Last reply
          0
          • C Colin Angus Mackay

            PrakashBhaskar wrote:

            for this you have to use corolated queries following the example SELECT sno, name, desc FROM <table name> WHERE sno=(SELECT MAX(sno) FROM <table mame) I think ur problem has solved

            That will return the row "8 suma jsakdksladksadks" only.


            Upcoming events: * Glasgow: Mock Objects, SQL Server CLR Integration, Reporting Services, db4o, Dependency Injection with Spring ... "I wouldn't say boo to a goose. I'm not a coward, I just realise that it would be largely pointless." Ready to Give up - Your help will be much appreciated. My website

            P Offline
            P Offline
            PrakashBhaskar
            wrote on last edited by
            #5

            i am sorry i have mis-understood the question

            Thanks Warm Regards Prakash-B

            1 Reply Last reply
            0
            • H harithadotnet

              Hi I have created one table in sql server as sno name desc 5 suma dckdfjdskfldkf;dsf 3 suma lklkjdkjfkjfdjbbbbb 8 suma jsakdksladksadks 4 manu nsdskdjsdsadl 2 manu klswkqewr 1 manu shdjsdksd like this.... now i want to dispaly one record fro each user (i.e the record which is having max sno for each user) i.e 8 suma jsakdksladksadks 4 manu nsdskdjsdsadl how to write query for this plz help me Thanks in advance

              Haritha

              D Offline
              D Offline
              Dipti D Jadhav
              wrote on last edited by
              #6

              You can use following query select max(no),name from trial1 group by name If have any questions, thn let me know

              C 1 Reply Last reply
              0
              • H harithadotnet

                Hi I have created one table in sql server as sno name desc 5 suma dckdfjdskfldkf;dsf 3 suma lklkjdkjfkjfdjbbbbb 8 suma jsakdksladksadks 4 manu nsdskdjsdsadl 2 manu klswkqewr 1 manu shdjsdksd like this.... now i want to dispaly one record fro each user (i.e the record which is having max sno for each user) i.e 8 suma jsakdksladksadks 4 manu nsdskdjsdsadl how to write query for this plz help me Thanks in advance

                Haritha

                D Offline
                D Offline
                Dipti D Jadhav
                wrote on last edited by
                #7

                You can use following query select max(no),name from trial1 group by name If have any questions, thn let me know Dipti Jadhav

                S 1 Reply Last reply
                0
                • D Dipti D Jadhav

                  You can use following query select max(no),name from trial1 group by name If have any questions, thn let me know

                  C Offline
                  C Offline
                  Colin Angus Mackay
                  wrote on last edited by
                  #8

                  d_d_jadhav wrote:

                  You can use following query select max(no),name from trial1 group by name If have any questions, thn let me know

                  No, that will result in:

                  8 suma

                  only. Which does not match the output the OP requested.


                  Upcoming events: * Glasgow: Mock Objects, SQL Server CLR Integration, Reporting Services, db4o, Dependency Injection with Spring ... "I wouldn't say boo to a goose. I'm not a coward, I just realise that it would be largely pointless." Ready to Give up - Your help will be much appreciated. My website

                  1 Reply Last reply
                  0
                  • D Dipti D Jadhav

                    You can use following query select max(no),name from trial1 group by name If have any questions, thn let me know Dipti Jadhav

                    S Offline
                    S Offline
                    Sathesh Sakthivel
                    wrote on last edited by
                    #9

                    One post is enough.

                    Regards, Satips.:rose: Don't walk in front of me, I may not follow; Don't walk behind me, I may not lead; Walk beside me, and just be my friend. - Albert Camus

                    1 Reply Last reply
                    0
                    • H harithadotnet

                      Hi I have created one table in sql server as sno name desc 5 suma dckdfjdskfldkf;dsf 3 suma lklkjdkjfkjfdjbbbbb 8 suma jsakdksladksadks 4 manu nsdskdjsdsadl 2 manu klswkqewr 1 manu shdjsdksd like this.... now i want to dispaly one record fro each user (i.e the record which is having max sno for each user) i.e 8 suma jsakdksladksadks 4 manu nsdskdjsdsadl how to write query for this plz help me Thanks in advance

                      Haritha

                      D Offline
                      D Offline
                      Dipti D Jadhav
                      wrote on last edited by
                      #10

                      The answer for ur question is here select sno,name,[desc] from trial1 where convert(varchar(50),sno)+name in(select convert(varchar(50),max(sno))+name from trial1 group by name) Please let me know, if u have any questions/comments.

                      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