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?
Related
This question already has answers here:
How can I make the decimal places of AVG function in sql limit to 2 only?
(3 answers)
Closed 6 days ago.
SELECT AVG(
TIMESTAMPDIFF(MINUTE,
customer_service_ticket_raise_time,
CURRENT_TIMESTAMP()
)
) AS avg_waiting_time
FROM customer_service_ticket;
I am writing this SQL query to display the average waiting time customers have been waiting but it outputs as a decimal value e.g. 29.8333. Is there any way I want get rid of the decimals and display the minutes as a whole value.
you can wrap your value with the CEILING function
https://www.w3schools.com/sql/func_sqlserver_ceiling.asp
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:
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.
This question already has an answer here:
MySQL - Thousands separator
(1 answer)
Closed 9 months ago.
I have a query:
select sum(invoiceamount) as invoice
from fact_salescount
where year in ({YEAR})
and month >= ({FROMMONTH})
and month <= ({TOMONTH})
This query can return a value from 100.00 to 15034115.93. It will return ONE value.
I would like to add, for each 000, like this: 15,034,115.93
I've seen a lot of similar questions, but none match mine. I hope someone can help me out.
I am using Pentaho and MySQL, and creating these queries within the Design Studio.
SELECT FORMAT(sum(invoiceamount),2)
FROM fact_salescount
WHERE year IN ({YEAR})
AND month >= ({FROMMONTH})
AND month <= ({TOMONTH})
This should do what you want, but I still don't like formatting number in the backend.