July 11, 2018 at 11:20 am
I have table contains two columns having below input records.
Sample Input Records
Column1 Column2
A B
A C
A D
R B
R D
S E
In the above records if i give where condition Column2='D' it will display below output records.
Output Records
Column1 Column2
A B
A C
A D
R B
R D
Logic:In the Sample input records Column2='D' contains two records.These two records contain the colum1 values as 'A' and 'R'.so i want to display the records which contains 'A' and 'R' from the input records
Suppose if i give where condition Column2='C' i it will display below output records.
Output Records
Column1 Column2
A B
A C
A D
Logic:In the Sample input records Column2='C' contains one record.These one record contain the colum1 value as 'A'.so i want to display the records which contains 'A' from the input records
Please help.
July 11, 2018 at 11:37 am
jkramprakash - Wednesday, July 11, 2018 11:20 AMI have table contains two columns having below input records.Sample Input Records
Column1 Column2
A B
A C
A D
R B
R D
S EIn the above records if i give where condition Column2='D' it will display below output records.
Output Records
Column1 Column2
A B
A C
A D
R B
R D
Logic:In the Sample input records Column2='D' contains two records.These two records contain the colum1 values as 'A' and 'R'.so i want to display the records which contains 'A' and 'R' from the input recordsSuppose if i give where condition Column2='C' i it will display below output records.
Output Records
Column1 Column2
A B
A C
A DLogic:In the Sample input records Column2='C' contains one record.These one record contain the colum1 value as 'A'.so i want to display the records which contains 'A' from the input records
Please help.
It helps if you provide the DDL scripts. Here is a way to do this
create table t(x varchar(1),y varchar(1));
insert into t values('A','B');
insert into t values('A','C');
insert into t values('A','D');
insert into t values('R','B');
insert into t values('R','D');
insert into t values('S','E');
select *
from t
where x in(select x from t where y in('D'))
July 11, 2018 at 11:40 am
Try this, and change the @Variable value as you see fit...CREATE TABLE #TEST (
Column1 char(1),
Column2 char(2)
);
INSERT INTO #TEST (Column1, Column2)
VALUES ('A', 'B'),
('A', 'C'),
('A', 'D'),
('R', 'B'),
('R', 'D'),
('S', 'E');
DECLARE @Variable AS char(1) = 'C';
SELECT *
FROM #TEST AS T
WHERE T.Column1 IN (
SELECT DISTINCT T2.Column1
FROM #TEST AS T2
WHERE T2.Column2 = @Variable
);
DROP TABLE #TEST;
Steve (aka sgmunson)
Rent Servers for Income (picks and shovels strategy)
July 13, 2018 at 9:19 am
Thank You Very Much.
July 16, 2018 at 6:40 am
jkramprakash - Friday, July 13, 2018 9:19 AMThank You Very Much.
You are very welcome!
Steve (aka sgmunson)
Rent Servers for Income (picks and shovels strategy)
Viewing 5 posts - 1 through 4 (of 4 total)
You must be logged in to reply to this topic. Login to reply
This website stores cookies on your computer.
These cookies are used to improve your website experience and provide more personalized services to you, both on this website and through other media.
To find out more about the cookies we use, see our Privacy Policy