简体   繁体   English

左外连接在同一张桌子上

[英]Outer Left Join on the same table

I am using this code to sort any 'name' that appears in the last 70 days with the Master Symptom of 'Problem' I have open and closed columns and so am only interested in any problem that is still open. 我正在使用此代码对过去70天内出现的“问题”主症状中出现的所有“名称”进行排序,我已经打开和关闭了列,因此仅对仍然存在的任何问题感兴趣。

select 
    name, LastEDT 
from 
    vw_UserView_ME_Open
where 
    received between dateadd(day, datediff(day, 0, getdate()) - 70, 0) 
                 and dateadd(day, datediff(day, 0, getdate()), 0)
    and MasterSymptom = 'Problem' 
    and closed is null
order by
    'LastEDT' asc;

What I looking to do is create an outer left join on the same table to reference any additional master symptoms that are logged against the 'name' 我要做的是在同一张表上创建一个外部左联接,以引用针对“名称”记录的所有其他主症状。

I have very limited (read no) experience of joins on the same table and so I am a little lost and I cant figure it out from the posts that I have read here. 我在同一张桌子上进行联接的经历非常有限(不读),因此我有点迷茫,无法从我在这里阅读的帖子中弄清楚。

Can anyone help? 有人可以帮忙吗?

This might get you started? 这可能会让您入门吗? Basically a self-JOIN is just the same as any other JOIN if you are careful with your aliases. 基本上,如果谨慎使用别名,则自我联接与其他联接完全相同。 My example simply looks for any entries with the same name where the Master Symptom = "Problem", then COUNTs these up. 我的示例只是查找名称相同的所有条目,其中Master Symptom =“ Problem”,然后对这些条目进行计数。 You may well want to do something different with the results? 您可能想对结果做些不同的事情?

SELECT 
    o.name, 
    o.LastEDT,
    COUNT(r.*) AS related_problems
FROM 
    vw_UserView_ME_Open o
    LEFT JOIN vw_UserView_ME_Open r ON r.name = o.name AND r.MasterSymptom = 'Problem' 
WHERE 
    o.received BETWEEN CONVERT(DATE, DATEADD(DAY, -70, GETDATE())) AND CONVERT(DATE, GETDATE())
    AND o.MasterSymptom = 'Problem' 
    AND o.closed IS NULL
GROUP BY
    o.name, 
    o.LastEDT
ORDER BY 
    o.LastEDT;

I "tidied" up your SQL a bit at the same time. 我同时“整理”了您的SQL。

Remember that because this is a LEFT JOIN you can't add any criteria for the related table into the WHERE clause, or you are essentially making the LEFT JOIN into an INNER JOIN. 请记住,因为这是LEFT JOIN,所以您不能在WHERE子句中添加相关表的任何条件,或者实际上是在将LEFT JOIN变成INNER JOIN。 So if you also wanted to test that closed IS NULL then you could add: 因此,如果您还想测试关闭的IS NULL,则可以添加:

r.closed IS NULL

to the end of the LEFT JOIN criteria, but not to the WHERE clause. 到LEFT JOIN条件的末尾,而不是WHERE子句。

Actually that's not the best example, as you COULD actually add that extra IS NULL criteria to the WHERE clause without breaking the JOIN. 实际上,这并不是最佳示例,因为您实际上可以在不破坏JOIN的情况下将额外的IS NULL条件添加到WHERE子句中。 However, the results would probably not be as intended, as this would now mean: 但是,结果可能与预期不符,因为这将意味着:

  • get me records where the related entry has a NULL is closed value; 获取相关记录为NULL的封闭值的记录;
  • also get me records where there are no related entries at all (as this will also return a NULL). 还可以得到我根本没有相关条目的记录(因为这也会返回NULL)。

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

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