This week's book giveaway is in the Servlets forum.
We're giving away four copies of Murach's Java Servlets and JSP and have Joel Murach on-line!
See this thread for details.
The moose likes JDBC and the fly likes simple SQL question Big Moose Saloon
  Search | Java FAQ | Recent Topics | Flagged Topics | Hot Topics | Zero Replies
Register / Login


Win a copy of Murach's Java Servlets and JSP this week in the Servlets forum!
JavaRanch » Java Forums » Databases » JDBC
Bookmark "simple SQL question" Watch "simple SQL question" New topic
Author

simple SQL question

Jackie Wang
Ranch Hand

Joined: Apr 18, 2002
Posts: 315
I have 2 tables
TABLE A
ID
PRICE
DATE_OF_DATE
countrycode

TABLE B
countrycode
exchange_rate

I used the code:
CREATE TABLE ALL as
select a.ID,
a.price,
b.exchange_rate,
a.a.DATE_OF_DATA
from TABLEA a, TABLEB b
where a.DATE_OF_DATA = b.DATE_OF_DATA
and a.countrycode = b.countrycode
order by a.barra_id, a.DATE_OF_DATA;
RUN;

It's giving me 2 entried for every ID and DATE
i.e.

ID price exchange Date
A 1 2.34 2001-01-01
A 1 2.34 2001-01-01
B 1.1 2.35 2001-01-02
B 1.1 2.35 2001-01-02

I only want:
ID price exchange Date
A 1 2.34 2001-01-01
B 1.1 2.35 2001-01-02

Can anyone tell me why?
Thanks!
Mattias Arthursson
Ranch Hand

Joined: Jul 26, 2004
Posts: 90
Use
select distinct...


Admit nothing. Blame everyone. Be bitter.
Adeel Ansari
Ranch Hand

Joined: Aug 15, 2004
Posts: 2874
first of all you didn't mention these, DATE_OF_DATA, barra_id , column in your table B and A description respectively and you are using it in the query. second thing you should fulfill the equi join condition. equi join should be between primary, foreign relationship.

i think this is not the case of distinct. try equi join correctly.
Julian Kennedy
Ranch Hand

Joined: Aug 02, 2004
Posts: 823
Jackie,

If you're just playing in development (e.g. setting up test data as a one-off) then SELECT DISTINCT will work fine but it's the first reaction of the SQL beginner when a joined query is producing duplicate rows.

As Adeel says, you need to get the join condition right, and that means ensuring that each primary key column in table A is joined to a corresponding foreign key column in table B. It currently looks like you're missing one, but it's not clear which from your example.

Also when posting code please try to copy and paste from the actual code you're testing (if edited a little for brevity). It's not easy to help you when it's clear that your code as given couldn't possibly produce the output you say it does. Also please use the UBB CODE tags (next to the Graemlins) when posting code.

Jules
Jackie Wang
Ranch Hand

Joined: Apr 18, 2002
Posts: 315
thanks for all of your help =)
 
Consider Paul's rocket mass heater.
 
subject: simple SQL question
 
Similar Threads
SQL question
how to convert gregoriancalendar obj to sqldate?
what is the right way to load this object graph. real world problem
How to avoid the additional blank lines ???
How to avoid blank lines