I have the next information in a mysql table:
+-----------------+----------+------------+----------------+---------------+
| time_stamp | m_value | pt_periode | meter_id_meter | mType_id_mtype|
+-----------------+----------+------------+----------------+---------------+
|2019-03-01 00:00:00 | 1800 | 0 | 1 | 22 |
+-----------------+----------+------------+----------------+---------------+
etc
Well, I have three meassures (march, april and may) for the day 1 of every month at 00:00:00. When I do the next query in grafana:
SELECT
time_stamp AS "time",
m_value
FROM menergy
WHERE
meter_id_meter = 1 and mtype_id_type = 22
order by
time_stamp
I get the time graph that looks fine, it represent the three values at the correct time but when I put the cursor on the graph the measure value is 0 and after the correct value, the next point go to 0.
Is it a query mistake?
https://drive.google.com/file/d/1dcLiGdDDpxtp3Jlt5Mpqgis5d3gVZyw_/view?usp=sharing
Related
I am working on a booking system where users can book certain services online. I am stuck on finding and displaying available time slots within a specific day. I know the length of the needed time slot is 1 hour and the business hours.
Is there a way to show time slots that has not yet been booked on a certain day and display only the available time slots that is available to be booked in a dropdown select form?
If a customer selects a specific day and clicks "Select Day" then it needs to query the DB and return the results.
My SQL structure is as follows
|id | title | start_time | end_time | booking_date |
| 1 | Name1 | 2022-05-12 08:00:00 | 2022-05-12 09:00:00 | 2022-05-12 |
| 2 | Name2 | 2022-05-12 10:00:00 | 2022-05-12 11:00:00 | 2022-05-12 |
| 3 | Name3 | 2022-05-12 13:00:00 | 2022-05-12 14:00:00 | 2022-05-12 |
| 4 | Name4 | 2022-05-12 14:00:00 | 2022-05-12 15:00:00 | 2022-05-12 |
as per above the select form should display the timeslots that is not already taken.
09:00 - 10:00
12:00 - 13:00
15:00 - 16:00
It would be something like:
select
id, title
from
<table>
where
start_time between '2022-05-12 00:00:00' and '2022-05-12 11:59:59'
and
booking_date is null
I don't know the name of your table, so you would need to replace <table> with that. I'm also assuming that "booking_date" will have a value to indicate that time slot has been reserved, that it's a date field, and it will be null if that slot hasn't been selected. However, booking_date could have a different purpose.
This is a lazy answer
(because I think just use SQL will do it, use subSelect and other function, but I don't know how to do, sorry.)
get today occupy time:
SELECT id, TIME(start_time) AS s_time FROM tablename
WHERE start_time >= '2022-05-12 00:00:00'
AND start_time < '2022-05-13 00:00:00'
diff time in php:
$sqlResult = []; // sql result
$timeAll = [
'00:00:00',
'01:00:00',
'02:00:00',
'03:00:00',
... // TODO: we need fill it
'23:00:00',
];
foreach ($sqlResult as $item) {
if (isset($timeAll[$item['s_time']])) {
unset($timeAll[$item['s_time']]);
}
}
return $timeAll;
// TODO: javascript or other client code can use it.
ref knowledge link:
MySQL SELECT WHERE datetime matches day (and not necessarily time)
If you choose 2022-05-26, and Peter is occupying room F25 from 2022-05-16 until 2022-05-29, it means the date you select must be out of this range.
So, the query below will only return rooms that were not booked on that day.
SELECT b.id, b.room_id as available_room FROM booking as b
WHERE(
unix_timestamp('$mydate')
NOT BETWEEN unix_timestamp(b.start_date)
AND unix_timestamp(b.end_time)
)
AND unix_timestamp(b.end_time) < unix_timestamp('$mydate');
Assuming $mydate is the variable that contains the date selected by the user, the above query will return rooms that will be available in the future on that particular day.
I am currently trying to set up a query in Grafana to display a graph. I have data in the database and my query returns the results I expect, but the graph seems to only display the smallest result.
Table [test_statements]
| ID | ReportTime | LapTime |
| 1 | 2018-03-29 13:02:06 | 2700 |
| 2 | 2018-03-29 13:03:06 | 2725 |
| 3 | 2018-03-29 13:04:06 | 1645 |
| 4 | 2018-03-29 13:05:06 | 2900 |
| 5 | 2018-03-29 13:06:06 | 3101 |
The template is structured as:
SELECT UNIX_TIMESTAMP(<time_column>) as time_sec,
<value column> as value,
<series name column> as metric
FROM <table name>
WHERE $__timeFilter(time_column)
ORDER BY <time_column> ASC
Current Query
SELECT
UNIX_TIMESTAMP(ReportTime) as time_sec,
LapTime as value,
'ReportTime' as metric
FROM test_statements
WHERE ReportTime > 0
ORDER BY ReportTime ASC
Here are my results:
I can see the ReportTime results are correct, but the graph is wrong.
Not sure what i'm doing wrong but a point in the correct direction would be helpful. Thanks.
Since there are no time scale labels on x-axis in screenshot
most likely you have Format as in graph editor's Metrics tab set to Series or Table instead of Time series.
Thus, what you see as y-axis value is the total (sum) of your individual datapoint values (approx. 7 * 2.7K) which is the default setting for series aggregation function. And x-axis is labeled with time series name(metric).
I have this table (have a look on SQLFiddle)
In previous steps the record number has been determined and the values for "PrevVal" and "NewVal" have been calculated.
The record's end value ("NewVal"), becomes the next record's starting value ("PrevVal")
I would like to condense the table in such a way that there is only one record per day, containing:
the date starting value "StartOfDay",
the total change during the day "TotalChange" and
the resulting day-end value "EndOfDay"
The desired result can be seen in the demo table "ChangesPerDayCondensed"
Who can help me solve this (a stored procedure is OK).
Thnx
I am a little confused whey the record numbers are going the opposite way. But neverthless you could solve this by evaluating the starting value and sum of mutations separatately and then adding them all to come up with ending value..
Ordering the results descending as the record number again needs to be lower for a higher date.
insert into ChangesPerDayCondensed
select #recrd:=#recrd+1, a.MyDate, b.PrevVal, a.Mutation, b.PrevVal+a.Mutation
from
(select MyDate, sum(Mutation) as Mutation from MutationsPerDay group by MyDate) a,
(select b.MyDate, b.PrevVal from (select MyDate, max(RecNo) as RecNo from MutationsPerDay group by MyDate) a, MutationsPerDay b where a.RecNo = b.RecNo) b,
(select #recrd:=0) c
where a.MyDate = b.MyDate order by MyDate desc;
I'd do it this way:
First create a lookup for each day (find first and lasts ReqNo) and then join two times to the Daily table and calculate the changes:
SELECT first_.MyDate,
first_.PrevVal AS StartOfDay,
last_.NewVal AS EndOfDay,
(last_.NewVal - first_.PrevVal) AS TotalChange
FROM
(SELECT mpd1.MyDate,
max(mpd1.RecNo) AS first_rec_no,
min(mpd1.RecNo) AS last_rec_no
FROM MutationsPerDay mpd1
GROUP BY MyDate) AS lo
JOIN MutationsPerDay AS first_ ON lo.first_rec_no = first_.RecNo
JOIN MutationsPerDay AS last_ ON lo.last_rec_no = last_.RecNo
Explanation:
What you actually want is:
For every day the first and the last value (and the difference).
So what you need to find first is for every date the id of the first and the last value:
SELECT mpd1.MyDate,
max(mpd1.RecNo) AS first_rec_no,
min(mpd1.RecNo) AS last_rec_no
FROM MutationsPerDay mpd1
GROUP BY MyDate
----------------------------------------------------
| MyDate | first_rec_no | last_rec_no |
----------------------------------------------------
| 2016-12-05 00:00:00 | 16 | 13 |
| 2016-12-07 00:00:00 | 12 | 12 |
| 2016-12-12 00:00:00 | 11 | 8 |
| 2016-12-14 00:00:00 | 7 | 7 |
| 2016-12-20 00:00:00 | 6 | 6 |
| 2016-12-21 00:00:00 | 5 | 4 |
| 2016-12-28 00:00:00 | 3 | 3 |
| 2016-12-29 00:00:00 | 2 | 2 |
| 2016-12-30 00:00:00 | 1 | 1 |
----------------------------------------------------
Then you can use these first and last id's to find the corresponding values in the source table. For example for the 2016-12-21 you'd get the rows with the id's first: 5 and last: 4
The PrevVal record no 5 represents the first value you have seen at this day and NewVal in record no 4 represents the last value you have seen at this day. If you subtract them you'll get the change for this day.
I hope this clarifies the methodology a bit.
I have read all the arguments: Tell SQL what you want, not how to get it. Use set-based approaches instead of procedural logic. Avoid cursors and loops at all costs.
Unfortunately, I have been racking my brain for weeks and I can't figure out how to come up with a set-based approach to generating an iterative COUNT for sequential subsets of chronologically ordered data.
Here is the specific application of the problem I am working on.
I do football-related research using a database that comprises many years of play-by-play data, which is of course arranged chronologically by year, game, and play. The database is loaded onto a web server running MySQL 5.0.
The fields I need for this particular problem are contained in the core table. Here is some sample data from the relevant part of the table:
GID | PID | OFF | DEF | QTR | MIN | SEC | PTSO | PTSD
--------------------------------------------------------
121 | 2455 | ARI | CHI | 2 | 4 | 30 | 17 | 10
121 | 2456 | ARI | CHI | 2 | 4 | 15 | 17 | 10
121 | 2457 | ARI | CHI | 2 | 3 | 53 | 17 | 10
121 | 2458 | ARI | CHI | 2 | 3 | 31 | 20 | 10
The columns represent, respectively: unique game identifier, unique play identifier, which team is on offense for that play, which team is on defense for that play, the quarter and time the play occurred, and the offense's and defense's scores going into the play. In other words, in (hypothetical) game 121, the Arizona Cardinals scored a field goal on play 2457 (i.e., going into play 2458).
What I want to do is go through several years' worth of data game by game, second by second, and count the number of times any possible score differential occurred at any given elapsed time. The following query arranges the data by seconds elapsed and score differential:
SELECT core.GID, core.PID, core.QTR, core.MIN, core.SEC, core.PTSO, core.PTSD,
((core.QTR - 1) * 900 + (900-(core.MIN * 60 + core.SEC))) AS secEl,
core.PTSO - core.PTSD AS oDif, (core.PTSO - core.PTSD) * -1 AS dDif
FROM core
ORDER BY secEl ASC, oDif ASC;
The result looks something like this:
GID | PID | OFF | DEF | QTR | MIN | SEC | PTSO | PTSD | secEl | oDif | dDif
---------------------------------------------------------------------------------
616 | 100022 | CHI | MIN | 1 | 15 | 00 | 0 | 0 | 0 | 0 | 0
617 | 100169 | HOU | DAL | 1 | 15 | 00 | 0 | 0 | 0 | 0 | 0
618 | 100224 | PHI | SEA | 1 | 15 | 00 | 0 | 0 | 0 | 0 | 0
619 | 100303 | JAX | NYJ | 1 | 15 | 00 | 0 | 0 | 0 | 0 | 0
Although that looks pretty, my goal is not to sort the data chronologically. Rather, I want to step sequentially through every one of the 4,500 possible seconds (four 15-minute quarters plus one 15-minute overtime period) in an NFL game and count the number of times every score differential has ever occurred in each one of those seconds.
In other words, I don't want to count just the number of times a team has been up by, say, 21 points at 1,800 seconds elapsed (i.e., the start of the second quarter) between 2002 and 2013. I want to count the number of times a team has been up by 21 points at any point in a game. On top of that, I want to do this for every score differential that has ever occurred (i.e., -50, -49, -48, ..., 0, 1, 2, ... 48, 49, 50, ...) for every second of every game.
This would be relatively easy to accomplish with a series of nested loops, but it wouldn't be the most reusable of code.
What I want to do is construct set logic that will COUNT the instances of each score differential that has occurred at every second of time elapsed without using loops or cursors. The results would be tabulated as follows:
secondsElapsed | scoreDif | Occurrences
-----------------------------------------
10 | -1 | 12
10 | 0 | 125517
10 | 1 | 0
10 | 2 | 3
Here is a sample query for getting the total number of instances of a specific score differential (+21) at a specific time point (3,000 seconds elapsed):
SELECT ((core.QTR - 1) * 900 + (900-(core.MIN * 60 + core.SEC))) AS timeElapsed,
(core.PTSO - core.PTSD) AS diff, COUNT(core.PTSO - core.PTSD) AS occurrences
FROM core
WHERE ((core.QTR - 1) * 900 + (900-(core.MIN * 60 + core.SEC))) = 3000
AND ABS(core.PTSO - core.PTSD) = 21
That query returns the following results:
timeElapsed | diff | occurrences
----------------------------------
3000 | 21 | 5
Now I want generalize this query to count the instances of every differential at every second elapsed.
Your description is rather confusing but if you want to "COUNT all of the possible score differentials for every possible second without using loops or cursors" then I would do something like:
1) Build a work table (either a temporary table# or a Table data type#) and fill it with the time increments you want e.g.
QTR | MIN | SEC |
1 | 00 | 01
1 | 00 | 02
..
1 | 01 | 59
1 | 02 | 00
1 | 02 | 01
1 | 02 | 02
..
4 | 15 | 59
2) You then use this as the basis of your query. Cross Join a list of the games you are interested in with the work table to give you a table of every game and every minute in that game.
3) With the result of (2) left join your query above back into it?
With this result set you can then look at a whole game and sum\count as neccessary without having to loop.
Not sure if this will cure your problem, but you could try using row_number over a partition...
SELECT ROW_NUMBER() OVER (PARTITION BY <column> ORDER BY <column>) AS aColumn, aColumn FROM aTable
I did it using a sub-query and two variables to define the time point and another to define the point difference.
The query then returns the Diff, then the amount of times the offensive side had it, followed by the defensive side and total times.
SET #Diff INT = 7;
SET #Seconds INT = 1530;
SELECT ABS(core.PTSO - core.PTSD) AS diff, SUM(CASE WHEN core.PTSO - core.PTSD <= 0 THEN 1 ELSE 0 END) OffensiveTimes, SUM(CASE WHEN core.PTSO - core.PTSD >= 0 THEN 1 ELSE 0 END) DefensiveTimes, SUM(1) TotalTimes
FROM (SELECT core.GID, core.PID, core.QTR, core.MIN, core.SEC, core.PTSO, core.PTSD,
((core.QTR - 1) * 900 + (900-(core.MIN * 60 + core.SEC))) AS secEl,
core.PTSO - core.PTSD AS oDif, (core.PTSO - core.PTSD) * -1 AS dDif
FROM core
) core
WHERE secEl = #Seconds AND ABS(core.PTSO - core.PTSD) = #Diff
GROUP BY ABS(core.PTSO - core.PTSD);
This returns this for the small dataset you gave
7 diff, 0 OffensiveTimes, 1 DefensiveTimes, 1 Times
Hope that was what you were looking for :)
I am looking to to use an SSRS line chart to graph ending run times of jobs. I am using runtime on the y-axis and date on the x-axis. The graph looks like a flat line and the times do not show up on the y-axis, but integers show.
I also want to put a target line at a specific time to show our SLA time.
Here is example of data I am trying to graph, but I want a line.
+-------+----------+
| y | x |
+-------+----------+
| 10:05 | 3/1/2009 |
| 11:00 | 3/2/2009 |
| 10:15 | 3/3/2009 |
+-------+----------+
+-------+----------+----------+----------+
| 11:00 | | x | |
| 10:45 | | | |
| 10:30 | | | |
| 10:15 | | | x |
| 10:00 | x | | |
+-------+----------+----------+----------+
| | 3/1/2009 | 3/2/2009 | 3/3/2009 |
+-------+----------+----------+----------+
Lets say you have table Times with Date and Hours.
Declare Report parameter #LimitHourParam decimal - for target time.
Set query of dataset:
SELECT Date, SUM(Hours) AS Hours, 'Actual Hours' AS LimitHour
FROM Times
GROUP BY Date
UNION ALL
SELECT DISTINCT Date, #LimitHourParam AS Hours, 'Hours Limit' AS LimitHour
FROM Times
So you will get in result hours grouped by dates and some hour limit for unique each date.
Place Chart control in report page.
Go to Chart Properties->Data
Press "Add Values"
Erase value of Label and set Value to
=Sum(Fields!Hours.Value)
Go to "Point Labels" tab of "Edit Chart Value" dialog, check "Show point labels", set Data label to
= String.Format("{0}:{1}", (Fields!Hours.Value / 1) - (Fields!Hours.Value Mod 1), (Fields!Hours.Value Mod 1)*60/1 - (Fields!Hours.Value Mod 1)*60 Mod 1)
Add Category Groups, set "Group On" expression to
=Fields!Date.Value
Add Series Group, set both "Group On" expression and Label values to
=Fields!LimitHour.Value
Go to Chart Properties->X Axis, set labels format dd/mm/yyyy
Result should look like this: