This question already has answers here:
Keep order from 'IN' clause
(6 answers)
Closed 8 years ago.
I had a requirement like
select * from USER where firstname IN
('nexus',
'samsung',
'apple');
On executing this query , i get results based on insertion order of rows in database.But i need the results exactly same way, i given for IN parameter.
for ex : the above query should give results like
nexus
samsung
apple
and not any other order like.
samsung
nexus
apple
What can i use with the above query to get selection results in the order i given?
One method is to use case:
order by (case when firstname = 'nexus' then 1
when firstname = 'samsumg' then 2
when firstname = 'apple' then 3
end)
Another method that has less typing:
order by instr(',nexus,samsung,apple,', ',' || name || ',')
Related
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;
This question already has answers here:
How to concatenate strings of a string field in a PostgreSQL 'group by' query?
(14 answers)
Closed last year.
I have data like this:
Group Provider
A ABC
A DEF
B DEF
B HIJ
And I want to transform the data like this:
Group ProviderList
A ABC, DEF
B DEF, HIJ
I was trying something like this using a concat(select distinct...) but not sure if this is the best approach
SELECT distinct
group,
CONCAT(select distinct provider from data)
FROM data
GROUP BY 1
What Laurenz meant with string_agg() is the following
SELECT
group,
STRING_AGG(Provider,',') as ProviderList
FROM data
GROUP BY 1
Optionally you could also use:
STRING_AGG(provider,',' order by Provider)
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;
This question already has answers here:
Postgresql GROUP_CONCAT equivalent?
(9 answers)
Closed 7 years ago.
This should be simple one. A query like
SELECT code_id FROM code WHERE status = 1
gives normally a result like
code_id
10
11
12
But my goal is to fetch into a string like
10,11,12
In order to use it in an other query
SELECT x FROM table WHERE status in (10,12,13)
Preferable in the same query. Is this possible using "standard" Postgresql WITHOUT adding extra extension?
Everything so far is using extension that not are available as standard.
Thanks in advance.
Whatever tool you are using just shows you the data like that for convenience. But you can also use the resultset in a subquery, like this
SELECT x FROM table WHERE status in (
SELECT code_id FROM code WHERE status = 1
)
You can try this way to get result as comma-separated
But my goal is to fetch into a string like
SELECT string_agg(code_id, ',') FROM code WHERE status = 1
This question already has answers here:
SQL Query to concatenate column values from multiple rows in Oracle
(10 answers)
Closed 8 years ago.
I'm having trouble explaining this, so if someone can make adjustments to the title or question then please do.
I have a simple SQL query that I'm running
SELECT orders.customer_no, orders.order_no FROM orders WHERE orders.creation = '01-JAN-14';
resulting in
customer_no order_no
----------- ----------
0 8051729
2 2809137
2 3794827
3 1934678
3 9237192
6 3462890
6 3131378
6 6267190
6 2864952
6 1325645
but what I want is
customer_no order_no
----------- ----------
0 8051729
2 2809137 3794827
3 1934678 9237192
6 3462890 3131378 6267190 2864952 1325645
Is it possible to do something like this direct within SQL?
Edit: Using Oracle8i Enterprise Edition Release 8.1.7.4.0 - Production.
I believe you want:
select orders.customer_no, listagg(orders.order_no, ' ') within group (order by orders.order_no) orders.order_no
from orders
WHERE orders.creation = '01-JAN-14'
group by orders.customer_no;
In MySQL you would want the GROUP_CONCAT function, which is roughly LISTAGG in Oracle, according to:
Is there any function in oracle similar to group_concat in mysql?
Based on Oracle: Way to aggregate concatenate an ungrouped column in grouped results, you can try something like:
WITH j
AS (SELECT customer_no, order_no
FROM orders
WHERE creation = '01-JAN-14')
SELECT RTRIM (
EXTRACT (SYS_XMLAGG (XMLELEMENT ("X", order_no || ' ')), '/ROWSET/X/text()').getstringval (),
', ')
FROM j
GROUP BY customer_no;