How to select distinct records based on condition - sql

I have table of duplicate records like
Now I want only one record from duplicate records which has latest created date as How can I do it ?

use row_number():
select EnquiryId, Name, . . .
from (select t.*,
row_number() over (partition by enquiryID order by CreatedDate desc) as seqnum
from table t
) t
where seqnum = 1;

Use ROW_NUMBER function to tag the duplicate records ordered by CreatedDate, like this:
;with CTE AS (
select *, row_NUMBER() over(
partition by EnquiryID -- add columns on which you want to identify duplicates
ORDER BY CreatedDate DESC) as rn
FROM TABLE
)
select * from CTE
where rn = 1

Related

Select an id from a group sql which returns duplicate records?

I have to select CompanyId column only from the following SQL;
select CompanyId,
row_number() over (partition by [GradeName] order by [TankNumber] ) rn
from [Data_DB].[dbo].[Company] where CompanyCode='ASAAA'
In the SQL, I try to figure out duplicate records, and from another table i want to delete some records based on the CompanyId from above query.
that is;
delete from [[dbo].ObservationData
where CompanyId in (select CompanyId,
row_number() over (partition by [GradeName] order by [TankNumber] ) rn
from [Data_DB].[dbo].[Company] where CompanyCode='ASAAA')
How can I modify above query?
Assuming you don't care which duplicate gets retained or deleted, you may try using a deletable CTE here:
WITH cte AS (
SELECT *,
ROW_NUMBER() OVER (PARTITION BY [GradeName] ORDER BY [TankNumber]) rn
FROM [Data_DB].[dbo].[Company]
WHERE CompanyCode = 'ASAAA'
)
DELETE
FROM cte
WHERE rn > 1;
This answer arbitrarily retains the "first" duplicate, with first being defined as the record with the earliest row number.
delete from [[dbo].ObservationData
where CompanyId in (select CompanyId from (select CompanyId,
row_number() over (partition by [GradeName] order by [TankNumber] ) rn
from [Datat_DB].[dbo].[Company] where CompanyCode='ASAAA') a where rn > 1 ;

Get latest record from a table based on 2 columns in hive

I want to get the latest record from my source table based on num and id columns and insert in my target table.
Scenario is explained in the attached screen shot. For latest record date column can be used.
Screenshot
Thanks.
Select num,id, date
FROM
(
Select *, ROW_NUMBER() OVER(partition by num,id Order by date desc) as rnk
FROM source_table
)a
WHERE rnk = 1;
by using corelated Subquery
select * from your_table t
where t.date= (
select max(date) from your_table t1
where t1.num=t.num and t1.id=t.id
)
You can do it using max() function
select num,id,max(date) from your_table t
group by num,id
SELECT NUM,ID,DATE FROM TABLE_TEMP
QUALIFY RANK OVER(PARTITION BY NUM,ID ORDER BY DATE DESC)=1;
You can do this using single line query
SELECT NUM,ID,DATE FROM TABLE_TEMP
QUALIFY RANK OVER(PARTITION BY NUM,ID ORDER BY DATE DESC)=1;

Select the most recent row where 2 columns contain the same value

I have a table of street codes and county codes. I need to only select the most recent row (ordered by created date) of any rows where these 2 columns are the same.
Ex.
Here only the last row should be selected, since it has the newest created date, where the Kommunekode and Vejkode are the same.
How can I filter my select statement to allow this logic? I tried using the distinct keyword, but that does not take the Created date into account.
My current code for the view:
SELECT
Infohub_RowId,
Infohub_CreatedDate,
Id,
Sekvensnummer,
Tidspunkt,
Operation,
Kommunekode,
Vejkode,
Oprettet,
Aendret,
Navn,
Vejnavn,
Navngivenvej_id,
Aendret AS Infohub_ValidityDate
FROM (
SELECT
Infohub_RowId,
Infohub_CreatedDate,
Sekvensnummer,
Tidspunkt,
Operation,
Id,
Kommunekode,
Vejkode,
Oprettet,
Aendret,
Navn,
Vejnavn,
Navngivenvej_id,
ROW_NUMBER() OVER(PARTITION BY Id ORDER BY Aendret DESC) AS RowNum
FROM
Dawa.tDelta_Vejstykke) AS x
WHERE x.RowNum = 1
The view should "clean up" the data, by selecting the newest duplicate records.
use Infohub_CreatedDate in order by and Kommunekode,Vejkode these two column in partition by
SELECT
Infohub_RowId,
Infohub_CreatedDate,
Id,
Sekvensnummer,
Tidspunkt,
Operation,
Kommunekode,
Vejkode,
Oprettet,
Aendret,
Navn,
Vejnavn,
Navngivenvej_id,
Aendret AS Infohub_ValidityDate
FROM (
SELECT
Infohub_RowId,
Infohub_CreatedDate,
Sekvensnummer,
Tidspunkt,
Operation,
Id,
Kommunekode,
Vejkode,
Oprettet,
Aendret,
Navn,
Vejnavn,
Navngivenvej_id,
ROW_NUMBER() OVER(PARTITION BY Kommunekode,
Vejkode ORDER BY Infohub_CreatedDate DESC) AS RowNum
FROM
Dawa.tDelta_Vejstykke) AS x
WHERE x.RowNum = 1
You want row_number() but Kommunekode, Vejkode should be in partition clause :
SELECT t.*
FROM (SELECT t.*,
ROW_NUMBER() OVER (PARTITION BY Kommunekode, Vejkode ORDER BY Infohub_CreatedDate DESC) AS Seq
FROM Dawa.tDelta_Vejstykke t
) t
WHERE Seq = 1;

Opposite of TOP in SQL Server

I need to retrieve the last few entries from a table. I can retrieve them using:
SELECT TOP n *
FROM table
ORDER BY id DESC
That I looked everywhere and that's the only answer I could find, But that way I get them in reverse order. I need them in the same order as they are in the table because it's for a messaging interface.
Use a derived table:
select id, ...
from
(
select top n id, ...
from t
order by id desc
) dt
order by id
I suggest you to use a ROW_NUMBER() like this:
SELECT *
FROM (
SELECT
*, ROW_NUMBER() OVER (ORDER BY id DESC) AS RowNo
FROM
yourTable
) AS t
WHERE
(RowNO < #n)
ORDER BY
id

How to get the row that holds the last value in a queue of identical values? (SQL)

I think it's easier to show you an image:
So, for each fld_call_id, go to the next value, if it's identical. When we get to the last value, I need the value in column fld_menu_id.
Or, to put it in another way, eliminate fld_call_id duplicates and save only the last one.
You can use ROW_NUMBER:
WITH CTE AS(
SELECT RN = ROW_NUMBER() OVER (PARTITION BY fld_call_id ORDER BY fld_id DESC),
fld_menu_id
FROM dbo.TableName
)
SELECT fld_menu_id FROM CTE WHERE RN = 1
You can create a Rank column and only select that row, something along the lines of the following:
;WITH cte AS
(
SELECT
*
,RANK() OVER (PARTITION BY fld_call_id ORDER BY fld_id DESC) Rnk
FROM YourTable
)
SELECT
*
FROM cte
WHERE Rnk=1
So you GROUP BY fld_call_id and ORDER BY fld_id in descending order so that the last value comes first. These are the rows where Rnk=1.
Edit after comments of OP.
SELECT Table.*
FROM Table
INNER JOIN
(
SELECT MAX(fldMenuID) AS fldMenuID,
fldCallID
FROM Table
GROUP BY fldCallID
) maxValues
ON (maxValues.fldMenuID = Table.fldMenuID
AND maxValues.fldCallID= Table.fldCallID)
Hope This works
SELECT A.*
FROM table A
JOIN (SELECT fld_id,
ROW_NUMBER() OVER (PARTITION BY Fld_call_id ORDER BY fld_id DESC) [Row]
FROM table) LU ON A.fld_id = LU.fld_id
WHERE LU.[Row] = 1