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. Cross Join

Cross Join

Scheduled Pinned Locked Moved Database
databasehelpquestion
2 Posts 2 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.
  • M Offline
    M Offline
    Meysam Mahfouzi
    wrote on last edited by
    #1

    I've got a query like this:

    WITH CTE1 AS
    (
    SELECT * FROM TABLE WHERE A = @A
    ),
    CTE2 AS
    (
    SELECT * FROM TABLE WHERE B = @B
    )
    SELECT * FROM
    CTE1
    CROSS JOIN
    CTE2

    The above query works fine for me and always returns cross join result of the two queries. The problem happens when one of those CTEs doesn't return any row and therefore the CROSS JOIN returns nothing as well. When only one of those CTEs return something, my expectation is to return the result of that CTE cross joined with null values. Do you know how I can achieve this purpose?

    W 1 Reply Last reply
    0
    • M Meysam Mahfouzi

      I've got a query like this:

      WITH CTE1 AS
      (
      SELECT * FROM TABLE WHERE A = @A
      ),
      CTE2 AS
      (
      SELECT * FROM TABLE WHERE B = @B
      )
      SELECT * FROM
      CTE1
      CROSS JOIN
      CTE2

      The above query works fine for me and always returns cross join result of the two queries. The problem happens when one of those CTEs doesn't return any row and therefore the CROSS JOIN returns nothing as well. When only one of those CTEs return something, my expectation is to return the result of that CTE cross joined with null values. Do you know how I can achieve this purpose?

      W Offline
      W Offline
      Wendelius
      wrote on last edited by
      #2

      Since cross join doesn't have any 'OUTER' option, the result is always all combinations from all sources. If any of the sources have 0 rows the size of the result set is 0. I think you could use simple outer joins and unions to get desired rows. Something like:

      SELECT *
      FROM Table A LEFT OUTER JOIN Table B ON 1=1
      WHERE ...
      UNION
      SELECT *
      FROM Table A RIGHT OUTER JOIN Table B ON 1=1
      WHERE ...

      However, I would re-check the design of the data model since normally the need to cross join is very rare and may indicate a design problem.

      The need to optimize rises from a bad design. My articles[^]

      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