How to use the keyword after AS in WHERE [duplicate] - sql

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;

Related

Count over previously defined alias [duplicate]

This question already has answers here:
Why can't I use column aliases in the next SELECT expression?
(4 answers)
Closed 3 months ago.
I'm trying to avoid writing twice the EXTRACT condition. There is any way to do that? Count over the previous alias apparently is not valid.
SELECT EXTRACT(DECADE FROM to_date(released_year::text, 'yyyy')) AS decade,
count(decade) AS total_by_decade
FROM album ...
Basically you can avoit it completely, but you can make a CTE
WITH
CTE as(
SELECT EXTRACT(DECADE FROM to_date(released_year::text, 'yyyy')) AS decade
FROM album ...)
SELECT decade, count(decade) AS total_by_decade FROM CTe GROUP BY decade

How to find Minimum semester in Oracle? [duplicate]

This question already has answers here:
Fetch the rows which have the Max value for a column for each distinct value of another column
(35 answers)
GROUP BY with MAX(DATE) [duplicate]
(6 answers)
Select First Row of Every Group in sql [duplicate]
(2 answers)
Oracle SQL query: Retrieve latest values per group based on time [duplicate]
(2 answers)
Return row with the max value of one column per group [duplicate]
(3 answers)
Closed 1 year ago.
I want to select starting semester of the student.
select distinct (stdcode),altcode,name,admdate,sem_code
,min(startsem)
from V_ALLSTUDATAAA
GROUP BY stdcode,altcode,name,degree_code,sem_code
order by altcode;
Desired Results:
Sample Data is attached below.
https://docs.google.com/spreadsheets/d/1-oqXgGfhIiLwWKLpUx94P9n1hXUAUE3dn3jAPV8HQ_k/edit?usp=sharing
One option is to use a correlated subquery; something like this:
select a.stdcode, a.altcode, a.name, a.admdate, a.degree_code, a.sem_code, a.startsem
from v_allstudataaa a
where a.startsem = (select min(b.startsem)
from v_allstudataaa b
where b.stdcode = a.stdcode
);

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

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.