Home Forums SQL Server 2008 SQL Server Newbies Display two fields of two UNRELATED tables WITHOUT crossjoin RE: Display two fields of two UNRELATED tables WITHOUT crossjoin

  • Like Olga's suggestion, this approach is also pretty simple:

    DECLARE @Table1 TABLE (PK INT, Name VARCHAR(6))

    INSERT INTO @Table1 SELECT 1, 'XXXXXX' UNION ALL SELECT 2, 'XXXXXX'

    DECLARE @Table2 TABLE (PK INT, Name VARCHAR(6))

    INSERT INTO @Table2

    SELECT 1, 'XXXXXX' UNION ALL SELECT 2, 'XXXXXX' UNION ALL SELECT 3, 'XXXXXX'

    SELECT [From Table 1]=COUNT(PK1), [From Table 1]=COUNT(PK2)

    FROM (

    SELECT PK, NULL FROM @Table1

    UNION ALL

    SELECT NULL,PK FROM @Table2) a(PK1, PK2)


    My mantra: No loops! No CURSORs! No RBAR! Hoo-uh![/I]

    My thought question: Have you ever been told that your query runs too fast?

    My advice:
    INDEXing a poor-performing query is like putting sugar on cat food. Yeah, it probably tastes better but are you sure you want to eat it?
    The path of least resistance can be a slippery slope. Take care that fixing your fixes of fixes doesn't snowball and end up costing you more than fixing the root cause would have in the first place.

    Need to UNPIVOT? Why not CROSS APPLY VALUES instead?[/url]
    Since random numbers are too important to be left to chance, let's generate some![/url]
    Learn to understand recursive CTEs by example.[/url]
    [url url=http://www.sqlservercentral.com/articles/St