pg.ddx.io  pgsql-sql@postgresql.org mailing list archive  
help / color / mirror / Atom feed
From: Ragnar Hafstað <gnari@simnet.is>
To: aarni@kymi.com
Cc: pgsql-sql@postgresql.org
Subject: Re: Read count ?
Date: Sat, 26 Feb 2005 13:24:19 +0000
Message-ID: <1109424259.10941.6.camel@localhost.localdomain> (raw)
In-Reply-To: <200502241717.14648.aarni@kymi.com>
References: <200502241717.14648.aarni@kymi.com>

On Thu, 2005-02-24 at 17:17 +0200, Aarni Ruuhimäki wrote:
> Hi,
> 
> Could someone please give a hint on how to query the following neatly ?
> 
> Get news from a news table that belong to a particular account, get segment 
> name from segments table for each news item and read count from read history 
> table that gets a news_id and timestamp insert every time the news is read. 
> Display everything by news count, most read news first ?
> 
> news_id 4, news_header, segment_name x, read 10 times
> news_id 2, news_header, segment_name y, read 8 times
> news_id 1, news_header, segment_name x, read 7 times
> news_id 3, news_header, segment_name x, read 0 times
> 
> news_table:
> news_id, account_id, segment_id, news_header, ...
> 
> segments_table:
> segment_id, account_id, segment_name
> 
> read_history_table:
> history_id, news_id, timestamp
> 

how about:

select news_id,news_header,segment_name,count(*) 
from news_table 
     natural join segments_table 
     natural join read_history_table
where account_id=? 
group by news_id,news_header,segment_name;

?

gnari





view thread (3+ messages)  latest in thread

Message-ID: <1109424259.10941.6.camel@localhost.localdomain>
Permalink:  ../1109424259.10941.6.camel@localhost.localdomain/
Also on:    postgresql.org/message-id/1109424259.10941.6.camel@localhost.localdomain

 · 

reply

Reply instructions:

You may reply publicly to this message via plain-text email
using any one of the following methods:

* Reply to all the recipients using the --to and --cc options:
  reply via email

  To: pgsql-sql@postgresql.org
  Cc: gnari@simnet.is, aarni@kymi.com
  Subject: Re: Read count ?
  In-Reply-To: <1109424259.10941.6.camel@localhost.localdomain>

* Save the following mbox file, import it into your mail client,
  and reply-to-all from there: mbox

This inbox is served by DDX for PostgreSQL; see mirroring instructions
for how to clone and mirror all data and code used for this inbox