want help in sql query, I am using H2 database
i have 12 dates (date with time) in a row, field name like Date_1, Date_2, Date_3... Date_12
like to know which has max date and Time. have to know whether is there any date which is less than 10 minutes from current time.
can some one help in sql query for the above
You can use GREATEST to find max value from current row:
SELECT GREATEST(Date_1, Date_2, Date_3,... ,Date_12) AS Max_date_in_row
FROM your_table;
To find out:
is there any date which is less than 10 minutes from current time
SELECT *
FROM your_table
WHERE GREATEST(Date_1, Date_2, Date_3,... ,Date_12) > DATE_ADD(NOW(), INTERVAL -10 MINUTE);
Related
I am using DB2 database and able to fetch data using below SQL. Here, I am looking for data
of last 10 minutes. Here CURRENT TIMESTAMP - 10 MINUTE is nothing but subtracting 10 minutes from current timestamp.
SELECT * FROM TEST WHERE param1 = '1234' AND param2 = '1' AND param3 = '0' AND DATE_TS > CURRENT TIMESTAMP - 10 MINUTE;
I am facing issue while using it with Hibernate Query Language. In Hibernate, I am able to perform the same using Native Query. But, I have to do it with HQL. How can I do it? How to reduce 10 minutes from current timestamp at SQL level.
Note: I tried in Java using reducing 10 minutes from system timestamp. But I see there TIMEZONE difference of 12Hrs. So not able to get results of last 10 minutes.
You can try this
LocalDateTime checkForDate = LocalDateTime.now().minusMinutes(10);
createQuery("SELECT * FROM TEST WHERE param1 = '1234' AND param2 = '1' AND param3 = '0' AND DATE_TS >:checkForDate").setParameter("checkForDate", java.sql.timestamp.valueOf(checkForDate));
I want to select date between (System date - 2 hour ) to (system date)
I tested this for selecting records in the last two hours and it works.
#SQL Server
SELECT
*
FROM
table
WHERE
datetime_col >= DATEADD(hh, -2, CURRENT_TIMESTAMP)
AND
datetime_col <= CURRENT_TIMESTAMP;
I'm less familiar with Oracle but here is a possible solution I found online.
#oracle
SELECT
*
FROM
table
WHERE
datetime_col BETWEEN LOCALTIMESTAMP - 1/24 AND LOCALTIMESTAMP
Oracle Forum: Find record count between two time stamps
I have a table and in that table there is a column called date_created, which is in TIME STAMP format 2016-07-20 11:30:15 and while I am fetching record I have to pass only the month and year in my front-end field like (month_no/year) 1/2016, 2/2016, 3/2016..... So I have to fetch all the record based upon a month. So how could I achieve this query. So far I tried like this.
SELECT t.*
FROM stock t
WHERE MONTH(str_to_date(t.date_created,'%Y/%M')) = 7
Since you have a timestamp column you don't need to worry too much. You need to put the month number and year number in the corresponding position.
SELECT t.*
FROM stock t
WHERE MONTH(t.date_created) = ?
AND YEAR(t.date_created) = ?;
If month = 7 and year = 2016 then your query:
SELECT t.*
FROM stock t
WHERE MONTH(t.date_created) = 7
AND YEAR(t.date_created) = 2016;
If You have created indexes on columns then i would suggest you not use any function in where clause this makes query slow and non-sargable.. Please use the following link why i am saying this
What makes a SQL statement sargable?
I would Suggest you to use the following query.
SELECT t.*
FROM stock t
WHERE t.date_created between '2016-07-01 00:00:00.000' AND '2016-07-31 00:00:00.000'
This is my MySQL database table structure and attendancedate datatype is Date:
attendancedate-------admissionnumber------------attendance
2013-10-03-----------LSTM-0008/2013-2014--------present
2013-10-09-----------LSTM-0008/2013-2014--------present
2013-11-02-----------LSTM-0008/2013-2014--------absent
and i want to disaply like that
monthname---------totalworkingdays---------present----absent
october-------------- 2--------------------2----------0
November--------------1--------------------0-----------1
so am writing the below MySQL query:
select monthname(attendencedate) as monthname,
(select distinct count(*)
from lstms_attendence
where attendencedate>='2013-10-03'
and attendencedate<='2013-10-09'
and addmissionno='LSTM-0008/2013-2014')as totalworkingdays,
sum(CASE WHEN attendence = 'present' THEN 1 ELSE 0 END) as present,
SUM(CASE WHEN attendence = 'absent' THEN 1 ELSE 0 END) AS absent
FROM lstms_attendence
WHERE addmissionno='LSTM-0008/2013-2014' GROUP BY monthname(attendencedate);
But the query is display like this:
monthname---------totalworkingdays---------present----absent
November-----------3-----------------------0------------2
October------------3-----------------------2------------1
please give me the exact query and here am passing dates as hardcoded but that dates are passing dynamically through java to that query.
Let me know the how to create dynamically dates and how to pass values to that query.
Try this:
SELECT
MONTHNAME(attendencedate) as the_month,
COUNT(*) as working_days,
SUM(IF(attendance='present',1,0)) as is_present,
SUM(IF(attendance='absent',1,0)) as is_absent
FROM
lstms_attendence
WHERE
addmissionno='LSTM-0008/2013-2014'
GROUP BY
the_month
Your question doesn't make clear what you're asking. What's wrong with your resultset? Is it the presence of your "november" row?
At any rate, you've got an excessively complex query here, as well as one which will break if you happen to include data from multiple years in it.
Instead of using MONTHNAME(attendancedate), we'll use
DATE_FORMAT(attendancedate, '%Y-%c-01') AS attendancemonth
to figure out in which month an attendancedate lies. This simply turns '2013-11-15' into '2013-11-01', the first day of the month in question. In other words, it truncates (rounds down) the date to the first day of the month.
SELECT admissionnumber,
DATE_FORMAT(attendancedate, '%Y-%c-01') AS attendancemonth,
COUNT(*) AS working_days,
SUM(IF(attendance='present',1,0)) AS is_present,
SUM(IF(attendance='absent',1,0)) AS is_absent
FROM lstms_attendence
WHERE attendancedate >= '2013-10-03'
AND attendancedate < '2013-10-06' + INTERVAL 1 DAY
AND admissionnumber='LSTM-0008/2013-2014'
GROUP BY admissionnumber,attendancemonth
Here is a sqlfiddle.http://sqlfiddle.com/#!2/a2dfb/7/0
This summarizes by both admissionnumber and month, so you can produce a report that contains multiple admissions if you want that.
It uses attendancedate < 'last' + INTERVAL 1 DAY in place of attendancedate <= 'last' because that is robust in cases where attendancedate contains a timestamp other than midnight.
If you need to pass in your parameters from Java, use a prepared statement. Replace the constants in the SQL query with ?, and use setTimestamp and setString. For one example, see JDBC Prepared Statement . setDate(....) doesn't save the time, just the date.. How can I save the time as well?
I am writing a Java Program where I am required to select Records for the Current Month from an Oracle database. Any time I need to select Records for the Current day I use:
select * from purchases where purchase_date = to_char(sysdate, 'DD-MON-YYYY')
What Method should I use to select records from the current Month?
On the table the dates appear like:
10/4/2012 //for 4th October 2012
I tried
select * from purchases where purchase_date like to_char(sysdate, 'MON-YYYY')
This selects nothing of course please assist.
If purchase_date is a DATE.
select *
from
purchases
where
purchase_date >= trunc(sysdate,'mm')
If purchase_date is a VARCHAR
select *
from
purchases
where
to_date(purchase_date,'mm/dd/yyyy') >= trunc(sysdate,'mm')
trunc(sysdate,'mm') truncates the current date keeping the month and obviously the year, so 10/04/2012 08:58 is truncated to 10/01/2012 00:00.
Assuming purchase_date is a VARCHAR2 and formatted like DD-MON-YYYY, which appears to be the case, then:
select * from purchases where trunc(to_date(purchase_date,'DD-MON-YYYY'),'MONTH') = trunc(sysdate,'MONTH')