SQL remove duplicate row - sql

I have next query
select No,
Item,
Quantity,
Status
from myTable
when execute query I get
I want to distinct No but with last import quantity, something like this:
Only to get last data from this table group by No

WITH CTE (No, DuplicateCount)
AS
(
SELECT No,
ROW_NUMBER() OVER(PARTITION BY No
ORDER BY No) AS DuplicateCount
FROM myTable
)
Delete from CTE WHERE DuplicateCount >1

If I understood correctly the question
Try this
SELECT * FROM myTable GROUP BY No

If you are using MS SQL Server
Try this
;WITH CTE AS
(
select ROW_NUMBER() over (partition by [No] order by [No] desc) as rn,*
from myTable
)
select No,
Item,
Quantity,
Status
from CTE where rn=1;

Related

delete duplicate sqlite duplicates using temp table

As you can see below, I'm able to select all the row_numbers that are duplicates. I identified them using a window function ROW_NUMBER()
Although I want to delete them from the database.
How can I change my code to remove the duplicates identified, as I'm currently getting an error
WITH RowNumCTE AS (
SELECT *,
ROW_NUMBER() OVER (
PARTITION BY ParcelID,
PropertyAddress,
SalePrice,
SaleDate,
LegalReference
ORDER BY
UniqueID
) row_num
FROM housing_data
)
SELECT *
FROM RowNumCTE
WHERE row_num > 1
Duplicates are identified as having a row_number greater than 1.
Thanks
I found the solution. I used
DELETE FROM housing_data
WHERE ROWID NOT IN (
SELECT MIN(ROWID)
FROM housing_data
GROUP BY ParcelID, PropertyAddress, SalePrice, SaleDate, LegalReference
);

Get the last time a value has changed in Google BigQuery

I have an employee database which contains records about employees. The fields are :
employee_identifier
employee_salary
date_of_the_record
I would like to get, for each record, the date of the last change in employee_salary. Which SQL query could work ?
I have tried with multiple sub-queries, but it does not work.
Below is for BigQuery Standard SQL
#standardSQL
SELECT * EXCEPT(arr),
(SELECT MAX(date_of_the_record) FROM UNNEST(arr)
WHERE employee_salary != t.employee_salary
) AS last_change_in_employee_salary
FROM (
SELECT *, ARRAY_AGG(STRUCT(employee_salary, date_of_the_record)) OVER(win) arr
FROM `project.dataset.employee_database`
WINDOW win AS (PARTITION BY employee_identifier ORDER BY date_of_the_record)
) t
use row_number()
with cte as
(
select *,
row_number()over(partition by employee_identifier order by date_of_the_record desc) rn from table_name
) select * from cte where rn=1
You can also do this without a subquery. If you want all the columns:
SELECT as value ARRAY_AGG(t ORDER BY date_of_the_record DESC LIMIT 1)[ordinal(1)]
FROM t t
GROUP BY employee_identifier;
If you just want the date, use GROUP BY:
SELECT employee_identifier, MAX(date_of_the_record)
FROM t t
GROUP BY employee_identifier;

MSSQL How Can I Get the latest Amount

How can I get the Latest amount, I already had some queries but instead it shows two records ,Im expecting to show only the the '7370' current amount
you can use correlated subquery
select * from tablename a where lasttime in (select max(lasttime) from tablename b where a.id=b.id)
OR you can use row_number()
select * from
(
select *,row_number() over(partition by id order by lasttime desc) as rn from tablename
)A where rn=1
Just add Top 1 before your fields.
Select TOP 1 fields from table
SELECT TOP 1 currentBalance FROM DBO.tbl_billing ORDER BY [date]

Distinct table by 2 columns and return all rows

I want to get unique fields using distinct on 2 columns (ID and __EventType), however, I have been unsuccessful. Here is my attempt that failed but conceptualizes what I'm trying to achieve:
SELECT * FROM (select distinct ID, __EventType from SP_HS_Firm_Calendar_All);
For example here is All my data:
Select ID, __DisplayName, __EventType,EventDate,EndDate from SP_HS_Firm_Calendar_All
Returns:
And here is the data I wish to return using distinct query above:
Thank you very much for all your help it is much appreciated!
You can use row_number() in most databaes:
select t.*
from (select t.*, row_number() over (partition by id, _eventtype order by id) as seqnum
from t
) t
where seqnum = 1;
You can use GROUP BY
SELECT *
FROM SP_HS_Firm_Calendar_All
GROUP BY ID, __EventType;
Please, try this one:
Select DISTINCT (ID, __DisplayName) from SP_HS_Firm_Calendar_All
Have you tried just select distinct ID, __EventType from SP_HS_Firm_Calendar_All?
That alone theoretically should get you all rows with distinct combinations of ID AND __EventType.
Here is the final solution, thank you very much!
select ID, __DisplayName, __EventType,EventDate,EndDate from (select t.*, row_number() over (partition by id, __EventType order by id) as seqnum from SP_HS_Firm_Calendar_All t) t where seqnum =1

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