I'm using spring data and am trying to write a query which returns all entries within the past week. The following is my repository code:
#Query("select l from LogEvent l where (l.entryTimestamp < CURRENT_TIMESTAMP) AND l.entryTimestamp IN l")
Page<LogEvent> findAllRecent(Pageable pageable);
I want to select all LogEvent objects which have a "entryTimestamp" variable holding the time at which they were created. I want to grab all entries created within the last 7 days. One of the issues I'm having is how to get a TIMESTAMP - 7 days that I can compare to each entity's timestamp. I've tried a number of things in looking around, like "interval '7' days" and some other examples I could find.
It's obviously lacking that week old timestamp comparison, but right now I feel like it should run and return all the LogEvent objects. As it is now, I get the following error: inconsistent datatypes: expected TIMESTAMP got NUMBER. Any pointers/help would be greatly appreciated. Thank you!
Related
I know this is a tough one but I'm basically trying to say. Give me a service call and its completion date, then give me the Max date for all service calls where the date is less than the date of the service call I'm inquiring about.
Basically the end result I'm looking for is to say was there another service call on this piece of equipment that was within the last 30 days.
So as you can see in the image for say Asset 50698 service call 579032 we have a date of 11/9/2020 the call below that was 10/22/2020 which was less than 30 days. I want to somehow find a way to count how many service calls I have where this has occurred. Is this possible?
I think you're looking for a context operator In, ForEach or ForAll (in in this case)
Add a variable "MaxAssetDate" and assign it a Formula similar to the following based on your column headers.
=Max([Service Call Completion Date] In ([Asset ID];[Service Call])) In (Asset ID])
Then add this as a column. Provided you have a prompt filtering for a given asset or "date" this column will then show the max date for each service call of the same asset ID. Then add a new variable: ServiceCallDaysDiff: Then by using DatesBetween() with "MaxAssetDate" and ServiceCallCompletionDate and DayPeriod; =DatesBetween([ServiceCallCompletionDate];[MaxAssetDate];DayPeriod) you should get a number 0-X. Then add a filter based if the number is between 1 and 30 then you show those records, otherwise hide the rest; or do whatever logic is then needed.
Now if you're dealing with hundreds of thousands of records this isn't ideal as you're putting all the processing on the webi engine when it ideally would occur as an object in the database layer. However if you only have a few thousand records this should be managable.
To add a count of service calls...
add variable: ServiceCallsCount:
=Sum(Sum(If([ServiceCallDaysDiff]=0;0;1)) In ([AssetID]))
this will count the non zero day differents. Note this will extend beyond 30 so if you want to limit by 30 days adjust the if statement to zero out those not between 1 and 30.
This is but one approach: there may be simpler ways.
I'm trying to get the difference between the 2 dates in googlescript. However it doesn't seem to be working. Does anyone seem to know why? My user defined variables are working it's just when i get the difference between the 2 the result is like 4.28E8 whereas the answer is supposed to be like 40. Is there a way to convert the date into a number then convert it back into a date?
ddate = output.getRange(lRow2,2,1,1).getValue() - output.getRange(2,1,1,1).getValue()
Function works, its just the difference is in milliseconds. For those interested in the conversion, it's as follows:
diff = ((output.getRange(lRow2,2,1,1).getValue() - output.getRange(2,1,1,1).getValue()) / (1000*60*60*24))
or simply:
days = milliseconds/ (1000*60*60*24)
I'd like to add a specific number of days to a date column and then compare that to the current date. Is there any way I can achieve this that is compatible with both HSQLDB (for testing) and MySQL (production)?
To make things even more complicated, I am using Hibernate.
Don't know the background of your problem, but can you do it the other way around: compare dae column with current date substracted by number of days?
I meant something like this in hql (I'm using jpql in this example, if you prefer JPA interface):
TypedQuery<SomeClass> q = getEntityManager().crateQuery(
"select s from SomeClass s where s.date = :date", SomeClass.class);
Calendar now = Calendar.getInstance();
// subtract number of days from current date, e.g. 10 days
now.add(Calendar.DAY, -10);
q.setParameter("date", now);
List<SomeClass> = q.getResultList();
This approach is database-agnostic, but of course works only for trivial cases, in some mor e complicated cases it will not work.
How to filter rows from MySQL database ignoring the time portion of a given DateTime field in MySQL using JPA?
For example, the following segment of code counts the number of rows from a database table that lie between the two dates given in a column of type DateTime in MySQL.
CriteriaBuilder criteriaBuilder=entityManager.getCriteriaBuilder();
CriteriaQuery<Long>criteriaQuery=criteriaBuilder.createQuery(Long.class);
Root<Discount> root = criteriaQuery.from(entityManager.getMetamodel().entity(Discount.class));
criteriaQuery.select(criteriaBuilder.countDistinct(root));
DateTimeFormatter dateTimeFormatter=DateTimeFormat.forPattern("dd-MMM-yyyy hh:mm:ss aa");
DateTime firstDate = dateTimeFormatter.parseDateTime("01-Oct-2013 11:34:26 AM").withZone(DateTimeZone.UTC);
DateTime secondDate = dateTimeFormatter.parseDateTime("31-Oct-2013 09:22:23 PM").withZone(DateTimeZone.UTC);
criteriaQuery.where(criteriaBuilder.between(root.get(Discount_.discountStartDate), firstDate, secondDate));
Long rowCount = entityManager.createQuery(criteriaQuery).getSingleResult();
The two parameters firstDate and secondDate will be in turn dynamic.
How to rewrite this query so that the comparison does not include the time portion in the SQL query which is to be delegated to MySQL.
The column discount_start_date in the entity Discount is designated as follows.
#Column(name = "discount_start_date")
#Type(type="org.jadira.usertype.dateandtime.joda.PersistentDateTime")
private DateTime discountStartDate;
Seems like you are working too hard.
(a) Apparently, MySQL offers a DATE() function that extracts the date portion of a date-
time field. (I'm a Postgres guy, and don't know MySQL.) You could pursue an approach using that function call as part of your query. But I'm guessing it would faster performance if you first obtained your start and stop time by calculating with Joda-Time in Java before executing the SQL query, as seen below.
(b) Why not do this with a simple SQL query, a two criteria SELECT?
In pseudo-code:
Find Discount records that go into effect from the moment this month starts up until the moment the next month starts.
Use Java and Joda-Time to give you the start & stop values.
org.joda.time.DateTime startOfThisMonth = new org.joda.time.DateTime().dayOfMonth().withMinimumValue().withTimeAtStartOfDay();
org.joda.time.DateTime startofNextMonth = startOfThisMonth.plusMonths( 1 ).dayOfMonth().withMinimumValue().withTimeAtStartOfDay();
Caution: Above code uses default time zone. You should specify a time zone in the constructor.
MySql seems to lack sophisticated time-date handling with time zones etc. So I suppose you would convert those time zoned DateTime objects to UTC.
org.joda.time.DateTime startOfThisMonthInUtc = startOfThisMonth.toDateTime( org.joda.time.DateTimeZone.UTC );
org.joda.time.DateTime startofNextMonthInUtc = startofNextMonth.toDateTime( org.joda.time.DateTimeZone.UTC );
Then do what you do to get date-time values for MySQL.
Then form a query that looks something like this… (Note the use of >= versus < without the Equals sign.)
SELECT title_, amount_, start_date_
FROM discount_
WHERE discount_.start_datetime_ >= startOfThisMonthFromJodaTime
AND discount_.start_datetime_ < startOfNextMonthFromJodaTime
;
When working with date and time, it's generally better to work with the first moment of the day, first moment of the first day of month, etc. rather than try to find the last moment or end time. So my query is based on the idea of find rows whose values go up to, but do not include, the moment after the time frame in which I'm interested.
I've got a client application that's going to update a database every five minutes with the current time, and then I want to output this time as a last active table in a seperate VB application.
I know about mysql time, but I don't quite understand how I can use it to display when a client was last active.
I've looked around and found some stuff about mysql times but I don't fully understand it.
Any help would be great, I'm going to place the results in a ListView with 'Client Name' and 'Last Active' if this helps, and I already know how to connect to my database and retrieve information.
Thank you.
I'd recommend using a DATETIME for storage. The TIME data type is limited to a single "time of day" or a timespan. True, you're looking for the time of day, but to calculate the "Last Active" time you need the date attached. Consider these "Last Active" values (using a 24-hour clock):
3/26/2013 at 17:00:00 <-- this has the maximum time (5PM), but...
3/27/2013 at 08:15:00 <-- ...this is the most recent time because it happens the following day
In other words, you need the date so you can sort the time.
The MySQL DATETIME data type should be supported by VB.NET, but I've never used the two together so I can't guarantee it. To query and report just the time component of the date you have a ton of options. Here are two:
Query the entire date/time from MySQL and return it as a System.DateTime value to VB.NET. In VB.NET you can format it using DateTime.ToString to show only the time components. The MySQL query would go something like this:
SELECT ClientName, MAX(LastActive) AS LastActiveDateTime
FROM your_table
GROUP BY ClientName
Format the time in MySQL and return it as a String to VB.NET. In VB.NET you'll just need to display the string as is. The MySQL query would go something like this:
SELECT ClientName, DATE_FORMAT(MAX(LastActive), '%r') AS LastActiveTime
FROM your_table
GROUP BY ClientName
The format code %r in the above query will return the time in a 12-hour format with AM/PM, for example 07:55:29 PM. To return a 24-hour format (19:55:29), use %T instead.