I have a table revenue with following fields
+----+-----------+-------------+---------------+
| id | member_id | amount_paid | datetime_paid |
+----+-----------+-------------+---------------+
I want to run a query to find the top 10 maximum revenue days. What would be the fastest way to achieve this? The table has a lot of data.
Regards,
Try the basic query which does it:
SELECT DATE(`datetime_paid`) AS `Max Revenue`
FROM `revenue`
ORDER BY `amount_paid` DESC
GROUP BY DATE(`datetime_paid`)
LIMIT 10;
This shows you the Top 10 Max paid days. :)
Assuming your datetime_paid column is of some date or time type (see MySQL docu), the following should work.
SELECT SUM( `amount_paid` ) AS `amount`, DATE( `datetime_paid` ) as `date`
FROM yourTable
GROUP BY `date`
ORDER BY `amount` DESC
LIMIT 10
First you need to group the results by the day the payment was made. This can be done by selecting DATE(datetime_paid) as payday, then grouping by payday. To get the total, select SUM(amount_paid) as total. Finally, to get the top 10, order by total desc and limit 10.
Your final query should look like:
SELECT DATE(`datetime_paid`) AS `payday`, SUM(`amount_paid`) AS `total`
FROM `table_name_here`
GROUP BY `payday`
ORDER BY `total` DESC
LIMIT 10
You may need to change that ORDER BY line to ORDER BY SUM(amount_paid) DESC, I can't remember if field aliases are allowed in ordering.
Related
I have a problem on a sql query. I need to take the last 30 days and then remove only the 4 results with the "voting" value greater
db structure
Id | time | voting
1 | unix time | 3
2 | unix time | 2
3 | unix time | 4
4 | unix time | 1
5 | unix time | 6
I would like to take me only the data of: 5-3-1-2
I have tried with
select a.*
from table a
inner join
( select votingng, max(time) as latest from table group by voting) v
on a.time = v.latest
and a.voting = v.voting
order by time desc limit
It sounds like you are trying to get the top 4 voting results in the past 30 days. Does either of these give you what you want?
SELECT a.*
FROM table a
WHERE a.time > UNIX_TIMESTAMP(DATE_SUB(CURDATE(), INTERVAL 30 DAY))
ORDER BY a.voting DESC
LIMIT 4;
or
SELECT a.*
FROM table a
WHERE DATEDIFF( NOW(), FROM_UNIXTIME(a.time) ) <= 30
ORDER BY a.voting DESC
LIMIT 4;
I think this is what you want:
select v.*
from voting v
where timestamp >= unix_timestamp(date_sub(curdate(), interval 1 month)
order by voting desc
limit 4;
Hope this is what you are looking for:
Select *
From
Voting
Where
time Between CURDATE() And DATE_SUB(CURDATE(),INTERVAL 30 DAY)
Order By voting Desc
Limit 4
Try this if you are using teradata
SELECT * from table
qualify row_number () over(order by time desc)=1 ;
or
select * from
(select table.*, row_number () over(order by time desc) as RANK from table)
where RANK=1
name amount date
----------------------------------------------------
sarava 10000 2015-12-01
muthu 5000 2016-01-08
sarava 7000 2016-01-06
muthu 10000 2016-01-16
expected output height order taken on this month:
name->muthu
amount->15000
Try as below :
SELECT name, SUM( amount ),date_format( `date` , '%M' ) as month
FROM tablename
where month='January'
GROUP BY date_format( `created_at` , '%M' )
It sounds like you want to sum the amount by name. Maybe you want something like this:
SELECT SUM(amount) AS amount, name from table1 GROUP BY name;
So I have table 'item' in database with attributes: id, dateItem, price.
I want to find MAX(SUM(price)) for some month in a current year.
id dateItem (Format: yyyy-mm-dd) price
1 25.06.2015. 986,69
2 21.06.2015. 1564
3 22.03.2015. 23,56
4 21.03.2015. 187,23
5 01.03.2015. 489,33
6 06.10.2015. 975,26
I came up with something like this, but I know it's not ok. Please, help :s
$sql = "SELECT MAX(SUM(price)) FROM item WHERE DATE('Y') = 2015 AND
dateItem between DATE('Y/m/1') and DATE('Y/m/31')";
You can't nest aggregation functions. You need to use a subquery.
SELECT MAX(pricesum)
FROM (SELECT SUM(price) AS pricesum
FROM item
WHERE YEAR(dateItem) = YEAR(NOW())
GROUP BY MONTH(dateItem)) AS subquery
You can do this with ORDER BY and LIMIT:
SELECT SUM(price) AS pricesum
FROM item
WHERE YEAR(dateItem) = YEAR(NOW())
GROUP BY MONTH(dateItem)
ORDER BY pricesum DESC
LIMIT 1;
If you want to know the month as well, you can include that in the SELECT clause.
This is kind of straight forward.
I want to ORDER BY date DESC Limit 4 and then I want to ORDER BY date ASC on that result, so just 4-games from the middle of the big table with ASC date order, any ideas?
Just ORDER BY date ASC Limit 4 Does not work!
What I have:
What I get:
What I want:
you can use subquery :
SELECT a.* FROM (SELECT * FROM yourtable ORDER BY date DESC Limit 4) a ORDER BY a.Date
if you want to get the last four rows from table
select * from table order by date desc limit 0,4
if you want to get the first four rows from table
select * from table order by date asc limit 0,4
I have table called room_prices that store different prices according to the date specified. Here's the sample data.
id room_id date_from date_to price
1 1 3500.00
25 1 7/28/2012 7/29/2012 3600.00
27 1 8/24/2012 8/27/2012 3000.00
49 1 11/12/2012 11/16/2012 4000.00
The first row is the default price of room 1 (room_id = 1).
When guest reserve a room, the system will first look at the prices on room_prices table. If the date reserved by guest was found on the table it will get the price according to the date selected and not the default price.
Ex.
If the guest reserved a room from 11/10/2012 to 11/11/2012 then the price should be 3,500.
If the guest reserved a room from 11/12/2012 to 11/14/2012 then the price should be 4,000.
If the guest reserved a room from 11/10/2012 to 11/13/2012 then the average price should be 3750. See table below:
11/10/2012 3500
11/11/2012 3500
11/12/2012 4000
11/13/2012 4000
3750
So my question is if there is a single SQL code to get the average price.
update:
I tried this sql to no avail:
SELECT avg(price) as avg_price
FROM lf_rooms_prices
WHERE room_id = 1
AND ((DATE_FORMAT(date_from, '%m/%d/%Y') >= '11/10/2012'
AND DATE_FORMAT(date_from, '%m/%d/%Y') < '11/10/2012')
OR (DATE_FORMAT(date_to, '%m/%d/%Y') > '11/13/2012'
AND DATE_FORMAT(date_to, '%m/%d/%Y') <= '11/13/2012'))
btw, I'm using codeigniter.
There's AVG() function, so to get average price your query should look like this:
SELECT AVG(`price`) FROM `room_prices` GROUP BY `room_id`
See docs here. And of course you can add WHERE to the query to narrow data set you want the average value of:
SELECT AVG(`price`) FROM `room_prices` WHERE <CONDITIONS> GROUP BY `room_id`
EDIT:
Full query:
SELECT AVG(`price`) FROM `room_prices`
WHERE `room_id` = 1
AND ((DATE_FORMAT(date_from, '%m/%d/%Y') >= '11/10/2012'
AND DATE_FORMAT(date_from, '%m/%d/%Y') < '11/10/2012')
OR (DATE_FORMAT(date_to, '%m/%d/%Y') > '11/13/2012'
AND DATE_FORMAT(date_to, '%m/%d/%Y') <= '11/13/2012'))
GROUP BY `room_id`
The key is to use GROUP BY.