Advanced SQL query problem

Reply

Join Date: Dec 2008
Posts: 1
Reputation: joliejoker is an unknown quantity at this point 
Solved Threads: 0
joliejoker joliejoker is offline Offline
Newbie Poster

Advanced SQL query problem

 
0
  #1
Dec 18th, 2008
Hello.
I'm burning my head on a problem with a query.
Querying a payments table, I need to find the actual membership category (let's say: IDCATEGORIA). If I run this:

  1. SELECT idpersona, idcategoria, datapagamento, datascadenza
  2. FROM personacategoria
  3. WHERE NOW( )
  4. BETWEEN datapagamento
  5. AND datascadenza
  6. ORDER BY idpersona, datascadenza

I get this result:

  1. idPersona idcategoria datapagamento datascadenza
  2. 2 2 2008-12-07 2010-03-31
  3. 4 2 2007-12-17 2009-03-31
  4. 4 1 2008-11-08 2010-03-31
  5. 5 2 2008-12-08 2010-03-31
  6. 6 2 2007-11-11 2009-03-31
  7. 7 2 2008-12-10 2010-03-31

The problem, as you can notice, is the second and third row: person 4 paid on the 2007-12-17 its membership, and then again one year later on the 2008-11-08. So, if I query now (2008-12-17), I obviously am in the overlapping period, so that I retrieve two records.
What I want to do is to retrieve, in cases like this, only the oldest DATASCADENZA record (obviously, for each single IDPERSONA).
I've tried lots of things today, but nothing seems to work... any hint?

thanks,
jj

n.b.: (just in case you need it): primary key for the table is IDPERSONA+IDCATEGORIA+DATAPAGAMENTO
Reply With Quote Quick reply to this message  
Join Date: Dec 2007
Posts: 113
Reputation: hielo is on a distinguished road 
Solved Threads: 17
hielo hielo is offline Offline
Junior Poster

Re: Advanced SQL query problem

 
0
  #2
Dec 21st, 2008
  1. SELECT idpersona, Max(idcategoria) as categoria, Max(datapagamento) as pagamento, Max(datascadenza) as scadenza
  2. FROM personacategoria
  3. WHERE NOW( )
  4. BETWEEN datapagamento
  5. AND datascadenza
  6. GROUP BY idpersona
  7. ORDER BY idpersona
Reply With Quote Quick reply to this message  
Reply

This thread is more than three months old.
Perhaps start a new thread instead?
Message:


Thread Tools Search this Thread



About Us | Contact Us | Advertise | DaniWeb | Acceptable Use Policy | RSS Feed

©2003 - 2009 DaniWeb® LLC