UNION Clause Question 
Author Message
 UNION Clause Question
Is it possible to get just the first row from a Union Query?

Something like Top 1 ?
Thanks.



Wed, 04 Aug 2004 23:51:59 GMT
 UNION Clause Question

Jose,

You can do it with a subquery, altho' there may be a better way;;

SELECT TOP 1 SQ.*
FROM
    (
        SELECT c1 from T1
        UNION
        SELECT c1 from T2
    ) AS SQ
ORDER BY SQ.c1

HTH,
--
--
Kind Regards,

Robert A. Ellis, MCSD
Software Developer


Quote:
> Is it possible to get just the first row from a Union Query?

> Something like Top 1 ?
> Thanks.



Thu, 05 Aug 2004 00:53:32 GMT
 UNION Clause Question
This looks fine to me.

One thing, though.  If you use UNION you may suffer a performance hit as
duplicates are eliminated.  Unless you know you need UNION, use UNION
ALL.

Steve Kass
Drew University

Quote:

> Jose,

> You can do it with a subquery, altho' there may be a better way;;

> SELECT TOP 1 SQ.*
> FROM
>     (
>         SELECT c1 from T1
>         UNION
>         SELECT c1 from T2
>     ) AS SQ
> ORDER BY SQ.c1

> HTH,
> --
> --
> Kind Regards,

> Robert A. Ellis, MCSD
> Software Developer



> > Is it possible to get just the first row from a Union Query?

> > Something like Top 1 ?
> > Thanks.



Thu, 05 Aug 2004 01:11:23 GMT
 UNION Clause Question
select top 1 *
  from (
    select * from (select top 1 * from tbl order by somecol) as a
     union all -- you can remove ALL if required
    select * from (select top 1 * from tbl order by somecol) as b
     ) as c
 order by somecol

--
Umachandar Jayachandran
SQL Resources at http://www.umachandar.com/resources.htm
( Please reply only to newsgroup. )



Thu, 05 Aug 2004 01:35:36 GMT
 UNION Clause Question
Thanks!


Quote:
> Jose,

> You can do it with a subquery, altho' there may be a better way;;

> SELECT TOP 1 SQ.*
> FROM
>     (
>         SELECT c1 from T1
>         UNION
>         SELECT c1 from T2
>     ) AS SQ
> ORDER BY SQ.c1

> HTH,
> --
> --
> Kind Regards,

> Robert A. Ellis, MCSD
> Software Developer



> > Is it possible to get just the first row from a Union Query?

> > Something like Top 1 ?
> > Thanks.



Thu, 05 Aug 2004 01:34:48 GMT
 
 [ 5 post ] 

 Relevant Pages 

1. UNION clause questions

2. UNION clause syntax question

3. SQL question: using UNION in FROM clause with DB2 V5

4. UNION Clause unsupporter?

5. Order By clause with UNION : SQL Server 2000

6. Using UNION and ORDER BY clauses

7. INTO Clause with UNION?

8. union in where clause causing problems?

9. Stored Procedure - TOP/UNION clauses

10. Union Clause help needed

11. Union Clause

12. Parameterised queries and UNION clause


 
Powered by phpBB® Forum Software