[英]How to delete all duplicates in one column and return only those that has date in next column in SQL?
I have a table with 4+ million rows and 2 columns: 1. phone number ( account ) and last active time ( last_active ).我有一个包含 4+ 百万行和 2 列的表: 1. 电话号码 ( account ) 和上次活动时间 ( last_active )。 However, not all accounts has last_active date and have null instead.
但是,并非所有帐户都有 last_active 日期,而是null 。 So, what I need is:
所以,我需要的是:
Firstly, delete all duplicates from account column and return the rest (I need only distinct phone numbers).首先,从帐户列中删除所有重复项并返回 rest(我只需要不同的电话号码)。
Secondly , while returning all distinct phone numbers (account), return only those which has a date (not null).其次,在返回所有不同的电话号码(帐户)时,仅返回具有日期(非空)的电话号码。
Seems to be pretty straight forward select query with aggregation.似乎是非常直接的 select 查询与聚合。 What have you tried and what error or problem are you getting
你试过什么,你遇到了什么错误或问题
SELECT Account, max(last_active) as Last_active
FROM TableName
WHERE last_active is not null
GROUP BY Account
maybe you don't want the most recent active however...也许你不想要最近的活跃......
so maybe: but this would show duplicate accoutns with all "Last_active" dates recorded...所以也许:但这会显示重复的帐户,其中记录了所有“Last_active”日期......
SELECT Account, last_active
FROM TableName
WHERE last_active is not null
GROUP BY Account
Hi I've just made a sample table in MySQL with about 200,000 records and half of them are duplicates.您好,我刚刚在 MySQL 中制作了一个示例表,其中包含大约 200,000 条记录,其中一半是重复的。
The following query works quite well.以下查询效果很好。
SELECT * FROM TableName
WHERE last_active IS NOT NULL
AND 1 GROUP BY account
now if you want to save it a new table you could现在如果你想把它保存到一个新表中,你可以
CREATE TABLE newTableName as
SELECT * FROM TableName
WHERE last_active IS NOT NULL
AND 1 GROUP BY account
I would agree with xQbert however I would add in a DISTINCT after the SELECT just in case there are times that account was active twice in the same date (If this is how the column is formatted) and both are recorded我会同意 xQbert 但是我会在 SELECT 之后添加一个 DISTINCT 以防万一帐户在同一日期激活两次(如果这是列的格式)并且两者都被记录
SELECT DISTINCT Account, max(last_active) as Last_active
FROM TableName
WHERE last_active is not null
GROUP BY Account
Consider below simple approach考虑下面的简单方法
select * from your_table
qualify count(*) over account = 1
and countif(last_active is null) over account = 0
window account as (partition by account)
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.