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. Top 1 distinct or something like that

Top 1 distinct or something like that

Scheduled Pinned Locked Moved Database
databasequestion
8 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.
  • P Offline
    P Offline
    Paul E Davies
    wrote on last edited by
    #1

    Hi, Just trying to get some test data out of one our DB - We have a table that has a Typeid (int) column and a details (nvarchar(1000)) column What I'd like to do is get the top 1 details for each typeid (there are about 40 distinct typeid) so something along the lines of

    select top 1 typeid,details from auditlog group by typeid,details,

    But where the query returns the top 1 details column for each eventtypeid like Typid details 1 details1 2 details2 .... 40 details40 Does that make sense?

    B D S 3 Replies Last reply
    0
    • P Paul E Davies

      Hi, Just trying to get some test data out of one our DB - We have a table that has a Typeid (int) column and a details (nvarchar(1000)) column What I'd like to do is get the top 1 details for each typeid (there are about 40 distinct typeid) so something along the lines of

      select top 1 typeid,details from auditlog group by typeid,details,

      But where the query returns the top 1 details column for each eventtypeid like Typid details 1 details1 2 details2 .... 40 details40 Does that make sense?

      B Offline
      B Offline
      Blue_Boy
      wrote on last edited by
      #2

      Use distinct

      Select distinct typeid,details from auditlog order by typeid


      I Love T-SQL "Don't torture yourself,let the life to do it for you." If my post helps you kindly save my time by voting my post. www.cacttus.com

      P 1 Reply Last reply
      0
      • B Blue_Boy

        Use distinct

        Select distinct typeid,details from auditlog order by typeid


        I Love T-SQL "Don't torture yourself,let the life to do it for you." If my post helps you kindly save my time by voting my post. www.cacttus.com

        P Offline
        P Offline
        Paul E Davies
        wrote on last edited by
        #3

        Thanks for the reply. I don't think I made it clear in my original post so apologies, but the details column could be anything (it's actually an XML fragment) so distinct will bring back all rows anyway or at least more than 1 row per eventid. I think I'm after a sort of "top 1 distinct" combination.

        B 1 Reply Last reply
        0
        • P Paul E Davies

          Thanks for the reply. I don't think I made it clear in my original post so apologies, but the details column could be anything (it's actually an XML fragment) so distinct will bring back all rows anyway or at least more than 1 row per eventid. I think I'm after a sort of "top 1 distinct" combination.

          B Offline
          B Offline
          Blue_Boy
          wrote on last edited by
          #4

          So, can you post data structure in your table and result which you want to get from that data?


          I Love T-SQL "Don't torture yourself,let the life to do it for you." If my post helps you kindly save my time by voting my post. www.cacttus.com

          1 Reply Last reply
          0
          • P Paul E Davies

            Hi, Just trying to get some test data out of one our DB - We have a table that has a Typeid (int) column and a details (nvarchar(1000)) column What I'd like to do is get the top 1 details for each typeid (there are about 40 distinct typeid) so something along the lines of

            select top 1 typeid,details from auditlog group by typeid,details,

            But where the query returns the top 1 details column for each eventtypeid like Typid details 1 details1 2 details2 .... 40 details40 Does that make sense?

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

            What I'd like to do is get the top 1 details for each typeid How do you know that a particular detail is 'top' among its peer details? Does picking a particular item matter at all? If it does not matter which row you pick as your 'top', try this:

            select typeid, min(details) from auditlog group by typeid

            it will decide that a detail is 'top' if its description comes first alphabetically. If this is not what you need, you would have to provide another column to determine which detail among many possibilities for the same typeid is to be selected.

            D P 2 Replies Last reply
            0
            • P Paul E Davies

              Hi, Just trying to get some test data out of one our DB - We have a table that has a Typeid (int) column and a details (nvarchar(1000)) column What I'd like to do is get the top 1 details for each typeid (there are about 40 distinct typeid) so something along the lines of

              select top 1 typeid,details from auditlog group by typeid,details,

              But where the query returns the top 1 details column for each eventtypeid like Typid details 1 details1 2 details2 .... 40 details40 Does that make sense?

              S Offline
              S Offline
              Simon_Whale
              wrote on last edited by
              #6

              Without sample data its hard to give you a best solution, give this a try

              select distinct a.typeid, (select top 1 details from auditlog where typeid = a.typeid) [details]
              from auditlog

              Lobster Thermidor aux crevettes with a Mornay sauce, served in a Provençale manner with shallots and aubergines, garnished with truffle pate, brandy and a fried egg on top and Spam - Monty Python Spam Sketch

              1 Reply Last reply
              0
              • D dasblinkenlight

                What I'd like to do is get the top 1 details for each typeid How do you know that a particular detail is 'top' among its peer details? Does picking a particular item matter at all? If it does not matter which row you pick as your 'top', try this:

                select typeid, min(details) from auditlog group by typeid

                it will decide that a detail is 'top' if its description comes first alphabetically. If this is not what you need, you would have to provide another column to determine which detail among many possibilities for the same typeid is to be selected.

                D Offline
                D Offline
                David Mujica
                wrote on last edited by
                #7

                I agree with the above post. You need a column like, timestamp, along with your other columns to group the TypeIds. This way you could do something like max(timestamp) and get the most recent logged event. Good luck.

                1 Reply Last reply
                0
                • D dasblinkenlight

                  What I'd like to do is get the top 1 details for each typeid How do you know that a particular detail is 'top' among its peer details? Does picking a particular item matter at all? If it does not matter which row you pick as your 'top', try this:

                  select typeid, min(details) from auditlog group by typeid

                  it will decide that a detail is 'top' if its description comes first alphabetically. If this is not what you need, you would have to provide another column to determine which detail among many possibilities for the same typeid is to be selected.

                  P Offline
                  P Offline
                  Paul E Davies
                  wrote on last edited by
                  #8

                  This did it thanks. apologies for the vagueness, well I knew what I meant. To hopefully clear up any questions - the table can have many thousands of rows each one holds the details of application events used for auditing purposes. There are about 40 event types that are logged (eventid) and the details column holds the data associated with a particular event e.g

                  this one describes a simple page view. Other columns are things like hold details of user ids, timestamps etc but in this instance were not required I jut needed one example of each event type to test a reporting application. Reading that I could have been clearer in my original post. Thanks again everyone.

                  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