Kindly anyone tell me the logic for oracle sql [duplicate] - sql

This question already has answers here:
Get top results for each group (in Oracle)
(5 answers)
How to extract the first 5 rows for each id user in Oracle? [duplicate]
(2 answers)
First two salaries in each department [duplicate]
(3 answers)
Limit SQL query to only the top two counts per group [duplicate]
(1 answer)
Select top 15 records from each group [duplicate]
(2 answers)
Closed 12 months ago.
User requirement:- 2 policies in each product code with Highest (Maximum) SUM INSURED
How do I get 2 policies in each product code with Highest (Maximum) SUM INSURED
SELECT
PROD.MV_PREMIUM_REGISTER.T_DATE_DESC,
PROD.MV_PREMIUM_REGISTER.P_POLICY_NUMBER,
PROD.MV_PREMIUM_REGISTER.P_POLICY_STATUS,
PROD.MV_PREMIUM_REGISTER.VERSION,
PROD.MV_PREMIUM_REGISTER.P_RISK_INC_DATE,
PROD.MV_PREMIUM_REGISTER.P_OFFICE_LOC_ID,
PROD.MV_PREMIUM_REGISTER.LOCATION_DESC,
PROD.MV_PREMIUM_REGISTER.ZONE_DESC,
PROD.MV_PREMIUM_REGISTER.I_IMD_DESC,
PROD.MV_PREMIUM_REGISTER.P_SUB_IMD,
PROD.MV_PREMIUM_REGISTER.P_PRODUCT_ID,
PROD.MV_PREMIUM_REGISTER.P_RISK_EXPIRY_DATE,
PROD.MV_PREMIUM_REGISTER.P_GC_PLAN,
PROD.MV_PREMIUM_REGISTER.IMD_CHANNEL,
PROD.MV_PREMIUM_REGISTER.P_PRODUCT_DESC,
(PROD.MV_PREMIUM_REGISTER.SUM_INSURED) AS SUM_INSURED,
(PROD.MV_PREMIUM_REGISTER.GROSS_PREMIUM) AS GROSS_PREMIUM,
(PROD.MV_PREMIUM_REGISTER.STAMP_DUTY) AS STAMP_DUTY
FROM
PROD.MV_PREMIUM_REGISTER
WHERE
T_DATE_DESC BETWEEN '27-FEB-2022' AND '28-FEB-2022'
AND VERSION NOT LIKE '%E%'
AND LT_POLICY_YEAR = 1;

SELECT P_PRODUCT_ID,P_POLICY_NUMBER,SUM_INSURED from(
SELECT
PROD.MV_PREMIUM_REGISTER.P_PRODUCT_ID,
PROD.MV_PREMIUM_REGISTER.P_POLICY_NUMBER,
(PROD.MV_PREMIUM_REGISTER.SUM_INSURED) AS SUM_INSURED,
RANK() OVER(PARTITION BY P_PRODUCT_ID ORDER BY SUM_INSURED DESC) AS
RANK_COUNT
FROM
PROD.MV_PREMIUM_REGISTER
WHERE
T_DATE_DESC BETWEEN '27-FEB-2022' AND '28-FEB-2022'
AND VERSION NOT LIKE '%E%'
AND LT_POLICY_YEAR = 1) MVP where MVP.RANK_COUNT <3;

Related

OTBI function to get the maximum sequence number for a person [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)
Select First Row of Every Group in sql [duplicate]
(2 answers)
Return row with the max value of one column per group [duplicate]
(3 answers)
Get value based on max of a different column grouped by another column [duplicate]
(1 answer)
SQL: getting the max value of one column and the corresponding other columns [duplicate]
(2 answers)
Closed 7 months ago.
I have a column that gives sequence number and another for phase name for a person number
PERSON_NUMBER
SEQUENCE
Phase Name
10
1
Initial
10
2
Mid
10
3
Final review
11
1
Initial
11
2
Mid
I want to get the person number and phase against max sequence phase for the employee -
PERSON_NUMBER
SEQUENCE
Phase Name
10
3
Final review
11
2
Mid
Which function can I use in OTBI for this ? I tried using
"max("Job Application - Progression"."Current Phase Sequence Number") keep(dense_rank first order by "Job Application - Progression"."Current Phase Name" desc)"
But this is not the correct syntax for OTBI

