This question already has answers here:
How to find the maximum count using mysql?
(9 answers)
Closed 4 years ago.
select max(total),blockfloor
from
(select blockfloor,count(roomnumber) as total
from room
where unavailable='0'
group by blockfloor) x;
Above code is mysql query which I used to find the blockfloor, which has maximum number of rooms available.I don't have clear understand about roll of x here.Can anyone explain what is the roll of x?
In your sql query x is alias for the temp table, where will be data from subquery.
for find max total row, you can use query
select blockfloor,count(roomnumber) as total
from room
where unavailable='0'
group by blockfloor
ORDER BY total DESC
LIMIT 1
where you order rows by total, and get first row with max value.
Related
This question already has answers here:
MySQL: Select top n max values?
(3 answers)
Closed 1 year ago.
I have table with billings have some of columns like (order number, date, and total) I want to get the MAX(total) in specific date with whole row and its data.
Here is the table:
Click here to show the picture
I have already wrote some line in MySQL but it came with some mistake
SELECT * FROM biling WHERE total=(SELECT MAX(total) FROM biling WHERE date='2021-10-26')
Let's say the part two of this query equals 50 it came with the maximum billing indeed but what if there is more than one row with the same of MAX(total) in this day I Just want to calculate just one row.
I hope you getting my point guys. And thanks in advance.
Adding LIMIT 1 to the end of your select seems to be the solution.
Updated!
The simple solution that is needed is
SELECT * FROM biling WHERE date='2021-10-26' ORDER BY total DESC LIMIT 1
This question already has answers here:
MySQL: SUM of a column based on a value of another column
(2 answers)
Closed 1 year ago.
Im trying to select from a single table with two options and output to two different variables.
My old query was to calculate all deposits, and then run another one to calculate all withdrawals but this takes long time because there are many rows.
I want to get all amount of Deposits & get all amount of withdrawals in just one query (in php).
Example: SELECT SUM(amount) FROM transactions WHERE cId=10 ...help here...
output will be:
Variable 1: 5500 (deposits)
Variable 2: 2500 (withdrawals)
I solved it like this:
SELECT (SELECT SUM(amount) FROM transactions WHERE cId=1 AND tStatus!='Withdrawal') AS deposits,
(SELECT SUM(amount) FROM transactions WHERE cId=1 AND tStatus='Withdrawal') AS withdrawals
Final result:
This question already has answers here:
Calculating a Moving Average MySQL?
(5 answers)
How do I calculate a moving average using MySQL?
(7 answers)
Closed 5 years ago.
Somehow this is not the initial request I have put forward. Indeed there are similar requests however as per my previous and initial message I tried the examples already delivered and some how I am failing to understand the concept.
What I want is a 3-month rolling average on the price.
The table title tbl consisting of the following productID, priceMonth, Price
The SQL statement I am using is the following:
SELECT AVG(Price) over(partition by productID between 2 preceding and current row)
as rolling_avg
FROM tbl
GROUP by priceMonth
order by price
I am not sure if the syntax is correct as it is giving me the following error:
An alias was previously found. (near "rolling_avg").
Therefore I cordially ask you the following:
1) I cannot locate where the alias problem is, can I cordially ask your assistance?
2) Is the above sql statements addresses what I am looking for? i.e a 3month rolling average?
This question already has answers here:
Count columns according to dates in SQL
(3 answers)
Closed 8 years ago.
I have the following information in my mysql table
Eventually I would like to make a select that would count the entries for a page_id but just for the specific date , meaning I would like to have the following output:
84 - 7 - 09/23/2013
85 - 4 - 09/23/2013
84 - 1 - 09/24/2013
Can it be done in a single select ?
select page_id, count(*), date
from table_name
group by page_id, date
SELECT page_id, count(page_id), date
FROM table
GROUP BY page_id, date
You need to SELECT the 3 fields. Then you need to COUNT one of those (page_id) since you need to count how many repetitions you got. And the last step, for the query to run (and also to make sense) is to GROUP BY the other 2 fields.
Hope you get a clearer idea on how to query the table.
This question already has answers here:
Mysql to select month-wise record even if data not exist
(2 answers)
Closed 9 years ago.
I have a table with a column date and a column ID (to keep it simple). I am doing a query to count the total and group by week
SELECT MONTH(table1.Date_1st), YEAR(table1.Date_1st), COUNT(table1.Id)
FROM table1 as table1
WHERE Date_1st BETWEEN '2013-04-01' AND '2013-07-25'
GROUP BY WEEK(Date_1st)
the problem is there is no results on specific week the result is not taken into consideration. I already try ifnull(table1.Id,0)
Try ISNULL or other NULL SQL functions:
NULL functions
ISNULL returns a 0 if the value is null.