Navigate
Home
ArticleWiki
Forum
Journal
Search
Newsletter
Links
Tech News
expertsrt.com
Welcome Guest.
Username:

Password:

Remember me

Please help me construct this query
Welcome, Guest. Please login or register.
January 09, 2009, 12:05:27 AM
11313 Posts in 1251 Topics by 508 Members
Latest Member: pissematbox
Experts Round Table Network  |  Databases  |  MySQL  |  Please help me construct this query « previous next »
Pages: [1]
Author Topic: Please help me construct this query  (Read 418 times)
thepreacher

Offline Offline

Posts: 79


« 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.
Logged
VGR
Mentor

Offline Offline

Posts: 684



WWW
« Reply #1 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...
Logged

techie overlord, answers all kind of questions on http://www.europeanexperts.org
Pages: [1]
« previous next »
    Jump to: