Joined: 20 Dec 2002 Posts: 80 Topics: 21 Location: Chicago
Posted: Thu May 06, 2004 12:13 pm Post subject:
Coolman,
If you want to select all the statecodes that do not have a corresponding region code in tablex, the following SQL should suffice:
Pls note: You need to provide more details while posting a topic. I have assumed that you are using DB2.
Basically, what the SQL query does is:
Create a superset of a combination of all Statecodes and distinct RegionCodes and search for this combination in a new resultant table formed by joining the two tables. All the records that do not match are the ones you are looking for.
Code:
SELECT substr(P.totalstring,2,1) statecode, substr(P.totalstring,1,1) regioncode
FROM
(SELECT W.regioncode||E.statecode totalstring
FROM
(select regioncode from
tablex ) W ,
(select distinct statecode
from tabley) E
WHERE
W.regioncode||E.statecode NOT IN
(select R.regioncode||R.statecode
from tabley R, tablex T
where R.regioncode = T.regioncode)
) P
;
Joined: 26 Nov 2002 Posts: 12375 Topics: 75 Location: San Jose
Posted: Thu May 06, 2004 12:30 pm Post subject:
Coolman,
A good query to test your sql skills. The following sql will give you desired results.
Code:
SELECT DISTINCT A.STATE_CODE,B.REGION_CODE
FROM TABLE_Y A
,TABLE_X B
WHERE (A.STATE_CODE,B.REGION_CODE) NOT IN (SELECT STATE_CODE
,REGION_CODE
FROM TABLE_Y)
;
You cannot post new topics in this forum You cannot reply to topics in this forum You cannot edit your posts in this forum You cannot delete your posts in this forum You cannot vote in polls in this forum