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 > Data Access, Manipulation & Batch Languages > ANSI SQL > How to work out this one-to-many join?

Reply
 
LinkBack Thread Tools Search this Thread Display Modes
  #1 (permalink)  
Old 09-21-05, 14:29
Vernon Vernon is offline
Registered User
 
Join Date: Jul 2003
Posts: 24
Red face How to work out this one-to-many join?

table Article ( id int primary key, ...);
table View ( id int primary key, articleID int references Article(id) on delete cascade, ...);
table Topic ( id int primary key, topic varchar(20) not null, articleID int references Article(id) on delete cascade);

I need to have the most viewed articles and the most viewed article on a given topic. I thought I could have a group by on the articleID on the View table. Can I use "order by count(*)" to finish the query statement?

Can someone point me the right direction to work it out?

Any help will be highly appreciated.

Vernon
Reply With Quote
  #2 (permalink)  
Old 09-22-05, 07:18
r937 r937 is offline
SQL Consultant
 
Join Date: Apr 2002
Location: Toronto, Canada
Posts: 19,524
most viewed articles --
Code:
select Article.id
     , count(View.id) as views
  from Article  
left outer
  join View
    on Article.id = View.articleID  
order
    by views desc
but as far as "most viewed articles on a given topic" is concerned, you need to re-examine the table design, apparently each of your topics can have only one article in it
__________________
r937.com | rudy.ca
please visit Simply SQL and buy my book
Reply With Quote
  #3 (permalink)  
Old 09-22-05, 18:48
Vernon Vernon is offline
Registered User
 
Join Date: Jul 2003
Posts: 24
Thanks very much Rudy. You're always very helpful.

I will study your query. While studying and re-examine my design of those domain object/DB tables this morning, I recognize a few errors. The Topic table is one of them. I should need two tables instead:

table topic (id int primary key, name varchar(9) not null);
table article_topic(article_id int references Article(id) on delete cascade, topic_id int references Topic(id) on delete cascade);

Since I am in Hibernate, I need to redesign those domina objects and tables to take the advantage of HQL.

Thanks again.
Reply With Quote
  #4 (permalink)  
Old 09-22-05, 22:56
r937 r937 is offline
SQL Consultant
 
Join Date: Apr 2002
Location: Toronto, Canada
Posts: 19,524
Quote:
Originally Posted by Vernon
I should need two tables instead:

table topic (id int primary key, name varchar(9) not null);
table article_topic(article_id int references Article(id) on delete cascade, topic_id int references Topic(id) on delete cascade);
yes, that's good
__________________
r937.com | rudy.ca
please visit Simply SQL and buy my book
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