mysql - SQL order by date descending expression -
i have 2 tables, worker , worker_balance:
--------------- ------------------ | worker | | worker_balance | |-------------|<----------|----------------| | | | int workerbid | | ... | | int amount | --------------- | int workerid | | date timestamp | ------------------
and following sql-query:
select worker.id, worker.xy, sum(worker_balance.amount) worker join worker_balance on workerbalance.workerid = worker.workerid group worker.id, worker.xy
i need results ordered latest timestamp, the oldest must first. worker, last transaction oldest, comes first.
you need group by in order aggregate , order by in order sort:
select worker.id ,worker.xy ,max(worker.timestamp) last_transaction ,sum(worker_balance.amount) worker join worker_balance on workerbalance.workerid = worker.workerid group worker.id ,worker.xy order last_transaction
Comments
Post a Comment