How to substruct dates in oracle db? [duplicate] - sql

This question already has answers here:
Oracle date difference to get number of years
(5 answers)
Closed 2 years ago.
I want to substruct 2 dates(current date and hairdressing_date) to get the result from table to represent data during last 2 years.
I have following SELECT statement:
SELECT count(c_id)
INTO counter
FROM RESERVATIONS r
WHERE r.customer_id = 1
AND (Sysdate - r.hairdressing_date) / 365 < 2;
It is custom, but I am not sure about '/ 365' part of code.
How to get the needed data correctly?
Could you write the correct implementation of this line?

Please use MONTHS_BETWEEN() function like this:
SELECT MONTHS_BETWEEN(TRUNC(SYSDATE), TO_DATE('22.01.2019', 'DD.MM.YYYY'))/12
FROM DUAL;
and revert to this one considering your case:
SELECT count(c_id)
INTO counter
FROM RESERVATIONS r
WHERE r.customer_id = 1
AND MONTHS_BETWEEN(TRUNC(SYSDATE), hairdressing_date)/12 < 2;

Related

How to fix "Inner Join Group-By" question [duplicate]

This question already has answers here:
ORA-00979 not a group by expression
(10 answers)
Closed 3 years ago.
Trying to run a query to pull back no duplicate lines. Currently when running this script I get an error back from Oracle saying
"ORA-00979: not a GROUP BY expression"
Tried removing different select rows.
SELECT QUEUE_NAME, ITEMS_IN_QUEUE, CREATION_USR_ID
FROM IN_WF_QUEUE B
INNER JOIN IN_WF_ITEM A
ON A.QUEUE_ID = B.QUEUE_ID
AND B.QUEUE_NAME LIKE '__________ %'
AND (A.QUEUE_START_TIME < (select sysdate from DUAL)-1000)
GROUP BY B.QUEUE_ID;
Thank you in advance for any help on this issue.
If you want to remove duplicates, why don't you use DISTINCT? That's more natural than using GROUP BY without any aggregates.
SELECT DISTINCT
queue_name, items_in_queue, creation_usr_id
FROM in_wf_queue b
INNER JOIN in_wf_item A
ON A.queue_id = b.queue_id
AND b.queue_name LIKE '__________ %'
AND A.queue_start_time < sysdate - 1000;
Also, there's no need to select SYSDATE from dual; it is a function that can be used standalone, as in my example. Note that it returns both date and time, so - maybe you'd want to remove time component by truncating it, i.e. use trunc(sysdate). Subtracting 1000 from it means "1000 days ago"; just saying, to avoid possible confusion.

How to find the largest range between related dates in a column? [duplicate]

This question already has answers here:
Oracle SELECT TOP 10 records [duplicate]
(6 answers)
Closed 3 years ago.
I have a database where there are bands in one column and another column where there are release dates of their albums, how do i find the oldest band based off of the difference between their release dates? I.e. the oldest band is not Band1 because they first released an album in 1968, with my method Band1 is only 7 years old (or active for 7 years could be better), while Band2 is 26 years old (years active) because 2001 - 1975 = 26.
For instance:
Band......Rel_date
Band1....12/12/68
Band1....08/05/75
Band2....09/02/75
Band2....05/05/99
Band2....03/05/01
Oracle 12c+
select band from your_table order by rel_date fetch first 1 rows only;
If you want to include ties, change it to with ties instead of only
EDIT
select band from t
group by band order by max(rel_date) - min(rel_date) desc fetch first 1 rows only;
EDIT2
For Oracle versions before 12c:
select * from
(
select band from t
group by band order by max(rel_date) - min(rel_date) desc
) where rownum = 1
If I understand what you're asking
SELECT BAND
FROM YOUR_TABLE
WHERE REL_DATE = (SELECT MIN(REL_DATE)
FROM YOUR_TABLE)
oughta do it.
EDIT
Based on further information
WITH cteBand_activity AS
(SELECT BAND,
MIN(RELEASE_DATE) AS FIRST_RELEASE_DATE,
MAX(RELEASE_DATE) AS LATEST_RELEASE_DATE,
TRUNC((MAX(RELEASE_DATE) - MIN(RELEASE_DATE)) / 365) + 1 AS YEARS_ACTIVE
FROM ALBUMS
GROUP BY BAND
ORDER BY YEARS_ACTIVE DESC)
SELECT BAND, YEARS_ACTIVE
FROM cteBand_activity
WHERE ROWNUM = 1
should give the result you're looking for.
dbfiddle here
Best of luck.

Binding parameter in SELECT query for PostgreSQL [duplicate]

This question already has answers here:
How to declare a variable in a PostgreSQL query
(15 answers)
Closed 3 years ago.
select (CURRENT_TIMESTAMP - '60 days'::interval);
This shows me a current time stamp with an interval of 60 days getting subtracted.
SELECT VALUE
FROM schema_name.some_parameter
WHERE some_parameter.NAME LIKE 'some_reference_name'
I want to add the above query in SELECT so that I don't need to add the hardcoded data for 60 days. I want to get it through the parameter variable.
Basically i need to use nested queries where the second query gets me the 60 days value i.e. hardcoded in first query.
Is there a possible solution for my problem?
Maybe something like this?
with v(val)
as
(
VALUES( CURRENT_TIMESTAMP - '60 days'::interval)
)
SELECT v.val from
schema_name.some_parameter cross join v
WHERE some_parameter.NAME LIKE 'some_reference_name'

can you get me select statement Modified_Date=Today? [duplicate]

This question already has answers here:
how to get current/Todays date data in sql server
(5 answers)
Closed 4 years ago.
I need to get all the records using MS SQL SELECT where Modified_Date=Today
my table looks like:
created_by modified_date
20150723081006 2015-07-23 15:14:22.843
20150723081006 2015-09-04 00:14:24.463
20150723081006 2015-09-04 00:14:24.463
One simple way to accomplish this would be to cast your modified date as a date value (removing the time portion) and then comparing it to the current date (again, with time removed):
SELECT *
FROM MyTable
WHERE CAST(Modified_Date AS DATE) = CAST(GETDATE() AS DATE)

SQLite - SELECT TOP syntax error [duplicate]

This question already has answers here:
How to get Top 5 records in SqLite?
(8 answers)
Closed 6 years ago.
I'm trying to use the statement SELECT TOP 1 * FROM tasks WHERE dueDate < ?1 ORDER BY dueDate DESC but SQLite says near "1": syntax error. What's wrong?
Use LIMIT 1 at the end of the query instead of TOP 1 (which isn't valid sqlite syntax).
You might also need to remove the ? in dueDate < ?1, but I don't know sqlite well enough to be sure.