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
Related
This question already has answers here:
Is there a way to access the "previous row" value in a SELECT statement?
(9 answers)
Closed 8 months ago.
How can I get the result in the PreviousDay column that the day should be -1 from the day in efdt column for all rows?
If the efdt date is 2001-04-02 the result in PreviousDay should be 2001-04-01 and DATEDIFF is 334
Image attached with it please refer it.
You can use the SQL Server LEAD window function over the "efdt" field, which will take the following value given two clauses:
PARTITION BY, which indicates the groups to work on
ORDER BY, which decided which value to take according to an order imposed by one (or more) of your fields.
SELECT T.*,
DATEDIFF(DAY, efdt, PreviousDay)
FROM (SELECT T1.*,
LEAD(efdt) OVER(PARTITION BY emcd ORDER BY efdt) AS PreviousDay
FROM psf206 T1) AS T
ORDER BY emcd, efdt
Check the demo here.
Note: if you want one day less for each row, just add -1 after the DATEDIFF operation (demo).
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
);
This question already has an answer here:
Mysql : Aggregation function is not working without group by
(1 answer)
Closed 1 year ago.
SELECT EXTRACT(YEAR FROM created_at) AS yr, count(*) AS users_count
FROM users;
I'm new to SQL (so new that I can't find anything wrong with this). There may not be, and it may be a trick question. Would appreciate feedback!
You should use "Group BY" function too, because you use COUNT function with an another column.
For example:
SELECT EXTRACT(YEAR FROM created_at) AS yr, count(*) AS users_count FROM users group by YR;
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
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.