select from table by option into two different variables (php & mysql) [duplicate] - mysql

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:

Related

How can I get the MAX(total) with the whole row and its data? MySQL [duplicate]

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

MYSQL Query Language [duplicate]

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.

3 month rolling/moving average [duplicate]

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?

SQL query to find the most common index of a minimal field [duplicate]

This question already has answers here:
ROW_NUMBER() in MySQL
(26 answers)
Closed 8 years ago.
I have a table that tracks the activity in several websites. Each row is of the following form: (Date, Hour, Website, Hits)
The Hour field is a number between 0 and 23 and represents an entire hour (for example, 22 is for any hits between 22:00 and 22:59).
I want to find the overall slowest hour for each website, meaning the input should be something like (Website, Hour).
In order to do that, I was thinking I should have a nested query to find the minimum hits for each website on each day, and then count the values of Hour (again, for each website on each day), and see which value is the maximal.
I'm still new to SQL so I'm having difficulties using the min() function properly, to find the minimal value only for a specific date and website. Then I have the same problem with using count() for a specific website.
I'm also curious if I can get not just the most common slowest hour, but maybe the 3 slowest, but at least to me it seems like it's really complicating the problem.
For the first nested query, I considered something like this:
SELECT DISTINCT Date Date_t, Website Website_t, Hour,
(SELECT min(Hits) from HITS_TABLE WHERE Date=Date_t and Website=Website_t) as MinHits
FROM HITS_TABLE
But not only it takes an abnormally long time to calculate, it also gives me multiple entries of (Date_t, Website_t, Hour, min(Hits)) for each value of Hour, so I take it that I'm not doing it in the smartest, nor the most efficient way.
Thanks in advance for any help!
You can get the minimum hour using a trick in MySQL:
select website, substring_index(group_concat(hour order by hits), ',', 1) as minhour
from table t
group by website;
For each website, this constructs a comma-delimited list of hours, ordered by the number of hits. The function substring_index() returns the first row.
This is something of a hack. In most other databases, you would use window/analytic functions, but these are not available in MySQL.
EDIT:
You can do this in standard SQL as well:
select t.*
from table t
where not exists (select 1
from table t2
where t2.hour = t.hour and
t2.hits < t.hits
);
This is interpreted as: "Get me all rows from the table where there are no other rows with the same hour and a lower number of hits." This is a round-about way of saying: "Get me the hour with the minimum value." Note that this will return multiple rows when there are ties.

pulling the following information with a single select [duplicate]

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.