左连接或右连接取决于数据

时间:2018-02-05 16:01:36

标签: sql-server sql-server-2008-r2

我处于这样一种情况,我需要加入2个表,但根据数据,我有时需要将表视为主表,而另一表作为连接表,反之亦然。 我举了一个简单的例子来重新创建这种情况,想象我有2个表(临时表),其中包含员工的任务:

# We are running collectd 5.4.0.git # FQDNLookup since 4.3, became true by default with 5.0 FQDNLookup false # Interval (in seconds) at which to query values. This may be overwritten on # a per-plugin basis by using the 'Interval' option of the LoadPlugin block. # This capability was announced with version 5.2 but became functional # only with 5.4.3. Interval 60 # Logging # syslog since 4.0 LoadPlugin syslog <Plugin syslog> LogLevel info </Plugin> # LoadPlugin section # cpu since 1.3 LoadPlugin cpu <Plugin cpu> # ReportByCpu true # ReportByState true # ValuesPercentage starting with collectd 5.5 # ValuesPercentage true # ValuesPercentage false forces measurements in jiffies/second when both # ReportByCpu and ReportByState are true (the defaults); collectd 4 does # not do time derivation (and time derivation cannot be turned off in 5) ValuesPercentage false </Plugin> # df since 3.6 LoadPlugin df <Plugin df> FSType rootfs FSType sysfs FSType proc FSType devtmpfs FSType devpts FSType tmpfs FSType fusectl FSType cgroup IgnoreSelected true # ReportByDevice since 4.8 ReportByDevice true # ValuesAbsolute, ValuesPercentage since 5.4 # ValuesAbsolute for reporting in bytes (true by default) # ValuesAbsolute true ValuesPercentage false </Plugin> # disk since 1.5 LoadPlugin disk # irq since 4.0 LoadPlugin irq # load since 1.0 (not sure about ReportRelative) LoadPlugin load <Plugin load> ReportRelative true </Plugin> # memory since 1.0 # No options for 4.10 LoadPlugin memory <Plugin memory> ValuesAbsolute true ValuesPercentage false </Plugin> # nfs since 3.3 LoadPlugin nfs # processes since 3.2 LoadPlugin processes <Plugin processes> # ProcessMatch since 4.5 ProcessMatch "all" "(.*)" </Plugin> # protocols since 4.7 LoadPlugin protocols # swap since 2.1 # No options for 4.10 LoadPlugin swap <Plugin swap> ReportByDevice true # ReportIO true # ReportBytes is false by default; when false, swap I/O is in pages # 4.10 reports swap I/O in pages (and cannot be configured otherwise) ReportBytes false ValuesAbsolute true ValuesPercentage false # 4.10 reports swap cached/free/used in bytes, swap_io in/out in pages </Plugin> # tcpconns since 4.2 LoadPlugin tcpconns # thermal since 4.5 LoadPlugin thermal # thermal since 4.7 LoadPlugin uptime # Server part LoadPlugin write_graphite <Plugin write_graphite> <Node "node-graphite-1"> Host "192.168.1.170" Port "1111" Protocol "tcp" EscapeCharacter "_" AlwaysAppendDS true SeparateInstances false </Node> </Plugin> 包含处理员工工作的任务

#JOB_TASKS包含处理员工家庭的任务

如果#FAMILY_TASKSID_EMPLOYEE = 1没有记录,而#FAMILY_TASKS有一些记录,则#JOB_TASKS应为主表,#JOB_TASKS为已加入的记录。如果对于#FAMILY_TASKSID_EMPLOYEE = 2有一些记录,但#FAMILY_TASKS没有记录,则#JOB_TASKS应该是已连接的表,而#JOB_TASKS应该是主要的。

不知何故,这些是我的查询:

#FAMILY_TASKS

我希望有一个自动执行正确连接的查询。这可以使用游标完成,但是如何使用select语句执行此操作?

我希望我表达自己。

2 个答案:

答案 0 :(得分:1)

你可以通过FULL OUTER JOIN和一些条件来做到这一点。

在伪代码中,您的条件将执行以下操作:

(ID_EMPLOYEE = 1 AND #JOBTASKS.Column IS NOT NULL)
OR 
(ID_EMPLOYEE = 2 AND #FAMILYTASKS.Column IS NOT NULL)

答案 1 :(得分:1)

Op没有更新他们的帖子,所以这是一个猜测。我认为这可以使用FULL OUTER JOIN来实现,使用以下逻辑:

DECLARE @EmployeeID int = 1;

SELECT * 
FROM #FAMILY_TASKS FT
     FULL OUTER JOIN #JOB_TASKS JT ON JT.ID_EMPLOYEE = JT.ID_EMPLOYEE
WHERE @EmployeeID IN (FT.ID_EMPLOYEE,JT.EmployeeID);