SQLite - SELECT TOP syntax error [duplicate] - sql

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.

Related

How to substruct dates in oracle db? [duplicate]

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;

query with top and order by - convert from MsSql to Oracle [duplicate]

This question already has answers here:
How do I limit the number of rows returned by an Oracle query after ordering?
(14 answers)
Closed 2 years ago.
I need to convert this query from MsSql syntax to Oracle syntax:
select top 1 (convert(varchar, UPDATED_DATE, 23)) as date from DA_CATEGORY order by date desc
How do I do this?
I need the data from both DB types to be the same string / value.
You can use the fetch clause as follows:
select to_char(UPDATED_DATE,'YYYY-MM-DD') as date
from DA_CATEGORY
order by UPDATED_DATE desc
fetch first row only

Oracle sql rownum between how to shrink the query [duplicate]

This question already has answers here:
How do I limit the number of rows returned by an Oracle query after ordering?
(14 answers)
How ROWNUM works in pagination query?
(3 answers)
Oracle SELECT TOP 10 records [duplicate]
(6 answers)
Oracle "LIMIT n,m" equivalent [duplicate]
(1 answer)
Closed 2 years ago.
I would like to get records from 25 to 50. I write this code, but, it looks terrible with double select clause.
Select * From (
Select eto.*, rownum rn from employee_trip_orders eto
) where rn between 25 and 50 ;
How can i shrink it to use one select like that?
Select eto.*, eto.rownum rn from employee_trip_orders eto
where rn between 25 and 50 ;
I don't need the second one. Thanks. I have old 11c Oracle version and offset keyword is not suitting me
How can i shrink it to use one select like that?
Since you are on Oracle 11g you cannot. You must use subquery inline to achieve your desired output.
Select eto.*, eto.rownum rn from employee_trip_orders eto
where rn between 25 and 50 ;
That query will never return a row. ROWNUM value is incremented only after it is assigned. Please see How ROWNUM works in pagination query.
From Oracle 12c onwards, you could use the new Top-n Row limiting feature.
You are not using order by clause so what is the meaning of the rownum? In the end, You are only fetching random 26 (25-50 inclusive) records.
So You can achieve the desired result using the following code:
Select eto.*, rownum rn
from employee_trip_orders eto
where rownum<= 26 ;
Cheers!!

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 use the keyword after AS in WHERE [duplicate]

This question already has answers here:
Using an Alias in a WHERE clause
(5 answers)
SQL Use alias in Where statement
(10 answers)
Using alias name in WHERE clause [duplicate]
(3 answers)
Closed 3 years ago.
I am calculating the total year that a member have been registered.
I need to return the result how many member registered more than 5 years but I am having error when I run the code.
It shows "duration" invalid identifier in line 3
select floor(months_between(SYSDATE,RegistrationDate)/12) as "Duration"
from member
where duration > 5;
You can use a subquery:
select *
from
(
select floor(months_between(SYSDATE,RegistrationDate)/12) as "Duration"
from member
)
where "Duration" > 5;