Update statement with FROM clause [duplicate] - sql

This question already has answers here:
Oracle SQL: Update a table with data from another table
(7 answers)
Update statement with inner join on Oracle
(15 answers)
Oracle Update Query using Join
(3 answers)
Closed 4 years ago.
I have a SELECT statement that pulls records this way.
SELECT /*+PARALLEL(16)*/ CONT_ID,
LOCATION_GROUP_ID,
CONT_METH_TP_CD,
END_DT
FROM (SELECT A.*,
DENSE_RANK() OVER (
partition BY CONT_ID, CONT_METH_TP_CD
ORDER BY LAST_UPDATE_DT DESC) AS RNK
FROM (SELECT LG.CONT_ID,
LG.LOCATION_GROUP_ID,
CG.CONT_METH_TP_CD,
LG.LAST_UPDATE_DT,
LG.END_DT
FROM HUB.LOCATIONGROUP LG,
HUB.CONTACTMETHODGROUP CG,
HUB.CONTACT C
WHERE LG.LOCATION_GROUP_ID = CG.LOCATION_GROUP_ID
AND LG.CONT_ID = C.CONT_ID
AND LG.END_DT IS NULL
AND C.INACTIVATED_DT IS NULL
) A
)
WHERE RNK > 1;
After obtaining these data, I have to go back to set my END_DT as SYSDATE, which will need another query.
Can I set my END_DT directly in this same query once I obtain the results, without going to another query?

Related

Oracle- How to get single row from multiple same type of rows [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)
SQL Selecting dates with maximum sale for each department [duplicate]
(3 answers)
Closed 1 year ago.
I have below table output from a query.
and I want the final output as below image from the above output. Main goal is to figure out one one rows for each ffi_customer_id for each year based on reported_on and created_on date.
Thanks in advance.
This kind of problem can be solved with analytic functions. You can read more here https://docs.oracle.com/cd/E11882_01/server.112/e41084/functions004.htm#SQLRF06174
For example you select the columns which you want in outer select and then in subquery rank over partition by ffi_customer_id, ref_period_value_code order by created_on and reported_on in descending order. Then select the records where rank is 1.
SELECT ffi_customer_id
,ref_period_value_code
,created_on
,reported_on
,financial_master_id
FROM ( SELECT your_table_name.*
,RANK() OVER(PARTITION BY ffi_customer_id, ref_period_value_code ORDER BY reported_on DESC, created_on DESC) AS "Rank"
FROM (SELECT * FROM your_table_name) AS table2(ffi_customer_id, ref_period_value_code, created_on, reported_on, financial_master_id)) table22) t0 WHERE "Rank" = 1;

Select max(date) does not work if the rows have different values, I only want to fetch the row with the highest date [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.
This is the code:
select security,max(dte),close,ask,bid,mid from k$prices
where to_char(dte,'MON-YYYY') = 'JAN-2021'
group by security,close,ask,bid,mid,dte
order by security,dte desc
Below is the result: I only want to get 2 rows, which has the highest date for each security (436 January 5 and 448 January 29) but because the values of the fields are different, all rows are still being shown. Please help me. Thanks
You could rank all your rows first within the inline view t, then select only those that have rank 1 ( rnb = 1)
select security, dte, close, ask, bid, mid
from (
select security, dte, close, ask, bid, mid, row_number()over(partition by security order by dte desc) rnb
from your_table
)t
where rnb = 1
;

Select nth row Oracle SQL [duplicate]

This question already has answers here:
How ROWNUM works in pagination query?
(3 answers)
How do I limit the number of rows returned by an Oracle query after ordering?
(14 answers)
how to select even records from a table in oracle?
(12 answers)
Closed 2 years ago.
I'm attempting to access the 3rd row from the following query:
SELECT *
FROM (SELECT
OFFENSEDESC,
COUNT(*) AS num
FROM
CRIMEPROFILE
GROUP BY
OFFENSEDESC
ORDER BY num DESC) o
WHERE rownum = 3
However, it returns no rows, I am uncertain as to why that is, any help is appreciated. NOTE: ORACLE SQL
Try this
SELCT
OFFENSEDESC,
num
from
(
SELECT
OFFENSEDESC,
COUNT(*) AS num,
ROW_NUMBER() OVER (ORDER BY COUNT(*) DESC) as rnk
FROM
CRIMEPROFILE
GROUP BY
OFFENSEDESC
) val
WHERE rnk = 3

top 10 rows in oracle [duplicate]

This question already has answers here:
Oracle SQL - How to Retrieve highest 5 values of a column [duplicate]
(5 answers)
Oracle SELECT TOP 10 records [duplicate]
(6 answers)
Closed 6 years ago.
i have 2 tables .
abc(CID(pk), cname,)
order(order_id(pk), CID(fk), number_of_rentals)
i want to fetch top 10 customers based on number of rentals.
SELECT cid, sum(no_rentals) as sum
FROM orders
group by cid, no_rentals
order by no_rentals desc;
how can i use rownum function in above query to fetch the desired output
Just wrap your query in:
SELECT * FROM ( your_query ) WHERE ROWNUM <= 10;
However, your query does not look like it is going to do what you intend as the GROUP BY no_renalts will mean that each distinct no_rentals value will be in its own group and you will not sum the values for each customer so you probably don't want to include it in the GROUP BY. Also, if you want to order by the total number of rentals then you want to ORDER BY SUM( no_rentals ) (or by its alias) like this:
SELECT cid,
SUM(no_rentals) as total_no_rentals
FROM orders
GROUP BY cid
ORDER BY total_no_rentals DESC;
Then you can apply the row limit like this:
SELECT *
FROM (
SELECT cid,
SUM(no_rentals) as total_no_rentals
FROM orders
GROUP BY cid
ORDER BY total_no_rentals DESC
)
WHERE ROWNUM <= 10;

How to Select in between row's record from tables in SQL 2008? [duplicate]

This question already has answers here:
Selecting specific row number in sql [duplicate]
(2 answers)
Closed 7 years ago.
I wonder, is there any option like "Skip"(from LINQ) in SQL to select particular rows in a table.
I mean, in a table named "abcd". In that table 300 rows are there. but from that 300 rows i want to select rows from 233 to 300 or 233 to 258.
How to do this?? Please anyone help.
You can use a cte or derived table for this:
With cte as (
Select col1, col2, row_number() over(order by sortColumn) as rn
From table
)
Select *
from cte
Where rn >= 233
And rn <= 258