How to get the list latest entries from the database table for list of values for the specific column? [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)
Select First Row of Every Group in sql [duplicate]
(2 answers)
Return row with the max value of one column per group [duplicate]
(3 answers)
Get value based on max of a different column grouped by another column [duplicate]
(1 answer)
GROUP BY with MAX(DATE) [duplicate]
(6 answers)
Closed 2 years ago.
I have a database table where there is a column hostname and few other columns.
in hostname, there are many instances of rows with same hostnames.
Ex. 192.0.0.1 has 40 entries and 192.0.0.2 has 35 entries and so on.
Is it possible to get the list of latest entries for each hostname? meaning in the result i should get one latest for 192.0.0.1 and one latest for 192.0.0.2 and so on.
I tried with
SELECT host,cluster,region,service
FROM system
WHERE host IN ("171.33.64.158","171.33.64.159")
ORDER BY id DESC LIMIT 1;
but the output contains only one row which is probably the first row of the entire result.
Can anyone please help me in this requirement?
Thanks,
Swapnil
Try this:
select host,cluster,region,service
from (
SELECT host,cluster,region,service, row_number() over (partition by host order by id
desc) as r_num
FROM system
WHERE host IN ("171.33.64.158","171.33.64.159"))
where r_num = 1;
Thanks
For proper ordering you need to convert the IP address into decimal equivalent. I would suggest a function like this:
FUNCTION IP2Decimal(IP IN VARCHAR2) RETURN NUMBER DETERMINISTIC IS
DecimalIp NUMBER;
BEGIN
SELECT SUM(REGEXP_SUBSTR(IP, '\d+', 1, LEVEL) * POWER(256, 4-LEVEL))
INTO DecimalIp
FROM dual
CONNECT BY LEVEL <= 4;
RETURN DecimalIp;
END IP2Decimal;
Then your query could be this:
SELECT hostname,
FIRST_VALUE(host) OVER (ORDER BY IP2Decimal(host)),
FIRST_VALUE(cluster) OVER (ORDER BY IP2Decimal(host)),
FIRST_VALUE(region) OVER (ORDER BY IP2Decimal(host)),
FIRST_VALUE(service) OVER (ORDER BY IP2Decimal(host))
FROM system
GROUP BY hostname;

Concatenate multiple rows to one row [duplicate]

This question already has answers here:
Simulating group_concat MySQL function in Microsoft SQL Server 2005?
(12 answers)
SQL Server: Combine multiple rows into one row from a join table?
(1 answer)
ListAGG in SQLSERVER
(4 answers)
how to stuff date and time in SQL
(2 answers)
Query to get multiple row into single row
(3 answers)
Closed 3 years ago.
I have the below data and I have to concatenate the long text column and make it as a single row. The challenge part is only one row has the notification number and other rows are null. You cannot group by the notification number.
I need the output as 2 rows
row number Notification Number Plant Creation Date Language Lineno Tag Long Text
1 10014354914 A057 43466 EN 1 >X aaabbbcccdddeeefffggghhhjjjkkklll
2 10014354915 A057 43466 EN 1 >X aaabbbcccdddeeefffgggpppqqqrrrsss
I have used cursor for this. But it is taking much time.
If you are using oracle:
with data("row number", "Notification Number","Plant","Creation Date","Language","Lineno","Tag","Long Text") as (
select 1,10014354914,'A057',43466,'EN',1,'>X','aaabbbcccdddeeefffggghhhjjjkkklll' from dual
union all
select 2,10014354915,'A057',43466,'EN',1,'>X','aaabbbcccdddeeefffgggpppqqqrrrsss' from dual)
select LISTAGG("Long Text",'') within group (order by "row number") from data;
if you are using ms-sql maybe try this:
SELECT u.[Long Text] AS [text()]
FROM yourtable u
ORDER BY u.[row number]
FOR XML PATH ('')

What is wrong in my sql WITH and SORT in postgresql [duplicate]

This question already has answers here:
ORDER BY Alias not working
(6 answers)
Postgres CASE in ORDER BY using an alias
(2 answers)
Alias with Order by
(2 answers)
Closed 4 years ago.
In the picture: I using pgadmin 4 and Postgres 9.4.
https://www.postgresql.org/docs/current/static/sql-select.html#SQL-ORDERBY
...can be the name or ordinal number of an output column (SELECT list
item), or it can be an arbitrary expression formed from input-column
values.
not an alias of list item, so use:
order by lower(rq.status)
or
order by 2

sql max(val1,val2)? [duplicate]

This question already has answers here:
How to get min/max of two integers in Postgres/SQL?
(2 answers)
Closed 6 years ago.
is there some way to determine the max value out of two values in SQL?
I use the mod function:
MOD(cnt, cnt/100)
This yields a division by 0 error when cnt is smaller than 100. I therefore would like something like this:
MOD(cnt, MAX(cnt/100, 1))
You can use greatest
SELECT greatest(a, b, c) FROM your_table;