This question already has answers here:
Get top 1 row of each group
(19 answers)
Closed last month.
I have duplicates like these in a table:
I can select the duplicates, but I need to select only the ones marked in yellow, aka: the oldest of the duplicates. How could I filter those?
You can use ROW_NUMBER() window function to identify the oldest record, and then just select that:
SELECT *
FROM
(
SELECT yt.*, ROW_NUMBER() OVER (PARTITION BY REFERENCE ORDER BY CREATION DATE ASC) as rn
FROM yt
)
WHERE rn = 1;
Related
This question already has answers here:
Get top 1 row of each group
(19 answers)
Closed 10 months ago.
I have such a situation. I need to make a code work so that it selects only one PVISBN (Item Number) based on PVLICP (license plate) (I need to get only the first row from 2 that I am getting back).
TableSeven AS (
SELECT PVISBN, PVWHS, PVLICP, PVRZNE, PVRLOC, PVAZNE, PVALOC, PVLPRG,
ROW_NUMBER() OVER (PARTITION BY PVISBN, PVRZNE ORDER BY PVLICP --, PVRLOC, PVAZNE, PVALOC, PVLPRG
ASC) AS rn
FROM [REPIT].[LEVYDTA].[WHSPDVT]
WHERE PVSPDT BETWEEN #Last2WeekDATE AND #LWDate
--AND PVISBN='0164556221'
) ,
TableTwelve AS (
SELECT PVISBN, PVWHS, PVLICP, PVRZNE, PVRLOC, PVAZNE, PVALOC, PVLPRG, rn
FROM TableSeven
WHERE rn = 1
),
I keep getting 2 rows and should get only the 1st one
If someone may have an idea, I will appreciate.
Assuming "Oldest" and "Lowest" is the same, you can use aggregation:
SELECT PVISBN, MIN(PVLICP) as PVLICP
FROM yourtable
GROUP BY PVISBN;
To your comment, if you are wanting all columns in the output, then consider window functions (also called Analytics Functions or Ordered Analytics Functions) to help identify the min() for a group/window (PVISBN in this case) and then filter:
SELECT *
FROM
(
SELECT yourtable.*, ROW_NUMBER() OVER (PARTITION BY PVISBN ORDER BY PVLICP ASC) as rn
FROM yourtable
) dt
WHERE rn = 1;
That will generate a row_number, starting at 1 for each row for each distinct PVISBN (you can run just the subquery to see how that looks). Then we just filter for rn of 1, which will be the record for that PVISBN with the lowest PVLICP.
I think this would be helpful for you
SELECT PVISBN, PVWHS, MIN(PVLICP) as PVLICP , ------------ FROM tablename WHERE PVLICP IS NOT NULL or PVLICP <> '' group by PVISBN;
This question already has answers here:
Select first row in each GROUP BY group?
(20 answers)
Get top 1 row of each group
(19 answers)
Closed 2 years ago.
ID,FID,DATE_ADDED
75,67,2020-07-29 11:19:37.5230000
76,67 ,2020-07-29 11:31:51.1870000
77,23,2020- 07-29 11:15:44.2230000
I have this record set however I would like to return a record set of the most recent entries for the FID
76,67 ,2020-07-29 11:31:51.1870000
77,23,2020- 07-29 11:15:44.2230000
Use row_number()
select * from
(
select *,row_number() over(partition by fid order by date_added desc) as rn
from tablename
)A where rn=1
A correlated subquery is a simple and efficient method:
select t.*
from t
where t.date_added = (select max(t2.date_added) from t t2 where t2.fid = t.fid);
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 top results for each group (in Oracle)
(5 answers)
Closed 3 years ago.
I have a following set of result which i got by running
SELECT
REV_USAGE_DATA.DDATE,
REV_USAGE_DATA.SEGMENT,
COUNT(*) AS Freq
FROM CADA_PERMSISDN_DASH REV_USAGE_DATA
GROUP BY
REV_USAGE_DATA.DDATE,
REV_USAGE_DATA.SEGMENT
ORDER BY
REV_USAGE_DATA.DDATE
For each date I want to get the whole row that has the highest value in the FREQ column. How can I achieve this?
You can use CTEs
WITH CTE0 AS
(
SELECT
REV_USAGE_DATA.DDATE,
REV_USAGE_DATA.SEGMENT,
COUNT(*) AS Freq
FROM CADA_PERMSISDN_DASH REV_USAGE_DATA
GROUP BY
REV_USAGE_DATA.DDATE,
REV_USAGE_DATA.SEGMENT
)
SELECT
DDATE,
SEGMENT,
FREQ
FROM CTE0
WHERE (DDATE, SEGMENT, FREQ) IN (
SELECT DDATE, MAX(SEGMENT), MAX(FREQ)
FROM CTE0
GROUP BY DDATE
)
Use ROW_NUMBER():
SELECT DDATE, SEGMENT, Freq
FROM (SELECT REV_USAGE_DATA.DDATE, REV_USAGE_DATA.SEGMENT,
COUNT(*) AS Freq,
ROW_NUMBER() OVER (PARTITION BY REV_USAGE_DATA.DDATE ORDER BY COUNT(*) DESC) as seqnum
FROM CADA_PERMSISDN_DASH REV_USAGE_DATA
GROUP BY REV_USAGE_DATA.DDATE,REV_USAGE_DATA.SEGMENT
) rud
WHERE seqnum = 1
ORDER BY REV_USAGE_DATA.DDATE;
If you have ties on a date and you want all the highest values, then use RANK() instead.
Plenty of similar solutions! Here's mine.
Use a CTE to calculate which record has the highest frequency (similar to row-number) then select them
WITH data AS
(
SELECT
REV_USAGE_DATA.DDATE,
REV_USAGE_DATA.SEGMENT,
COUNT(*) AS Freq
FROM CADA_PERMSISDN_DASH REV_USAGE_DATA
GROUP BY
REV_USAGE_DATA.DDATE,
REV_USAGE_DATA.SEGMENT
),
maxRecords AS
(
SELECT DDATE, SEGMENT, FREQ,
CASE WHEN FREQ = MAX(FREQ) OVER(PARTITION BY DDATE) THEN 1 ELSE 0 END HighestFreq
FROM data
)
SELECT DDATE, SEGMENT, FREQ
FROM maxRecords
WHERE HighestFreq = 1
ORDER BY DDATE;
This question already has answers here:
How to exclude a column from SELECT query?
(6 answers)
Closed 9 years ago.
SELECT * FROM (
SELECT ROW_NUMBER() OVER (ORDER BY ProductID) AS rn, p.*
FROM(
SELECT * FROM products
) p) p2
WHERE rn BETWEEN 0 AND 10
ORDER BY rn
I don't want the above query to return the rn column.
As of your sample query you are selecting top 10 records ordered by ProductID. If it is true then the same results can be achieved by the following:
select * from
(select * from products order by ProductID)
where rownum <= 10
If it is not the results you are expecting then please correct your query (maybe add "PARTITION BY").
This question already has answers here:
Closed 10 years ago.
Possible Duplicate:
Oracle SQL - How to Retrieve highest 5 values of a column
i have a table abc, where i have following columns
act_id,cust_id,lastUpdatedDate,custActivity. Where act_id is primary key .
lastUpdatedDate store last activity done for this customer.
i am trying to get latest 10 rows for given custid based on lastUpdatedDate.
How can i achieve it.
-vivek
You can use ROWNUM in Oracle.. Click Here for Documentation
select *
from
( select *
from your_table
where cust_id=<given cust_id>
order by lastUpdatedDate desc )
where ROWNUM <= 10;
Oracle supports ROW_NUMBER() and window function. Try the following,
SELECT act_id, cust_id, lastUpdatedDate, custActivity
FROM
(
SELECT act_id, cust_id, lastUpdatedDate, custActivity,
ROW_NUMBER() OVER (PARTITION BY cust_id ORDER BY lastUpdatedDate DESC) rn
FROM tableNAME
) x
WHERE rn <= 10
Hope this may helpful to you-
select act_id,cust_id,lastUpdatedDate,custActivity
from (
select act_id,cust_id,lastUpdatedDate,custActivity, row_number() over (order by lastUpdatedDate) r
from abc
where act_id=<cust_id>
)
where r between 1 and 10;