How can I extract month and years using JPA - spring

I hope you can help me.
I have this situation:
PostgreSQL
Table: Flags
id_flag (integer) | flag_type (smallint) | start_date (date)
1 2 2020-01-01
I have a FlagsRepository.java
public interface FlagsRepository extends JpaRepository<Flags, Integer> {
#Query(value="select type_flag from web_flags where extract(month from start_date)=:month and extract(year from start_date)=:year"
List<Flags> myFindUsingMonthAndYear( #Param("month") Integer month, #Param("year") Integer year);
}
When I execute using CommandLineRunner on my Application.java it returns an error: could not execute the query.
How can I solve it? Thanks

Related

Oracle - How to check entered date is greater than 9 months

Need to check whether the employee has crossed the 9 months or not from his joining date.
Creating sample data as:
create table employee(id number,hire_date date);
insert into employee values(1,'12-APR-2017');
insert into employee values(2,'18-Oct-2017');
insert into employee values(3,'01-Jan-2018');
select id,hire_date, case when months_between(sysdate,hire_date) > 9 then 'Crossed' else 'Not-Crossed' end is_crossed from employee;
The output will be:
ID HIRE_DATE IS_CROSSED
1 12-APR-17 Crossed
2 18-OCT-17 Not-Crossed
3 01-JAN-18 Not-Crossed
SELECT to_char(TO_DATE(joindt,'DD/MM/RRRR')) < add_months(TO_DATE(SYSDATE),-9)
FROM PYEMPMAS
Thanks guys. I got a solution..

How to use IN operator in where clause of SpringBoot Cassandra

I am trying to run following query from SpringBoot repository
#Query("select * from c where id1 =?0 and id2 =?1 and id3 =?2 and c1 =?3 and year in :year and month in :month and day in :day and hour in :hour and minute in :minute"
public List<Counter> findCWithinRangeIn(
#Param("id1") String id1,
#Param("id2") String id2,
#Param("id3") String id3,
#Param("c1") String c1,
#Param("year") List<Integer> year,
#Param("month") List<Integer> month,
#Param("day") List<Integer> day,
#Param("hour") List<Integer> hour,
#Param("minute") List<Integer> minute);
But always hitting error : Invalid amount of bind variables.
Please suggest on how to proceed further.
Note: I did follow Spring CrudRepository findByInventoryIds(List<Long> inventoryIdList) - equivalent to IN clause but it did not work for me.
I want to execute something like:
select * from c
where id1 ="xyz" and
id2 ="abc" and
id3 ="pqr" and
c1 = "Sample" and
year IN (2016,2017) and
month IN (9,10,11) and
day IN (19,20,24) and
hour IN (23, 13) and
minute IN (50, 51, 53)
If you don't need to specify custom query this could work for you (based on method name)
public List<Counter> findAllById1AndId2AndId3AndC1AndYearInAndMonthInAndDayInAndHourInAndMinuteIn(String id1, STring id2, String id3, String c1, List<Long> years, List<Long> months, List<Long> days, List<Long> hours, List<Long> minutes)
note: in your Counter entity the field's names must match the names of attribute names specified in method name.

"Select Extract Minute" failed in oracle

Please help me to solve this problem.
I need to extract minute from hour ("JAM" column) in a table .
I have try this query :
WITH recordabsen AS
(SELECT userid,
TO_CHAR(checktime,'MM/DD/YYYY') AS tanggal ,
MIN(TO_CHAR(checktime,'hh24:mi')) AS JAM
FROM checkinout
WHERE USERID = '688'
AND (checktime BETWEEN to_date('04/01/2013','MM/DD/YYYY') AND to_date('05/01/2013','MM/DD/YYYY'))
AND checktype = 'I'
GROUP BY userid,
TO_CHAR(checktime,'MM/DD/YYYY')
)
SELECT EXTRACT (MINUTE FROM JAM) AS minute
FROM recordabsen
WHERE to_date(JAM,'hh24:mi') > TRUNC(to_date(JAM,'hh24:mi')) + 8/24
but returns an error :
Invalid Extract field
As soon as you've done:
to_char(checktime,'hh24:mi')
you got a string and not a date, so maybe it's better to use strings functions and not date functions, i.e.:
substr("JAM", 4)
Here is a sqlfiddle demo
Why don't you just pass the checktime from the WITH clause query? Then, if checktime is a date, you can cast it to TIMESTAMP, which will allow you to use EXTRACT MINUTE:
SELECT EXTRACT (MINUTE FROM CAST(SYSDATE AS TIMESTAMP)) FROM dual;
Check at SQLFiddle: http://www.sqlfiddle.com/#!4/d41d8/18054

Doctrine Query count() and groupBy() date

I need to find the total number of logins per day, but how would I select count() and group by day in DQL? I'm using Doctrine 2.3.
public function getLoginCount()
{
return $this->createQueryBuilder('i')
->select('i') // and count(*)
->groupBy('i.timestamp') // group by day
->getQuery()
->execute()
;
}
I need something like this:
Date | count
2013-01-01 | 6
2013-01-02 | 7
2013-01-03 | 3
From help on the Doctrine IRC channel you need to create a custom DQL function.
Example:
https://github.com/beberlei/DoctrineExtensions/blob/master/lib/DoctrineExtensions/Query/Mysql/Day.php
Docs:
http://www.doctrine-project.org/blog/doctrine2-custom-dql-udfs.html
A bit late for OP, but maybe someone will find it handy. I was able to achieve that with the DQL query bellow:
$dql = '
SELECT
SUBSTRING(i.timestamp, 1, 10) as date,
COUNT(i) as count
FROM Entity i
GROUP BY date
';
$query = $entityManager->createQuery($dql);
return $query->getResult();
I think similar should be doable with Doctrine query builder.

JPA 2 Criteria Day from date

I have a field in a table and that stores a date. I'd like to select all records that which the date is on day 5.
After a lot of research I get the code below:
Predicate dayValue = cb.equal(cb.function("day", Integer.class, test.<Date>get(Test_.dateInit)), now.get(Calendar.DAY_OF_MONTH) );
But, I'm using a Oracle database and it doesn't have the function day:
[EL Warning]: 2013-01-14 11:51:08.001--UnitOfWork(23011228)--Thread(Thread[main,5,main])--Exception [EclipseLink-4002] (Eclipse Persistence Services - 2.1.2.v20101206-r8635): org.eclipse.persistence.exceptions.DatabaseException
Internal Exception: java.sql.SQLException: ORA-00904: "DAY": invalid identifier
Is there an other way to do this select?
Thanks
I got a way.
Using the function to_char of oracle in the function method:
Predicate dayValue = cb.equal(cb.function("to_char", Integer.class, test.<Date>get(Test_.dateInit),cb.parameter(String.class, "dayFormat")), now.get(Calendar.DAY_OF_MONTH) );
...
q.setParameter("dayFormat", "DD");
In Oracle you can use
select to_char (date '2013-01-14', 'D') d from dual;
to give you the day number, the above will output 1 for Monday
if you want to see Monday just change to
select to_char (date '2013-01-14', 'Day') d from dual;
the above will output Monday
hope that helps.

Resources