April 16, 2013 at 12:25 am
How would you write a query that would return only the items in the where clause, which were not found in the table? ie. 'fff','ggg','hhh','iii' are not found in the table. I could build a second table with all the values in the where clause and do an except, but that is too much work...
create table List
(id int,
name varchar(20)
)
insert into List
values
(1,'aaa'),
(2,'bbb'),
(3,'ccc'),
(4,'ddd'),
(5,'eee'),
(6,'jjj'),
(7,'kkk'),
(8,'lll');
select * from List
where name not in ('ooo','bbb','ccc','ddd','eee','fff', 'ggg', 'hhh', 'iii', 'jjj','kkk','lll');
April 16, 2013 at 1:10 am
SELECT d.*
FROM (VALUES ('ooo'),('bbb'),('ccc'),('ddd'),('eee'),('fff'),('ggg'),('hhh'),('iii'),('jjj'),('kkk'),('lll')) d (name)
WHERE NOT EXISTS (SELECT 1 FROM List l WHERE l.name = d.name)
For fast, accurate and documented assistance in answering your questions, please read this article.
Understanding and using APPLY, (I) and (II) Paul White
Hidden RBAR: Triangular Joins / The "Numbers" or "Tally" Table: What it is and how it replaces a loop Jeff Moden
April 16, 2013 at 6:11 am
Thank you.
April 16, 2013 at 7:33 am
Hi Chris,
Thanks for the query. I didn't use the syntax like this.
FROM (VALUES ('ooo'),('bbb'),('ccc'),('ddd'),('eee'),('fff'),('ggg'),('hhh'),('iii'),('jjj'),('kkk'),('lll')) d (name)
In the same scenario, I will use UNION ALL with SELECT or insert the values in another table. Your query will be helpful for me. Thanks again.
April 16, 2013 at 8:15 am
KoldCoffee (4/16/2013)
Thank you.
Any time.
For fast, accurate and documented assistance in answering your questions, please read this article.
Understanding and using APPLY, (I) and (II) Paul White
Hidden RBAR: Triangular Joins / The "Numbers" or "Tally" Table: What it is and how it replaces a loop Jeff Moden
April 16, 2013 at 8:17 am
Pothiraj R (4/16/2013)
Hi Chris,Thanks for the query. I didn't use the syntax like this.
FROM (VALUES ('ooo'),('bbb'),('ccc'),('ddd'),('eee'),('fff'),('ggg'),('hhh'),('iii'),('jjj'),('kkk'),('lll')) d (name)
In the same scenario, I will use UNION ALL with SELECT or insert the values in another table. Your query will be helpful for me. Thanks again.
UNION ALL with SELECT is just another table-valued constructor but handy to know if you're connected to pre-2K8.
You're welcome.
For fast, accurate and documented assistance in answering your questions, please read this article.
Understanding and using APPLY, (I) and (II) Paul White
Hidden RBAR: Triangular Joins / The "Numbers" or "Tally" Table: What it is and how it replaces a loop Jeff Moden
Viewing 6 posts - 1 through 5 (of 5 total)
You must be logged in to reply to this topic. Login to reply