| | |
Advanced SQL query problem
Please support our MySQL advertiser: PostgreSQL or MySQL? Compare and contrast the two most popular open source databases
![]() |
•
•
Join Date: Dec 2008
Posts: 1
Reputation:
Solved Threads: 0
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:
I get this result:
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
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:
sql Syntax (Toggle Plain Text)
SELECT idpersona, idcategoria, datapagamento, datascadenza FROM personacategoria WHERE NOW( ) BETWEEN datapagamento AND datascadenza ORDER BY idpersona, datascadenza
I get this result:
MySQL Syntax (Toggle Plain Text)
idPersona idcategoria datapagamento datascadenza 2 2 2008-12-07 2010-03-31 4 2 2007-12-17 2009-03-31 4 1 2008-11-08 2010-03-31 5 2 2008-12-08 2010-03-31 6 2 2007-11-11 2009-03-31 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
•
•
Join Date: Dec 2007
Posts: 113
Reputation:
Solved Threads: 17
MySQL Syntax (Toggle Plain Text)
SELECT idpersona, Max(idcategoria) as categoria, Max(datapagamento) as pagamento, Max(datascadenza) as scadenza FROM personacategoria WHERE NOW( ) BETWEEN datapagamento AND datascadenza GROUP BY idpersona ORDER BY idpersona
![]() |
Similar Threads
- Oracle Help Vb.net application (VB.NET)
- Database Design for Dating Site (Database Design)
- Help with automatic update problem and more (Viruses, Spyware and other Nasties)
- Cannot find server or DNS Error - please help! (Viruses, Spyware and other Nasties)
- I NEED HELP PLEASE:Warning: mysql_num_rows(): (PHP)
- I NEED HELP PLEASE:Warning: mysql_num_rows(): (MySQL)
- Need help with the syntax problem (PHP)
- Advanced mySQL Querys (PHP)
Other Threads in the MySQL Forum
- Previous Thread: run an query
- Next Thread: php MyAdmin to server
| Thread Tools | Search this Thread |
agplv3 alfresco amazon api artisticlicense aws bizspark breathalyzer camparingtocolumns changingprices cmg communityjournalism contentmanagement contractors copyright court crm database design developer development distinct dui ec2 email enterprise eudora facebook form foss gartner gnu government gpl greenit groklaw groupware hiring hyperic images innerjoins insert ip joebrockmeier join journalism keyword keywords kickfire laptop law legal license licensing linux maintenance managing mariadb matchingcolumns metron microsoft microsoftexchange mindtouch montywidenius mozilla multiple music mysql mysqlcolumnupdating mysqldatetimeordermax() mysqlindex mysqlinternalqueries mysqlquery mysqlsearch news open-xchange opendatabasealliance opengovernment opensource oracle penelope php priceupdating query referencedesign reorderingcolumns resultset saas select sharepoint simpledb sourcecode spotify sql sugarcrm syntax techsupport thunderbird transparency virtualization





