View previous topic :: View next topic |
Author |
Message |
maxisnowhere Beginner
Joined: 25 May 2005 Posts: 59 Topics: 20
|
Posted: Wed Feb 14, 2007 3:34 am Post subject: Searching value NOT IN a tabel from a list |
|
|
Hi Guys,
I've a question and hope you can help me.
I have a table TAB1 like this:
Code: |
COL1 COL2
------ -------
1 AAA
2 BBB
3 CCC
4 DDD
|
and a list of possible value for COL1 like
Code: | (1,2,3,4,5,6,7,8,9) |
I would like to find out witch values of the list are NOT in COL1 of TAB1.
ehehe I could do a simply Pli program but I would like to do it with just a query.
Any idea?
Thank for all contribution!
Have a good one _________________ Maxisnowhere |
|
Back to top |
|
|
maxisnowhere Beginner
Joined: 25 May 2005 Posts: 59 Topics: 20
|
Posted: Wed Feb 14, 2007 3:49 am Post subject: |
|
|
Ops, I forgot: im'working with DB2! _________________ Maxisnowhere |
|
Back to top |
|
|
hareshh Beginner
Joined: 13 Dec 2006 Posts: 16 Topics: 0
|
|
Back to top |
|
|
maxisnowhere Beginner
Joined: 25 May 2005 Posts: 59 Topics: 20
|
Posted: Wed Feb 14, 2007 4:54 am Post subject: |
|
|
Hi Haresh,
unfortunately it is just a list. I was trying to make them a table using something like:
Code: |
(select 1 from sysibm.sysdummy1) union
(select 2 from sysibm.sysdummy1) union
(select 3 from sysibm.sysdummy1) union
(select 4 from sysibm.sysdummy1) union
(select 5 from sysibm.sysdummy1) union
(select 6 from sysibm.sysdummy1) union
(select 7 from sysibm.sysdummy1) union
(select 8 from sysibm.sysdummy1) union
(select 7 from sysibm.sysdummy1) |
but I am having some problem by managing that. I'm not sure mine is a good solution...
Thank 4 your help! _________________ Maxisnowhere |
|
Back to top |
|
|
kolusu Site Admin
Joined: 26 Nov 2002 Posts: 12376 Topics: 75 Location: San Jose
|
Posted: Wed Feb 14, 2007 6:43 am Post subject: |
|
|
maxisnowhere,
Try this untested sql
Code: |
SELECT A.MISING_VAL FROM
TABLE (SELECT 1 FROM SYSIBM.SYSDUMMY1
UNION
SELECT 2 FROM SYSIBM.SYSDUMMY1
UNION
SELECT 3 FROM SYSIBM.SYSDUMMY1
UNION
SELECT 4 FROM SYSIBM.SYSDUMMY1
UNION
SELECT 5 FROM SYSIBM.SYSDUMMY1
UNION
SELECT 6 FROM SYSIBM.SYSDUMMY1
UNION
SELECT 7 FROM SYSIBM.SYSDUMMY1
UNION
SELECT 8 FROM SYSIBM.SYSDUMMY1
UNION
SELECT 9 FROM SYSIBM.SYSDUMMY1) AS A(MISING_VAL)
WHERE A.MISING_VAL NOT IN (SELECT DISTINCT COL1 FROM TAB1)
; |
Hope this helps...
Cheers
Kolusu _________________ Kolusu
www.linkedin.com/in/kolusu |
|
Back to top |
|
|
maxisnowhere Beginner
Joined: 25 May 2005 Posts: 59 Topics: 20
|
Posted: Wed Feb 14, 2007 7:54 am Post subject: |
|
|
hi Kolusu,
it fonction!
this is a good solution, now I want to find out how to define the more efficiently, but this is my job!
Thank you very much!!!!
have a good one _________________ Maxisnowhere |
|
Back to top |
|
|
kolusu Site Admin
Joined: 26 Nov 2002 Posts: 12376 Topics: 75 Location: San Jose
|
Posted: Wed Feb 14, 2007 10:02 am Post subject: |
|
|
Quote: |
this is a good solution, now I want to find out how to define the table
more efficiently, but this is my job!
|
maxisnowhere,
Efficiency would depend if COl1 has an index defined, or how big the table is and many other factors to be considered. Why don't you run an explain on the statement and see the cost of the statement.
Here is an alternate way of getting the desired results.
Code: |
SELECT A.COL1
FROM (SELECT COL1 AS COL1
FROM TAB1
UNION ALL
SELECT 1 AS COL1 FROM SYSIBM.SYSDUMMY1
UNION ALL
SELECT 2 AS COL1 FROM SYSIBM.SYSDUMMY1
UNION ALL
SELECT 3 AS COL1 FROM SYSIBM.SYSDUMMY1
UNION ALL
SELECT 4 AS COL1 FROM SYSIBM.SYSDUMMY1
UNION ALL
SELECT 5 AS COL1 FROM SYSIBM.SYSDUMMY1
UNION ALL
SELECT 6 AS COL1 FROM SYSIBM.SYSDUMMY1
UNION ALL
SELECT 7 AS COL1 FROM SYSIBM.SYSDUMMY1
UNION ALL
SELECT 8 AS COL1 FROM SYSIBM.SYSDUMMY1
UNION ALL
SELECT 9 AS COL1 FROM SYSIBM.SYSDUMMY1) A
GROUP BY A.COL1 HAVING COUNT(*) = 1
|
Hope this helps...
Cheers
Kolusu _________________ Kolusu
www.linkedin.com/in/kolusu |
|
Back to top |
|
|
maxisnowhere Beginner
Joined: 25 May 2005 Posts: 59 Topics: 20
|
Posted: Wed Feb 14, 2007 11:04 am Post subject: |
|
|
Thank you! You have really helped me !!!!
The performance is not really the problem, just my list is with 150 number and using the sysibm,.sysdummy1 bla bla is quite long.
See ya!!
Wih u a wonderfull ST Valantine day!!! _________________ Maxisnowhere |
|
Back to top |
|
|
kolusu Site Admin
Joined: 26 Nov 2002 Posts: 12376 Topics: 75 Location: San Jose
|
Posted: Wed Feb 14, 2007 11:31 am Post subject: |
|
|
maxisnowhere,
What is the format of col1 and list all the possible 150 values , may be there is an alternative way to do it
Kolusu _________________ Kolusu
www.linkedin.com/in/kolusu |
|
Back to top |
|
|
maxisnowhere Beginner
Joined: 25 May 2005 Posts: 59 Topics: 20
|
Posted: Wed Feb 14, 2007 11:47 am Post subject: |
|
|
kolusu,
col1 1 is a DECIMAL 11 with such values (here just some):
Code: |
12304174002
12304174002
12304174505
12304344007
12302343406 |
Thanks a lot _________________ Maxisnowhere |
|
Back to top |
|
|
|
|