简体   繁体   English

如何删除一列中的所有重复项并仅返回 SQL 中下一列中具有日期的那些?

[英]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).其次,在返回所有不同的电话号码(帐户)时,仅返回具有日期(非空)的电话号码。

enter image description here在此处输入图像描述

enter image description here在此处输入图像描述

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.

相关问题 如何返回特定列中只有 integer 个值的所有记录? - How to return all records having only integer values in a specific column? 从具有所有列匹配值的 Bigquery 表中删除数据 - delete data from Bigquery table that has all column matching value 如何在 BigQuery 中将所有列除以一列 - How to divide all columns by one column in BigQuery Bigquery 标准 SQL:过滤掉重复项,同时保持一列的顺序 - Bigquery standard SQL: Filter out the duplicates while keeping the sequence of one column 将 varchar 列转换为 SQL 中的日期 - Convert varchar column to date in SQL 如何从日期列中包含 Null 的表在 Google Bigquery 中查询? - How do I query in Google Bigquery from a table that has Null in a date column? 如何通过在 bigquery sql 中进行分组字符串比较来返回同一列中字符串值的差异? - How to return difference in string values from the same column by doing a grouped string comparison in bigquery sql? 如何只为特定的一个分配新的列值 - How assing new column value for only specific one SQL - 将日期列转换为开始和结束日期 - SQL - convert date column to start and end dates 如何使用 SQL 在表中创建一个新列以仅显示来自另一列的特定数据? - How can I create a new column in a table to show only specific data from another column using SQL?
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM