Alright so here it is. I need to figure out the average amount of days between two columns.
Column 1 is recieved_date and column 2 is fix_date
Just want to know how to take the two dates find the difference in days, do that for every row and pop out a number stating the average amount of days it takes to fix something.
Tried to find it online but every time I find something like it, they have two specific dates. I need the entire columns averaged.
You can use the TIMESTAMPDIFF function both for dates and datetime.
See Mysql average time between visits
Add a group by and some other columns to this and it should do the trick:
select
avg(fix_period)
from
(
select
datediff(fix_date, received_date) as fix_period
from some_table
) as a
;
Related
I'm working whit a MariaDB database.
I need to know, for every day in a certain time the avg of a count.
I'd tried somethink like this.
SELECT AVG(dayShipments), weekdays
FROM (SELECT COUNT(idShipment) as "dayShipments", WEEKDAY(dateShipments) as "weekdays"
FROM weekdays
WHERE dateShipments BETWEEN '2021-05-01'AND '2021-05-21'
GROUP BY dateShipments) as t1
GROUP BY weekdays
My boss told me that this query ignore the day where I don't have any Shipment.
How can i inlude that?
Sorry for my bad English and thanks for helping me
If you want to summary by day-of-the-week (which is what your query appears to be doing. And you want to treat days with no shipments as 0, then use SUM() and division:
SELECT WEEKDAY(dateShipments) as weekday,
COUNT(*) / 3 as dayShipments as avg_per_day
FROM weekdays
WHERE dateShipments BETWEEN '2021-05-01'AND '2021-05-21'
GROUP BY weekday;
The 3 is because the query spans three weeks.
First of all don't worry about your english. It's good enough.
Secondly, your boss is right. If you have no records in "weekdays" table for specific day, the mentioned day will never show up with this query.
For solving problem I think you need to have a temporary table for day of week and left join with your t1 table.
Im trying to average the past 5 rows in my table i created in SSRS grouped by date(Monday of every week). Ive tried runningValue however it looks back at all the past rows for each group. Is there a way to limit the scope to just the past 5 rows or weeks for each Date group.
Thanks
I would accomplish this with grouping. I don't know what your dataset is like but I assume it is a SQL query you can modify. The easiest solution would be to add a week number column to your query. For example:
SELECT datepart(week, YOURDATE) as WeekNumber
More info on datepart:
https://learn.microsoft.com/en-us/sql/t-sql/functions/datepart-transact-sql?view=sql-server-2017
Once you have a week number, use the table creation wizard in Report Builder and add WeekNumber as a row group. This will group your values by week number and give you a total under each week. You can change the total by double clicking and making it AVG() instead of SUM().
Note: If you already have each 5 day period in a group, you should be able to right click that and add total. At which point you can just change the SUM to AVG there.
Hi I would like to find a query for the below, I am trying to calculate data between two columns however based on another column which needs to be a selected group of the same values
Unfiltered
Start Time________Disconnect Time______Signalling IP
12:59:00.3________13:26:03.3___________1.1.1.1
10:59:00.3________11:03:03.3___________2.2.2.2
19:59:00.3________20:02:03.3___________1.1.1.1
Filtered
Start Time________Disconnect Time______Signalling IP
12:59:00.3________13:26:03.3___________1.1.1.1
19:59:00.3________20:02:03.3___________1.1.1.1
If you see the table above, I want the selected IP only which is 1.1.1.1, and then from there, calculate the total duration of time from the Start Time and Disconnect Time for that Egress IP.
So column 3 has multiple values, however I need to select the same value, then from there calculate the sum of column 1 and 2 based on column 3.
Please let me know if you have anything in mind, as I have tried multiple queries but can't get the correct one
to calculate difference between to times.
you can use time_to_sec to convert each time value to seconds
and subtract start time from end time to get time period in seconds.
you cat turn it back to time format with SEC_TO_TIME
example
select
column3,
SEC_TO_TIME(sum(TIME_TO_SEC(column2) - TIME_TO_SEC(column1))
from
table
group by column3
I've got the following query on a timestamp(specified by INT(10), which kind of does what I want, but not exactly:
SELECT count(entry_date) as theCount, FROM_UNIXTIME(entry_date, '%Y-%m-%d') AS dd
FROM exp_weblog_titles
WHERE entry_date < UNIX_TIMESTAMP(NOW())
GROUP BY dd
ORDER BY dd DESC
LIMIT 7
That spits out the last 7 dates and the # of entries on those dates that are before today's date. The problem with the query I've created is, it only spits out the date if it actually has an entry on it. When in actuality, I still want that date included even if there are zero entries. Is that possible?
You will need to have a list of dates from somewhere. A lot of people create a calendar table just for this purpose with every date (or every value) that they need so that this calendar table and your data table can be joined to provide this kind of query.
You will find in the long run this is far simpler than any other solution, just takes a moment to set up.
See this article for an example of how to generate such a table.
I have several rows in a table, each containing a start date and an end date. The user has a checkbox for each month of the year. I need to determine which rows contain a date range that includes any of the user's chosen months.
It's easy to check the start & end months by, for example, MONTH(start_date) IN ($month_list), but this approach won't match any months between the two dates.
So I suppose what I'm asking is: is there a way of obtaining the inclusive months from a date range purely in SQL?
I assume you would want to include data rows where the date range spans or intersects with the selected periods - in which case, I'd shove the user selected periods into a table and do a fuzzy join, something like.....
SELECT DISTINCT at.*
FROM a_table at, user_periods up
WHERE at.start_date<=up.end_date
AND at.end_date>=up.start_date
AND up.trans_id=$SOME_VAR
(the trans_id just allows the table to be used for multiple operations)
To minimise the effort here, the user_periods table should have an index on start_date and end_date, and similar for a_table.
Can something like this help?
WHERE
MONTH(start_date) < MONTH_YOU_ARE_CHECKING and
MONTH() > MONTH_YOU_ARE_CHECKING
If you need to check all at once you can do a list of all the months and after delete from the list the month that the user choose, and after compare against the list. It will be better with a pseudocode example :)
MONTHS = 1,2,3,4,5,6,7,8,9,10,11,12
USER_SELECTED_MONTHS= 1,6,8,9,12
LIST_TO CHECK = 2,3,4,5,7,10,11
so, now you can do:
MONTH(start_date) NOT IN (2,3,4,5,7,10,11)
What do you think, could it help you?
regards