Sqlserver
 sql >> Base de Dados >  >> RDS >> Sqlserver

Selecionando registros com base no valor da coluna no SQL Server


Isso funcionará:
CREATE TABLE #temp
    (
        Column1 CHAR(1) ,
        Column2 CHAR(2)
    );


INSERT INTO #temp ( Column1 ,
                    Column2 )
VALUES ( 'A', 'B' ) ,
       ( 'A', 'C' ) ,
       ( 'A', 'D' ) ,
       ( 'R', 'B' ) ,
       ( 'R', 'D' ) ,
       ( 'S', 'E' );

SELECT *
FROM   #temp
WHERE  Column1 IN (   SELECT Column1
                      FROM   #temp
                      WHERE  Column2 = 'D' );


DROP TABLE #temp;

Resultado:
+---------+---------+
| Column1 | Column2 |
+---------+---------+
| A       | B       |
| A       | C       |
| A       | D       |
| R       | B       |
| R       | D       |
+---------+---------+