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 select 1 entry without using "top 1" in SQL query?

How to select 1 entry without using "top 1" in SQL query?

Scheduled Pinned Locked Moved Database
databasetutorialquestionlearning
5 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.
  • S Offline
    S Offline
    Segal
    wrote on last edited by
    #1

    I'm learning writing SQL query now. If I, for example, want to retrieve information from 3 joining tables (T1, T2, T3), and only want to return the latest failed task and task name is 'Mailboxes', I wrote the following query: select top 1 TT.task_type_desc, TL.starting_time, AAC.type_code_value from tasklog as TL inner join task_type as TT on TL.task_type_id=TT.task_type_id inner join aatype_code as AAC on AAC.type_code_id=TL.task_status and AAC.type_code_class='task_status' where AAC.type_code_value='Failed' and TT.task_type_desc='Mailboxes' order by TL.starting_time desc It gave me the required result. But how to do this query without using "top 1" and still get the same result? Thanks!

    P 1 Reply Last reply
    0
    • S Segal

      I'm learning writing SQL query now. If I, for example, want to retrieve information from 3 joining tables (T1, T2, T3), and only want to return the latest failed task and task name is 'Mailboxes', I wrote the following query: select top 1 TT.task_type_desc, TL.starting_time, AAC.type_code_value from tasklog as TL inner join task_type as TT on TL.task_type_id=TT.task_type_id inner join aatype_code as AAC on AAC.type_code_id=TL.task_status and AAC.type_code_class='task_status' where AAC.type_code_value='Failed' and TT.task_type_desc='Mailboxes' order by TL.starting_time desc It gave me the required result. But how to do this query without using "top 1" and still get the same result? Thanks!

      P Offline
      P Offline
      Paul Riley
      wrote on last edited by
      #2

      If it ain't broke... why do you want to fix it? Paul

      A 1 Reply Last reply
      0
      • P Paul Riley

        If it ain't broke... why do you want to fix it? Paul

        A Offline
        A Offline
        Anonymous
        wrote on last edited by
        #3

        Because, this is just part of my query, if I use top 1, then, I can't get other entries required by some other conditions. So, basically, on that part I mentioned in my previous mail, I just want the last failed entry with any task name. Any suggestions?

        J N 2 Replies Last reply
        0
        • A Anonymous

          Because, this is just part of my query, if I use top 1, then, I can't get other entries required by some other conditions. So, basically, on that part I mentioned in my previous mail, I just want the last failed entry with any task name. Any suggestions?

          J Offline
          J Offline
          Jon Hulatt
          wrote on last edited by
          #4

          Can't really answer without seeing the rest of your query, but you may be able to achieve what you're trying with a subquery:- select a.a,b.b,c.c from table_a a (nolock) inner join table_b b (nolock) inner join ( select top 1 * from table_c (nolock)) c Signature space for rent. Apply by email to....

          1 Reply Last reply
          0
          • A Anonymous

            Because, this is just part of my query, if I use top 1, then, I can't get other entries required by some other conditions. So, basically, on that part I mentioned in my previous mail, I just want the last failed entry with any task name. Any suggestions?

            N Offline
            N Offline
            Nick Parker
            wrote on last edited by
            #5

            Anonymous wrote: Because, this is just part of my query, if I use top 1, then, I can't get other entries required by some other conditions. Use the Top 1, you can use the UNION statement to make multiple selects like this to match all of your criteria:

            SELECT a, b, c, d FROM table_one WHERE a = 5

            UNION

            SELECT a, b, c, e FROM table_one WHERE a = 6

            UNION

            SELECT a, b, c, f FROM table_one WHERE a = 7

            UNION

            SELECT a, b, c, g FROM table_one WHERE a = 8

            HTH :) Nick Parker
            **The goal of Computer Science is to build something that will last at least until we've finished building it. - Unknown


            **

            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