In Microsoft SRS I want to create custom groups for Rows. What I want to do is to take the values by time but display only three bands, Morning afternoon and night. So by using the time I want to group the rows.
Thanks
Try adding an extra column to the SQL. Use a CASE statement so that times falling in the range of 00:01 to 11:59 are given the literal value of "Morning". Values 12:00 to 17:00 "Afternoon" and 17:01 - 23:59 as "Evening". Then use this column to group the data.
Related
I am generating a report which displays how long the plant was running for the day.
Since the operator shuts down the system at lunchtime, I have 2 records for the plant operation hours for each day. I want to display only one record that contains the production Start Time (6:00 am) and production End Time (around 4:00 pm). I have got a table Runtime_Combined that has an auto-incrementing index.
I want to select start date (e.g. 9/1/2021 6:04 AM, which has runtime_combined_ndx = 1) and end date (e.g. 9/1/21 4:23 PM, which has runtime_combined_ndx = 2).
SELECT ProductionStartDate, ProductionEndDate
FROM Runtime_Combined
WHERE month(ProductionStartDate) = month (ProductionStartDate)
And day(ProductionStartDate) = day( ProductionStartDate)
You can use aggregation:
SELECT date(ProductionStartDate), sum(runtime_combined_ndx)
FROM Runtime_Combined
GROUP BY date(ProductionStartDate);
Use grouping. Something like:
SELECT MIN(ProductionStartDate) AS Start, MAX(ProductionEndDate) As End
FROM Runtime_Combined
WHERE <....>
GROUP BY DATE(ProductionStartDate)
In order to update records with a range of values I am aware of using the BETWEEN operator e.g.
UPDATE datetable
SET tableflag = 1
WHERE date BETWEEN '2017-02-01' AND '2017-03-01'
will set the field 'tableflag' to 1 for every record where the date lies between the 1st of February 2017 and the 1st of March 2017. This is a simple way for handling a single or multiple fixed ranges (by using several or conditions betwee BETWEEN operators).
For a dynamic range e.g. set 'tableflag' to 1 for every record where the date is between the first and last Sunday in February for the year 2017, one can use a subquery to generate the dates for the first and last Sundays in February.
However if the condition in the above problem is generalized and one needs to set 'tableflag' to 1 for every record where the date is between the first and last Sunday in February, regardless of year, the problem becomes quite a bit more complicated. The between operator appears to require a single row and passing it two equally dimensioned subqueries throws an error (1242: Subquery returns more than 1 row).
Any idea how I can solve the generalized problem?
You are describing complex patterns where the dates aren't consecutive dates within a range, but a set of individual dates.
If you need a pattern of specific dates, I would use the IN() predicate instead of BETWEEN.
It would be awkward to generate a complex pattern of dates in SQL, so use a client application with access to a robust date library. Generate a list of desired dates with more convenient methods, then use that list in an IN() predicate in SQL.
I've a table with lots of entries consisting of dates and a number.
For instance:
07.02.2016 - 12
06.02.2016 - 48
05.02.2015 - 24
...and so on.
Now I need to sum all of the values older than 2 months. For instance the 3rd entry (05.02.2015) will be added to the second (06.02.2016) and the second one should get the value 72 and the 3rd one should be deleted.
I'd like to know if there is some way to do this in mysql only?
Instead of writing the code for you, I'd like to merely give you some hints:
Identify which rows are older than 2 month and sum them up.
select sum(number) from table where date > curdate() + interval 2 months
or sth. similar will do.
Select the max. date of the entries that are smaller or equal to "now+2months".
Update that row with the value from step 1.
Delete the rows from step 1.
See here for details on date functions in MySQL.
This can be done in 2 statements (one for steps 1-3, one for the deletion).
I have a little problem with a specific functionality. I have a table of reservations for rooms.
Table: Reservations
room (BIGINT), date (DATE)
4, 2012-09-25
4, 2012-09-27
4, 2012-09-30
I need to obtain the dates when room with id = 4 is available between a date range. For example, if i want the dates that the room is available between 2012-09-25 and 2012-10-01 (inclusive) i have to obtain the next result:
date (DATE)
2012-09-26
2012-09-28
2012-09-29
2012-10-01
Any help will be appreciated. Thanks in advance.
This sort of query is easily handled if you add a table of dates to your database. Fill it with a large number of dates (and add to it when you need to), and just do a right outer join to it to find the missing dates.
If you can't or don't want to add an additional table, you can convert the dates to integer field (representing the number of days since Jan 1, 1970) with this:
SELECT room, UNIX_TIMESTAMP(`date`)/86400 FROM reservations
From there, you can use the techniques in this question or elsewhere to look for gaps in that sequence.
As #steven-cheng said, i had to use the combination of mysql and code because creating a temporary table is too messy. Using the code to get the dates that are available is faster.
I have a scenario where I want to be able to SELECT rows from a MySQL table, but exclude rows where the current time-of-day is inside a time-range.
Example:
The "quiet" period for one row is 10pm - 8:30am.
My SQL SELECT statement should not return that row if the current server time is after 10pm or before 8:30am.
Example 2: The "quiet period" is NULL and ignored.
Example 3: A new row is created with a quiet period from 9:53am to 9:55am. If the current server time is in that 2-minute window, the row is not returned by the SELECT.
My question:
What data format would you use in the database, and how would you write the query?
I have thought about a few different approaches (defining start_time as one column and duration as another, defining both in seconds... or using Date stamps... or whatever). None of them seem ideal and require a lot of calculation.
Thanks!
I would store the start and end dates as MySQL native TIME fields.
You would need to consider ranges that span midnight as two separate ranges but you would be able to query the table like this, To find all current quiet periods
SELECT DISTINCT name FROM `quiet_periods`
WHERE start_time<=CURTIME() AND CURTIME()<=end_time
Or to find all non-active quiet periods
SELECT name FROM quiet_periods WHERE name NOT IN (
SELECT name FROM `quiet_periods`
WHERE start_time<=CURTIME() AND CURTIME()<=end_time
)
So with sample data
id -- name -- start_time -- end_time
1 -- late_night -- 00:00:00 -- 08:30:00
2 -- late_night -- 22:00:00 -- 23:59:59
3 -- null_period -- NULL -- NULL
4 -- nearly_10am -- 09:53:00 -- 09:55:00
At 11pm this would return
null_period
nearly_10am
from the second query.
Depending on performance and how many rows you had you might want to refactor the second query into a JOIN and probably add the relevant INDEXes too.