If this is your first visit, be sure to check out the FAQ by clicking the link above. You may have to register before you can post: click the register link above to proceed. To start viewing messages, select the forum that you want to visit from the selection below.

 
Go Back  dBforums > Database Server Software > DB2 > Show values that are not in a table

Reply
 
LinkBack Thread Tools Search this Thread Display Modes
  #1 (permalink)  
Old 03-16-10, 17:07
CCMF CCMF is offline
Registered User
 
Join Date: Nov 2004
Posts: 16
Show values that are not in a table

I have a lot of values in an Excel worksheet and want to check them against a table.

Example:
338306
338406
338506


From these 3 values I know that only 338306 exists in the table.

What is the SQL statement to show me that 338406 and 338506 don't exist in the table?

Thanks in advance.
Reply With Quote
  #2 (permalink)  
Old 03-16-10, 17:16
n_i n_i is offline
:-)
 
Join Date: Jun 2003
Location: Toronto, Canada
Posts: 4,448
Code:
select * from excel_worksheet eww where not exists (
  select 1 from database_table dt where dt.value = eww.value
)
Reply With Quote
  #3 (permalink)  
Old 03-18-10, 01:03
Vaibhav Vyas Vaibhav Vyas is offline
Registered User
 
Join Date: Jul 2009
Posts: 17
select 1 from excel_worksheet eww EXCEPT(
select 1 from database_table dt
) with ur
Reply With Quote
  #4 (permalink)  
Old 03-18-10, 07:33
n_i n_i is offline
:-)
 
Join Date: Jun 2003
Location: Toronto, Canada
Posts: 4,448
Quote:
Originally Posted by Vaibhav Vyas View Post
select 1 from excel_worksheet eww EXCEPT(
select 1 from database_table dt
) with ur
I have a feeling that the query above will always return 0 rows.
Reply With Quote
  #5 (permalink)  
Old 03-18-10, 07:37
Vaibhav Vyas Vaibhav Vyas is offline
Registered User
 
Join Date: Jul 2009
Posts: 17
I think you get confused by 1...It should be just replaced by the column name @ both sides...:

Don't you think will that work???
__________________
Vyas| Miracle Happens

Last edited by Vaibhav Vyas; 03-18-10 at 07:42.
Reply With Quote
  #6 (permalink)  
Old 03-19-10, 14:50
CCMF CCMF is offline
Registered User
 
Join Date: Nov 2004
Posts: 16
I will see if I can make it clearer what I mean:

First of all, please forget the Excel worksheet I mentioned in my first posting.
I have a lot of numbers like 338306,338406,338506. I want to know which of these values is not in a table (I know that 338306 is).

Maybe it could be something like:
select ... from .... where column_name ... in (338306,338406,338506)

Thank you.
Reply With Quote
  #7 (permalink)  
Old 03-19-10, 15:29
n_i n_i is offline
:-)
 
Join Date: Jun 2003
Location: Toronto, Canada
Posts: 4,448
Same principle:
Code:
with t(value) as (values 338306,338406,338506)
select value from t where not exists (
  select 1 from database_table dt where dt.value = t.value
)
Reply With Quote
  #8 (permalink)  
Old 03-19-10, 15:40
CCMF CCMF is offline
Registered User
 
Join Date: Nov 2004
Posts: 16
It works.

Thank you, Nick.
Reply With Quote
  #9 (permalink)  
Old 03-19-10, 16:53
tonkuma tonkuma is online now
Registered User
 
Join Date: Feb 2008
Location: Japan
Posts: 2,178
Quote:
What is the SQL statement to show me that 338406 and 338506 don't exist in the table?
It could be written by using NOT EXISTS(like Nick wrote), NOT IN, or EXCEPT(like Vyas wrote).

Code:
SELECT value
  FROM (VALUES 338306 , 338406 , 338506) t(value)
 WHERE NOT EXISTS
       (SELECT *
          FROM database_table dt
         WHERE dt.column_name = t.value)
;
Code:
SELECT value
  FROM (VALUES 338306 , 338406 , 338506) t(value)
 WHERE value NOT IN
       (SELECT column_name
          FROM database_table)
;
Code:
VALUES 338306 , 338406 , 338506
EXCEPT
SELECT column_name
  FROM database_table
;

Last edited by tonkuma; 03-19-10 at 16:58. Reason: Removed quotation marks from VALUES in the examples.
Reply With Quote
  #10 (permalink)  
Old 03-22-10, 13:14
CCMF CCMF is offline
Registered User
 
Join Date: Nov 2004
Posts: 16
After some tests, I decided to use the first code that tonkuma suggested:
Quote:
SELECT value
FROM (VALUES 338306 , 338406 , 338506) t(value)
WHERE NOT EXISTS
(SELECT *
FROM database_table dt
WHERE dt.column_name = t.value)
;
It seems to have the lowest cost.

Thanks to all of you.
Reply With Quote
Reply

Thread Tools Search this Thread
Search this Thread:

Advanced Search
Display Modes

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is Off
HTML code is Off
Trackbacks are On
Pingbacks are On
Refbacks are On