简体   繁体   English

SQL 分组相关列

[英]SQL grouping related columns

Suppose I have a table with 2 columns like this:假设我有一个包含 2 列的表,如下所示:

Person1, Person2
David    Jessica
Jessica  David
David    Oz
Oz       David
Guy      Richard
Richard  Guy
Jessica  Oz
Oz       Jessica

and another table with 2 columns:另一个表有 2 列:

Person   Last Posted
David    12/8/2016
Jessica  5/10/2016
Oz       23/11/2016
Guy      8/3/2016
Richard  27/6/2016

assuming a players name is unique and will appear only once.假设玩家名称是唯一的,并且只会出现一次。 how to choose the person with the latest post?如何选择最新帖子的人? A related group for this example is (David ,Jessica, Oz),(Guy, Richard)此示例的相关组是 (David ,Jessica, Oz),(Guy, Richard)

The output should be:输出应该是:

Person
Oz
Richard

This one should work.这个应该有效。 In the first sub-query, called winner, list the person with the most recent post for each pairing.在第一个子查询中,称为获胜者,列出每个配对中发布最新帖子的人。 Then join this to a second sub-query, called loser, which lists the person with the least recent post per pairing.然后将其加入到第二个子查询中,称为失败者,它列出了每个配对中最近发布的人。 The output should return people who are in the winners sub-query but not the losers one.输出应该返回获胜者子查询中的人,而不是失败者。 Being in the losers sub-query would demonstrate that someone in your group had a more recent post than you:在失败者子查询中将表明您组中的某个人比您发布了更新的帖子:

SELECT DISTINCT winner.person
FROM
(SELECT
 CASE WHEN t2_1.last_post > t2_2.last_post THEN person1 ELSE person2 END AS person
 FROM t1
 INNER JOIN t2 t2_1 ON t1.person1 = t2_1.person
 INNER JOIN t2 t2_2 ON t1.person2 = t2_2.person) winner
LEFT JOIN
(SELECT
 CASE WHEN t2_1.last_post < t2_2.last_post THEN person1 ELSE person2 END AS person
 FROM t1
 INNER JOIN t2 t2_1 ON t1.person1 = t2_1.person
 INNER JOIN t2 t2_2 ON t1.person2 = t2_2.person) loser
ON winner.person = loser.person
WHERE loser.person IS NULL

Tested here: http://sqlfiddle.com/#!9/6ef390/13在这里测试: http ://sqlfiddle.com/#!9/ 6ef390/13

Assuming your first table is table1 and the second is table2, how about this:假设您的第一个表是 table1,第二个是 table2,那么如何:

select 
Person = case when isnull(t1.[last posted],'1900-01-01')>isnull(t2.[last posted],'1900-01-01') then t.person1 else t.person2 end

from 
table1 t
left join table2 t1 on t.person1=t1.person
left join table2 t2 on t.person2=t2.person

(If you know you won't ever have a person who hasn't posted, you could omit the use of isnull and use inner joins ) (如果你知道你永远不会有一个没有发布的人,你可以省略使用isnull并使用inner joins

This question is not well written.这个问题写得不好。 I wrote a solution to the step of selecting distinct users with latest date, but the result comes out different then what you wrote.我为选择具有最新日期的不同用户的步骤编写了一个解决方案,但结果与您所写的不同。 not sure what kind of logic you aim for.不确定你的目标是什么逻辑。

Join the 2nd table (posts table twice) and then use the greatest function:加入第二个表(posts 表两次),然后使用greatest函数:

select distinct case when posts1.last_posted = greatest(posts1.last_posted,posts2.last_posted) then posts1.person else posts2.person end as latest_person from games left join posts as posts1 on games.person1 = posts1.person left join posts as posts2 on games.person2 = posts2.person

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM