SQL将两行合并到一个select语句中 [英] SQL Combine two rows into a select statement
问题描述
您好,
我有两列我正在运行一个独特的select语句。问题是我真的只想要第一列的一个独特的,第二个我想采取非NULL的一个或两个都是NULL然后传递NULL。
我正在使用这个选择语句。
Hello,
I have two columns that I am running a distinct select statement for. The problem is I really want just a distinct for the first column, the second one I would like to take the one that isn't NULL or if both are NULL then pass NULL.
I am using this select statement.
SELECT DISTINCT
offer_id_new
,Date1
FROM [PCGRecruitingDatabaseUpsize].[dbo].[ExtendOffer]
它还给我这个
And it returns me this
offer_id_new Date
819 2009-05-08 00:00:00.000
864 NULL
864 2010-08-08 00:00:00.000
866 NULL
对于两个ID为864的ID,我想每个offer_id_new只拉一个,但这个ID864都有一个NULL日期和约会。如果存在,我想只有一行是日期。
For the two ID's of "864", I would like to pull only one per offer_id_new but this ID "864" has both a NULL date and a date. If this exists I would like to just have 1 row which is the date.
推荐答案
你也可以尝试聚合(分组)
You could also try an aggregate (group by)
SELECT offer_id_new
,max(Date1)
FROM [PCGRecruitingDatabaseUpsize].[dbo].[ExtendOffer]
GROUP BY offer_id_new
您好,
将'左连接'改为一个'内部联接'。
干杯
Hello,
Alter the 'left join' into a 'inner join'.
Cheers
我想通了。插入临时表,其中Date不为null。如果临时表中不存在id,则再次插入。
I figured it out. Insert into a temp table where Date is not null. Then insert again if the id does not exist in the temp table.
这篇关于SQL将两行合并到一个select语句中的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!