Search code examples
sqloracle-databasecrystal-reportscross-join

CROSS JOIN filter


I need to 'manufacture' records for a query, but at the same time, restrict the list of values that are returned from a 'lookup' table.

Is there a way to use 'filter' a CROSS JOIN without having to resort to using an in-line view?

This syntax works as expected (I get the desired results):

SELECT  E.ID,  
        M.VALUE,
        MT.ID, MT.NAME

FROM    ENCOUNTER E
CROSS JOIN (
      SELECT  ID, NAME
      FROM    MEASUREMENT_TYPE
      WHERE   ID IN ('6941','6946')
) MT
LEFT OUTER JOIN MEASURE M ON E.ID=M.ENCOUNTER_ID
        AND MT.ID=M.MEASURE_TYPE_ID

Unfortunately, if I use this approach, I need to use a Command object with Crystal Reports rather than its native 'Visual Linking Expert'. Command objects irritate me.

Adding a filter to the WHERE clause results in an equal join, which is undesirable in this situation.


Solution

  • SELECT
      E.ID,  
      M.VALUE,
      MT.ID,
      MT.NAME
    FROM
      ENCOUNTER         AS E
    CROSS JOIN
      MEASUREMENT_TYPE  AS MT
    LEFT OUTER JOIN
      MEASURE           AS M
        ON  E.ID  = M.ENCOUNTER_ID
        AND MT.ID = M.MEASURE_TYPE_ID
    WHERE
      MT.ID IN ('6941','6946')
    

    In this case you can put the filter in the WHERE clause because it's a LEFT JOIN and you're filtering the left side of the query.

    If you were filtering the Measure table, the filter would have had to go in the LEFT JOIN's ON clause.


    An additional alternative is to INNER JOIN instead of CROSS JOIN and use the filter there. This is because the ON clause doesn't actually need to reference both tables...

      ENCOUNTER         AS E
    INNER JOIN
      MEASUREMENT_TYPE  AS MT
        ON MT.ID IN ('6941','6946')