[英]How to get all columns in SQL after using DISTINCT and UNION functions?
I am trying to write a Query that will combine historical appointment data with live-updating appointment data.我正在尝试编写一个查询,将历史约会数据与实时更新约会数据相结合。
The Live Updating Data and Historical Data have all common column headers and data types.实时更新数据和历史数据具有所有常见的列标题和数据类型。
The Historical Data set is a static snapshot of 100k-150k rows of data which I am trying to UNION with the Live-Updating Data to create a Full Data Set历史数据集是 100k-150k 行数据的静态快照,我试图将其与实时更新数据联合以创建完整数据集
Since there is some overlap between the Live-Updating Data and the Historical Data, I want to filter out Distinct appointment ID's由于实时更新数据和历史数据之间存在一些重叠,我想过滤掉不同的约会 ID
Here is the query that I've written:这是我写的查询:
SELECT
DISTINCT(n.appointment_id)
FROM (
SELECT
* FROM note_data
UNION
SELECT * FROM note_data_historical) as n
FULL OUTER JOIN note_data_historical as historical
on historical.appointment_id = n.appointment_id
FULL OUTER JOIN note_data as live
on live.appointment_id = n.appointment_id
What I am trying to do is to avoid having to write out the couple of dozen column headers, but also not have duplicate rows.我想要做的是避免写出几十个列标题,但也没有重复的行。
So to summarize, I would like to:总而言之,我想:
It sounds like you want something like the following听起来您想要以下内容
SELECT *
FROM note_data
UNION ALL
SELECT *
FROM note_data_historical
WHERE note_data_historical.appointment_id NOT IN
(
SELECT appointment_id FROM note_data
)
This gets all of your note_data and note_data_historical rows, unless the note_data_historical.appointment_id exists in note_data.这将获取您所有的 note_data 和 note_data_historical 行,除非 note_data 中存在 note_data_historical.appointment_id。 And you don't need to list the columns in your query.而且您不需要在查询中列出列。
Note that I used a UNION ALL
instead of a UNION
, but since I don't know your data, I don't know if that's actually reasonable.请注意,我使用了UNION ALL
而不是UNION
,但由于我不知道您的数据,我不知道这是否真的合理。
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.