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
CODE PROJECT For Those Who Code
  • Home
  • Articles
  • FAQ
Community
  1. Home
  2. Database & SysAdmin
  3. Database
  4. Union HELP!!!!!

Union HELP!!!!!

Scheduled Pinned Locked Moved Database
questionhelpsharepoint
9 Posts 6 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.
  • C Offline
    C Offline
    cdietschrun
    wrote on last edited by
    #1

    CREATE PROCEDURE dbo.sp_GetNewestTemplates

    AS

    SELECT TOP 1 *
    FROM TBL_SWBOM_CHECKLIST_TEMPLATES
    WHERE TEMPLATEOWNER = 'Joe Smith'
    order by lastchange desc
    SELECT TOP 1 *
    FROM TBL_SWBOM_CHECKLIST_TEMPLATES
    WHERE TEMPLATEOWNER = 'Jane Smith'
    order by lastchange desc
    SELECT TOP 1 *
    FROM TBL_SWBOM_CHECKLIST_TEMPLATES
    WHERE TEMPLATEOWNER = 'GI Jane'
    order by lastchange desc

    GO

    WHY does that code give no errors but this does:

    CREATE PROCEDURE dbo.sp_GetNewestTemplates

    AS

    SELECT TOP 1 *
    FROM TBL_SWBOM_CHECKLIST_TEMPLATES
    WHERE TEMPLATEOWNER = 'Joe Smith'
    order by lastchange desc
    union
    SELECT TOP 1 *
    FROM TBL_SWBOM_CHECKLIST_TEMPLATES
    WHERE TEMPLATEOWNER = 'Jane Smith'
    order by lastchange desc
    union
    SELECT TOP 1 *
    FROM TBL_SWBOM_CHECKLIST_TEMPLATES
    WHERE TEMPLATEOWNER = 'GI Jane'
    order by lastchange desc

    GO

    I need this to work! What am I missing, and how can I get around this ridiculously annoying error? All it says is "Error 156: Syntax error near union."

    V L D 3 Replies Last reply
    0
    • C cdietschrun

      CREATE PROCEDURE dbo.sp_GetNewestTemplates

      AS

      SELECT TOP 1 *
      FROM TBL_SWBOM_CHECKLIST_TEMPLATES
      WHERE TEMPLATEOWNER = 'Joe Smith'
      order by lastchange desc
      SELECT TOP 1 *
      FROM TBL_SWBOM_CHECKLIST_TEMPLATES
      WHERE TEMPLATEOWNER = 'Jane Smith'
      order by lastchange desc
      SELECT TOP 1 *
      FROM TBL_SWBOM_CHECKLIST_TEMPLATES
      WHERE TEMPLATEOWNER = 'GI Jane'
      order by lastchange desc

      GO

      WHY does that code give no errors but this does:

      CREATE PROCEDURE dbo.sp_GetNewestTemplates

      AS

      SELECT TOP 1 *
      FROM TBL_SWBOM_CHECKLIST_TEMPLATES
      WHERE TEMPLATEOWNER = 'Joe Smith'
      order by lastchange desc
      union
      SELECT TOP 1 *
      FROM TBL_SWBOM_CHECKLIST_TEMPLATES
      WHERE TEMPLATEOWNER = 'Jane Smith'
      order by lastchange desc
      union
      SELECT TOP 1 *
      FROM TBL_SWBOM_CHECKLIST_TEMPLATES
      WHERE TEMPLATEOWNER = 'GI Jane'
      order by lastchange desc

      GO

      I need this to work! What am I missing, and how can I get around this ridiculously annoying error? All it says is "Error 156: Syntax error near union."

      V Offline
      V Offline
      Vimalsoft Pty Ltd
      wrote on last edited by
      #2

      what error are you getting ?

      Vuyiswa Maseko, Few companies that installed computers to reduce the employment of clerks have realized their expectations.... They now need more and more expensive clerks even though they call them "Developers" or "Programmers." C#/VB.NET/ASP.NET/SQL7/2000/2005/2008 http://www.vuyiswamaseko.somee.com http://www.vuyiswamaseko.tiyaneProperties.co.za vuyiswa@its.co.za http://www.itsabacus.co.za/itsabacus/

      1 Reply Last reply
      0
      • C cdietschrun

        CREATE PROCEDURE dbo.sp_GetNewestTemplates

        AS

        SELECT TOP 1 *
        FROM TBL_SWBOM_CHECKLIST_TEMPLATES
        WHERE TEMPLATEOWNER = 'Joe Smith'
        order by lastchange desc
        SELECT TOP 1 *
        FROM TBL_SWBOM_CHECKLIST_TEMPLATES
        WHERE TEMPLATEOWNER = 'Jane Smith'
        order by lastchange desc
        SELECT TOP 1 *
        FROM TBL_SWBOM_CHECKLIST_TEMPLATES
        WHERE TEMPLATEOWNER = 'GI Jane'
        order by lastchange desc

        GO

        WHY does that code give no errors but this does:

        CREATE PROCEDURE dbo.sp_GetNewestTemplates

        AS

        SELECT TOP 1 *
        FROM TBL_SWBOM_CHECKLIST_TEMPLATES
        WHERE TEMPLATEOWNER = 'Joe Smith'
        order by lastchange desc
        union
        SELECT TOP 1 *
        FROM TBL_SWBOM_CHECKLIST_TEMPLATES
        WHERE TEMPLATEOWNER = 'Jane Smith'
        order by lastchange desc
        union
        SELECT TOP 1 *
        FROM TBL_SWBOM_CHECKLIST_TEMPLATES
        WHERE TEMPLATEOWNER = 'GI Jane'
        order by lastchange desc

        GO

        I need this to work! What am I missing, and how can I get around this ridiculously annoying error? All it says is "Error 156: Syntax error near union."

        L Offline
        L Offline
        Lost User
        wrote on last edited by
        #3

        What happens when you remove the ORDER BY?

        C 1 Reply Last reply
        0
        • L Lost User

          What happens when you remove the ORDER BY?

          C Offline
          C Offline
          cdietschrun
          wrote on last edited by
          #4

          The syntax works, but I have 3 disjoint selections as a result. I am looking to save one of the pieces of data in each row to a variable in VB.NET, if you can tell me how to do that without the union of the 3 results then that is fine. At the moment I don't know how

          L 1 Reply Last reply
          0
          • C cdietschrun

            CREATE PROCEDURE dbo.sp_GetNewestTemplates

            AS

            SELECT TOP 1 *
            FROM TBL_SWBOM_CHECKLIST_TEMPLATES
            WHERE TEMPLATEOWNER = 'Joe Smith'
            order by lastchange desc
            SELECT TOP 1 *
            FROM TBL_SWBOM_CHECKLIST_TEMPLATES
            WHERE TEMPLATEOWNER = 'Jane Smith'
            order by lastchange desc
            SELECT TOP 1 *
            FROM TBL_SWBOM_CHECKLIST_TEMPLATES
            WHERE TEMPLATEOWNER = 'GI Jane'
            order by lastchange desc

            GO

            WHY does that code give no errors but this does:

            CREATE PROCEDURE dbo.sp_GetNewestTemplates

            AS

            SELECT TOP 1 *
            FROM TBL_SWBOM_CHECKLIST_TEMPLATES
            WHERE TEMPLATEOWNER = 'Joe Smith'
            order by lastchange desc
            union
            SELECT TOP 1 *
            FROM TBL_SWBOM_CHECKLIST_TEMPLATES
            WHERE TEMPLATEOWNER = 'Jane Smith'
            order by lastchange desc
            union
            SELECT TOP 1 *
            FROM TBL_SWBOM_CHECKLIST_TEMPLATES
            WHERE TEMPLATEOWNER = 'GI Jane'
            order by lastchange desc

            GO

            I need this to work! What am I missing, and how can I get around this ridiculously annoying error? All it says is "Error 156: Syntax error near union."

            D Offline
            D Offline
            David Skelly
            wrote on last edited by
            #5

            You are specifying ORDER BY too many times. You do not need to specify ORDER BY for each of the sub-queries; you only need to specify it once for the entire UNION result set.

            SELECT TOP 1 *
            FROM TBL_SWBOM_CHECKLIST_TEMPLATES
            WHERE TEMPLATEOWNER = 'Joe Smith'
            union
            SELECT TOP 1 *
            FROM TBL_SWBOM_CHECKLIST_TEMPLATES
            WHERE TEMPLATEOWNER = 'Jane Smith'
            union
            SELECT TOP 1 *
            FROM TBL_SWBOM_CHECKLIST_TEMPLATES
            WHERE TEMPLATEOWNER = 'GI Jane'
            order by lastchange desc

            C 1 Reply Last reply
            0
            • C cdietschrun

              The syntax works, but I have 3 disjoint selections as a result. I am looking to save one of the pieces of data in each row to a variable in VB.NET, if you can tell me how to do that without the union of the 3 results then that is fine. At the moment I don't know how

              L Offline
              L Offline
              Lost User
              wrote on last edited by
              #6

              The UNION concatenates the results, but you can use this result as if it were a query again, e.g.;

              SELECT * FROM
              (
              SELECT TOP 1 [name] FROM sysobjects WHERE [name] LIKE ('sysh%')
              UNION
              SELECT TOP 2 [name] FROM sysobjects WHERE [name] LIKE ('sys%')
              UNION
              SELECT TOP 1 [name] FROM sysobjects WHERE [id] < 0
              ) AS myCTE ORDER BY [name]

              1 Reply Last reply
              0
              • D David Skelly

                You are specifying ORDER BY too many times. You do not need to specify ORDER BY for each of the sub-queries; you only need to specify it once for the entire UNION result set.

                SELECT TOP 1 *
                FROM TBL_SWBOM_CHECKLIST_TEMPLATES
                WHERE TEMPLATEOWNER = 'Joe Smith'
                union
                SELECT TOP 1 *
                FROM TBL_SWBOM_CHECKLIST_TEMPLATES
                WHERE TEMPLATEOWNER = 'Jane Smith'
                union
                SELECT TOP 1 *
                FROM TBL_SWBOM_CHECKLIST_TEMPLATES
                WHERE TEMPLATEOWNER = 'GI Jane'
                order by lastchange desc

                C Offline
                C Offline
                cdietschrun
                wrote on last edited by
                #7

                For Joe Smith, I have multiple rows that I want only the row that has both the name (WHERE TEMPLATEOWNER = 'Joe Smith') AND the row where the LASTCHANGE is the newest. So that first select should return the newest entry by Joe Smith. Then the second and third selects should find the one row that is the most recently updated by Jane and GI. The way it stands now, query analyzer shows three disjoint results. I need a joint record set. The removal of the order by in each select breaks the function.

                W 1 Reply Last reply
                0
                • C cdietschrun

                  For Joe Smith, I have multiple rows that I want only the row that has both the name (WHERE TEMPLATEOWNER = 'Joe Smith') AND the row where the LASTCHANGE is the newest. So that first select should return the newest entry by Joe Smith. Then the second and third selects should find the one row that is the most recently updated by Jane and GI. The way it stands now, query analyzer shows three disjoint results. I need a joint record set. The removal of the order by in each select breaks the function.

                  W Offline
                  W Offline
                  WoutL
                  wrote on last edited by
                  #8

                  Try this. It should do exactly what you want

                  Select * from (
                  SELECT TOP 1 *
                  FROM TBL_SWBOM_CHECKLIST_TEMPLATES
                  WHERE TEMPLATEOWNER = 'Joe Smith'
                  order by lastchange desc
                  ) d
                  union
                  Select * from (
                  SELECT TOP 1 *
                  FROM TBL_SWBOM_CHECKLIST_TEMPLATES
                  WHERE TEMPLATEOWNER = 'Jane Smith'
                  order by lastchange desc
                  ) d
                  union
                  Select * from (
                  SELECT TOP 1 *
                  FROM TBL_SWBOM_CHECKLIST_TEMPLATES
                  WHERE TEMPLATEOWNER = 'GI Jane'
                  order by lastchange desc
                  ) d

                  Wout Louwers

                  T 1 Reply Last reply
                  0
                  • W WoutL

                    Try this. It should do exactly what you want

                    Select * from (
                    SELECT TOP 1 *
                    FROM TBL_SWBOM_CHECKLIST_TEMPLATES
                    WHERE TEMPLATEOWNER = 'Joe Smith'
                    order by lastchange desc
                    ) d
                    union
                    Select * from (
                    SELECT TOP 1 *
                    FROM TBL_SWBOM_CHECKLIST_TEMPLATES
                    WHERE TEMPLATEOWNER = 'Jane Smith'
                    order by lastchange desc
                    ) d
                    union
                    Select * from (
                    SELECT TOP 1 *
                    FROM TBL_SWBOM_CHECKLIST_TEMPLATES
                    WHERE TEMPLATEOWNER = 'GI Jane'
                    order by lastchange desc
                    ) d

                    Wout Louwers

                    T Offline
                    T Offline
                    Tauseef A
                    wrote on last edited by
                    #9

                    just remove column name in order by clause simly place column index .

                    Tauseef A Khan MCP Dotnet framework 2.0.

                    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