Experts Round Table Network

Databases => MySQL => Topic started by: thepreacher on June 22, 2008, 03:02:02 PM



Title: Please help me construct this query
Post by: thepreacher on June 22, 2008, 03:02:02 PM
Please help me construct this query
Quote
select * from payment_transaction where username = "$username" and substring(transactionId, 4, 8) as startdate is between "$startdate" and "$endfate" order by startdate sum paymenamt as subtotal for each date;

Please do not read the above as a well constructed query its not. Its just what i hope the query to achieve. Basically the query results will be used to generate a report which will list the transactions generated by a staff within a date range. The report will have to have a sub total for each date then a grand total.


Title: Re: Please help me construct this query
Post by: VGR on June 25, 2008, 02:04:01 PM
for the grand total, you'll have to use a second query, or a front-end script, or a reporting tool (report generator : it'll do the second SQL call anyway, but it'll be nicer ;-)

select substring(transactionid,4,8) AS startdate,sum(payment) AS subtotal from payment_transaction where username='$username' and startdate>='$startdate' and startdate<='$enddate' GROUP BY substring(transactionid,4,8) order by startdate ASC;

notes :
- you use string arithmetic on dates
- you should add a column for substring(transactionid,4,8)
- it's ugly because you can't use an alias in the WHERE clause (but you could in an HAVING clause)
- if you get an error for the ORDER BY clause, replace startdate with substring(transactionid,4,8) one more time...