File APIs for Java Developers
Manipulate DOC, XLS, PPT, PDF and many others from your application.
http://aspose.com/file-tools
Win a copy of Clojure in Action this week in the Clojure forum!
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic

simple SQL question

 
Jackie Wang
Ranch Hand
Posts: 315
  • 0
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
Posts: 90
  • 0
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Use
select distinct...
 
Adeel Ansari
Ranch Hand
Posts: 2874
  • 0
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
Posts: 823
  • 0
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
Posts: 315
  • 0
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
thanks for all of your help =)
 
I agree. Here's the link: http://aspose.com/file-tools
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic