简体   繁体   English

对组织单位为datevalue <…且中间有数据透视表的所有系统用户的SQL查询

[英]SQL Query for all systemusers who's organisationunits has a datevalue < … with pivot table in between

I have 3 tables, 我有3张桌子

  • SystemUsers
  • OrganisationalUnits
  • UserOrganisationUnits - Pivot with UserId/OrganisationUnitId foreign keys UserOrganisationUnits带有UserId / OrganisationUnitId外键的数据透视

All tables have an Active bit column and the OrganisationalUnit table has a CoveredTill datetime column. 所有表都有一个“ Active位”列,而OrganisationalUnit表有一个CoveredTill datetime列。

I want a list of all UserOrganisationUnits where OrganisationalUnit "CoveredTill" is < x (value doesn't matter as will be set in clr) date and all tables records are active. 我想要OrganisationalUnit “ CoveredTill” <x(值与在clr中设置无关紧要)日期且所有表记录均处于活动状态的所有UserOrganisationUnits的列表。

So far I've only managed to get unique users from my query so any advice would be much appreciated. 到目前为止,我仅从查询中获得了唯一身份用户,因此任何建议将不胜感激。

SELECT     
   SystemUsers.DisplayName, SystemUsers.Email, 
   OrganisationalUnits.Description, OrganisationalUnits.CoveredTill, 
   OrganisationalUnits.Id, OrganisationalUnits.ParentId, OrganisationalUnits.TypeId
FROM         
   SystemUsers 
RIGHT OUTER JOIN
   OrganisationalUnits ON SystemUsers.Id = OrganisationalUnits.Id 
INNER JOIN
   UserOrganisationUnits ON SystemUsers.Id = UserOrganisationUnits.Id
WHERE     
   (OrganisationalUnits.Active = 1) 
   AND (SystemUsers.Active = 1) 
   AND (UserOrganisationUnits.Active = 1) 
   AND (OrganisationalUnits.CoveredTill < '2017-03-31 00:00:00.000')

I randomly set the date as the above since I manually set the covered till date as "2015-03-31 00:00:00.000" on all OrganisationalUnits "CoveredTill" columns. 由于我在所有OrganisationalUnits“ CoveredTill”列上将覆盖的截止日期手动设置为“ 2015-03-31 00:00:00.000”,因此我将日期随机设置为上述日期。

My aim is to have a single sql query string which I can use in my clr stored procedure to create a list of all users who need to be emailed because there organisational unit is no longer covered and for said email to include which organisational unit hence why just having a unique list of users isn't sufficient. 我的目标是拥有一个sql查询字符串,该字符串可在我的clr存储过程中使用,以创建所有需要通过电子邮件发送的用户的列表,因为不再覆盖组织单位,并且所述电子邮件包括哪个组织单位,因此为什么仅拥有唯一的用户列表是不够的。

I already have the email code so it's just the query I'm having issues with. 我已经有电子邮件代码,所以这只是我遇到问题的查询。

Edit:- 编辑:-

I had first approached this as needing 3 separate queries which I guess after a bit more testing I'll have to revisit unless someone can spot what I'm doing wrong with the above query? 我最初需要3个单独的查询,但经过一些测试,我猜想我将不得不重新访问它,除非有人能发现我在上面的查询中做错了什么?

Edit 2:- 编辑2:-

After working through the issue with sarin and preparing to provide him a sql db/query copy I found that some of the joins were pointing towards the wrong ID fields (automatically created from different panes so always be wary of anything they create) so below I've included the final solution. 解决了sarin的问题并准备向他提供sql db / query副本后,我发现某些联接指向错误的ID字段(从不同的窗格自动创建,因此请始终警惕它们创建的任何内容),因此在下面我已经包含了最终解决方案。 (Which includes a couple extra fields I decided might be useful). (我认为其中包括几个额外的字段可能会有用)。

SELECT  OrganisationalUnits.Description, OrganisationalUnits.CoveredTill, SystemUsers.DisplayName, SystemUsers.Email, 
        UserOrganisationUnits.LastVisited AS UOULastVisited, SystemUsers.LastVisited AS SULastVisited
FROM    UserOrganisationUnits INNER JOIN
        OrganisationalUnits ON UserOrganisationUnits.OrganisationUnitId = OrganisationalUnits.Id INNER JOIN
        SystemUsers ON UserOrganisationUnits.UserId = SystemUsers.Id
WHERE   (OrganisationalUnits.Active = 1) AND (SystemUsers.Active = 1) AND (UserOrganisationUnits.Active = 1) AND 
        (OrganisationalUnits.CoveredTill < '2017-03-31 00:00:00.000')

Performing a Right\\left join before you do an inner join can give you unexpected results as it may produce nulls and your inner join will then attempt to filter them out. 在执行内部联接之前执行“右\\左”联接可能会给您带来意想不到的结果,因为它可能会产生null,然后您的内部联接会尝试将其过滤掉。 I'm not entirely clear about the relationships between the tables but can you turn them all into INNER JOINS to get the same list and produce you a similar result based on your expected outcome? 我对表之间的关系尚不完全清楚,但是您能否将它们全部转换为INNER JOINS以获得相同的列表并根据预期结果为您提供相似的结果?

"My aim is.... to create a list of all users who need to be emailed because there organisational unit is no longer covered" “我的目​​标是...创建所有需要通过电子邮件发送的用户的列表,因为不再覆盖该组织单位”

UPDATED: 更新:

SELECT     
   SystemUsers.DisplayName, SystemUsers.Email, 
   OrganisationalUnits.Description, OrganisationalUnits.CoveredTill, 
   OrganisationalUnits.Id, OrganisationalUnits.ParentId, OrganisationalUnits.TypeId
FROM         
   UserOrganisationUnits
INNER JOIN SystemUsers ON SystemUsers.Id = UserOrganisationUnits.Id
                       AND (SystemUsers.Active = 1) 

INNER JOIN OrganisationalUnits ON OrganisationalUnits.Id = SystemUsers.Id
                               AND OrganisationalUnits.Active = 1
                               AND OrganisationalUnits.CoveredTill < '2017-03-31 00:00:00.000'

WHERE UserOrganisationUnits.Active = 1

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

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