Results 1 to 4 of 4

Thread: Using UNION

  1. #1
    Join Date
    Apr 2007
    Posts
    31

    Unanswered: Using UNION

    Is it possible to use UNION in a DB2 query? I'm a SQL Server person in need of a translation of the following query to something that DB2 will like:

    SELECT
    “FMLOG Total” AS CountCriteria,
    Count(*) AS RecordCount
    FROM
    CBDBOW.FUEL_MGLOG

    UNION

    SELECT
    “FMLOG Less MC901 AS CountCriteria,
    Count(*) AS RecordCount
    FROM
    CBDBOW.FUEL_MGLOG
    WHERE fm_sc_srvc_cntr_cd = 'mc901'

    UNION

    SELECT
    “PRMRLOG Less AT900 AT950 VR900s’ AS CountCriteria,
    Count(*) AS RecordCount
    FROM
    CBDBOW.PRMRLOG
    WHERE
    prmlog_sec_last IN ('AT900', 'AT950')
    OR
    prmlog_sec_last LIKE 'vr90%'

    ******
    I think the DB2 version is 9, and the O/S is AIX version 5.3.0

    Thank you for your help!

    cdun2

  2. #2
    Join Date
    Jun 2003
    Location
    Toronto, Canada
    Posts
    5,516
    Provided Answers: 1
    Quote Originally Posted by cdun2
    Is it possible to use UNION in a DB2 query?
    Is there a reason for you to think that it may not be possible? It is standard SQL, after all.
    ---
    "It does not work" is not a valid problem statement.

  3. #3
    Join Date
    May 2003
    Location
    USA
    Posts
    5,737
    The DB2 data types of the columns in the select list must be the same in all sections of the union (all first columns must be the same, all second columns must be the same, etc).

    If you use UNION, DB2 will eliminate any duplicate rows (based on the columns in the select list) when the answer set for all the sections of UNION are combined. If you use UNION ALL, DB2 will not eliminate duplicates. But if you know there will be no duplicates (based on the logic of the database design and the query), then you can save some time by using UNION ALL.
    M. A. Feldman
    IBM Certified DBA on DB2 for Linux, UNIX, and Windows
    IBM Certified DBA on DB2 for z/OS and OS/390

  4. #4
    Join Date
    Apr 2007
    Posts
    31
    The issue I was having turned out to be a syntax problem, and not with UNION.

    Thank you for your help.

    cdun2

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •