Domanda

I have tables A, B and C. Now table A as column A1(Primary Key), table B has column B1(Primary Key) and table C has columns A1(Foreign Key to Table A.A1), B1(Foreign Key to Table B.B1).

Now I'm writing a query which list all rows from A and B and a bit column which will be set to to 1 if a row is found matching in table C otherwise 0.

SELECT 
     ISNULL((SELECT CAST(1 AS BIT) 
             FROM C 
             WHERE C.A1 = A.A1 AND C.B1 = B.B1),0) AS [TAG],
     A.A1,
     B.B1
FROM A CROSS JOIN B

This query is producing an Subquery returned more than 1 value. error even though the query has no duplicate rows after combining the A1 and B1 columns.

È stato utile?

Soluzione

try this,

SELECT  CAST(COALESCE(c.b1, 0) AS BIT)   TAG
FROM
        (
            SELECT  A1, B1
            FROM    A CROSS JOIN B
        ) D LEFT JOIN C
                ON  c.A1 = d.A1 AND
                    c.B1 = D.B1
Autorizzato sotto: CC-BY-SA insieme a attribuzione
Non affiliato a StackOverflow
scroll top