How to find Minimum semester in Oracle? [duplicate] - sql

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
);

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

PostgreSQL Using Max and Round [duplicate]

This question already has answers here:
How to round an average to 2 decimal places in PostgreSQL?
(8 answers)
Changing data type to float and rounding to 2 decimal digits
(2 answers)
Closed 1 year ago.
How do you use MAX and ROUND at the same time?
i.e.
SELECT
customer_id,
MAX(some_number)
FROM
customer_data
GROUP BY
customer_id;
60.2354125
Would like to return the result of 60.24
You would write this as:
SELECT customer_id, ROUND(MAX(some_number), 2)
FROM customer_data
GROUP BY customer_id;
EDIT:
Based on your comment, you can convert to a numeric first:
SELECT customer_id, ROUND(MAX(some_number)::numeric, 2)
FROM customer_data
GROUP BY customer_id;
Thanks for pointing me in the right direction #Gordon. The solution was casting as numeric
round(MAX(cast(some_number as numeric)),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

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;

My own GROUP BY function [duplicate]

This question already has answers here:
Product() aggregate function
(3 answers)
Closed 6 years ago.
I'd like to be able to create my own GROUP BY function. Like one has AVG() or SUM() functions which can be used in GROUP BY where average and sum is calculated from values in each particular group, I'd like to be able to create my own function which will calculate something from values in group. Let's say I'd like to create MYFN() function which will calculate Σx^2/n (x is value in group and n number of values in group). The usage of this function should look like this:
SELECT mygroup,MYFN(x)
FROM mytable
GROUP BY mygroup
ORDER BY 2 DESC
How to do that?
SELECT mygroup, sum(ColFun) As ColFun FROM
(
SELECT mygroup,MYFN(x) As ColFun FROM mytable
)Y
GROUP BY mygroup