If this is your first visit, be sure to check out the FAQ by clicking the link above. You may have to register before you can post: click the register link above to proceed. To start viewing messages, select the forum that you want to visit from the selection below.

 
Go Back  dBforums > Database Server Software > DB2 > duplicate record

Reply
 
LinkBack Thread Tools Search this Thread Display Modes
  #1 (permalink)  
Old 10-08-03, 09:46
fartman fartman is offline
Registered User
 
Join Date: Oct 2003
Posts: 6
duplicate record

Hi,
I have 2 tables on DB2 version 6

Tab_A
id name
1 shop1
2 shop2

Tab_B
id_tab_a keyword
1 book
1 bookstore
1 handbook
2 restaurant

I need to write a SQL query that find who have 'book' as keyword on Tab_B, but with no duplicated record. If i write:
Select distinct id, name left join Tab_B on id=id_tab_a where
keyword like '%book%'
obtain 3 duplicated records.

Thanks,
Jhon
Reply With Quote
  #2 (permalink)  
Old 10-08-03, 12:44
ARWinner ARWinner is offline
Registered User
 
Join Date: Jan 2003
Posts: 3,575
Wouldn't you just need something like:

select distinct a.id,a.name from tab_b as b, tab_a as a
where (b.id_tab_a = a.id) and (b.keyword like '%book%')

Andy
Reply With Quote
  #3 (permalink)  
Old 10-08-03, 12:49
fartman fartman is offline
Registered User
 
Join Date: Oct 2003
Posts: 6
Really, i have join on other four table by id of table_a, it will work?

Thank You for the answer.
John
Reply With Quote
  #4 (permalink)  
Old 10-08-03, 12:57
ARWinner ARWinner is offline
Registered User
 
Join Date: Jan 2003
Posts: 3,575
John,
can you be more specific. Like maybe show the layouts of the tables and what you want from the query? I can help with specifics, not generalities, since I do not know your setup.

Andy

Quote:
Originally posted by fartman
Really, i have join on other four table by id of table_a, it will work?

Thank You for the answer.
John
Reply With Quote
  #5 (permalink)  
Old 10-08-03, 13:10
achiola achiola is offline
Registered User
 
Join Date: May 2002
Location: General Deheza, Cba, Arg.
Posts: 273
Re: duplicate record

Select distinct
id,
name
left outter join Tab_B
on id=id_tab_a
where keyword like '%book%'
and id is null
Reply With Quote
  #6 (permalink)  
Old 10-08-03, 18:06
aloz aloz is offline
Registered User
 
Join Date: May 2003
Location: San Juan, PR
Posts: 18
Re: duplicate record

You can use the statement
select a.name
from tab1 a
where 1 = ( select count(*)
from tab2 b
where b.id = a.id and b.keyw like '%book%' )

Saludos, Antonio.
Reply With Quote
  #7 (permalink)  
Old 10-10-03, 12:19
fartman fartman is offline
Registered User
 
Join Date: Oct 2003
Posts: 6
Re: duplicate record

Problem solved with:

select id,name from tab_a
left join tab_b on id=id_tab_a where keyword like '%book%'
group by id,name

Thank you to all.
Jhonny

Quote:
Originally posted by aloz
You can use the statement
select a.name
from tab1 a
where 1 = ( select count(*)
from tab2 b
where b.id = a.id and b.keyw like '%book%' )

Saludos, Antonio.
Reply With Quote
Reply

Thread Tools Search this Thread
Search this Thread:

Advanced Search
Display Modes

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is Off
HTML code is Off
Trackbacks are On
Pingbacks are On
Refbacks are On