• Post Reply Bookmark Topic Watch Topic
  • New Topic
programming forums Java Mobile Certification Databases Caching Books Engineering Micro Controllers OS Languages Paradigms IDEs Build Tools Frameworks Application Servers Open Source This Site Careers Other Pie Elite all forums
this forum made possible by our volunteer staff, including ...
Marshals:
  • Campbell Ritchie
  • Jeanne Boyarsky
  • Ron McLeod
  • Paul Clapham
  • Liutauras Vilda
Sheriffs:
  • paul wheaton
  • Rob Spoor
  • Devaka Cooray
Saloon Keepers:
  • Stephan van Hulst
  • Tim Holloway
  • Carey Brown
  • Frits Walraven
  • Tim Moores
Bartenders:
  • Mikalai Zaikin

simple SQL question

 
Ranch Hand
Posts: 315
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • 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!
 
Ranch Hand
Posts: 90
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Use
select distinct...
 
Ranch Hand
Posts: 2874
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • 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.
 
Ranch Hand
Posts: 823
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • 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
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
thanks for all of your help =)
 
With a little knowledge, a cast iron skillet is non-stick and lasts a lifetime.
reply
    Bookmark Topic Watch Topic
  • New Topic