在sql中加入多个select语句的结果

时间:2018-05-28 19:38:21

标签: sql sql-server select join intersect

我有四个select语句,我想将它们全部加入以获取公共行。

在一个例子中,我提供了2个选择语句:

SELECT 
    h.userid, 'Activity' as table_name, 
    h.stamp, 
    DATEDIFF(dd, kh.LatestDate, GETDATE()) as days_since, 
    m.group_name
FROM 
    ([Animal].[SYSADM].[activity_history] h
INNER JOIN 
    (SELECT userid, MAX(stamp) as LatestDate
     FROM [Animal].[SYSADM].[activity_history]
     GROUP BY userid) kh ON h.userid = kh.userid AND h.stamp = kh.LatestDate)  
LEFT OUTER JOIN 
    [Animal].[SYSADM].secure_member m ON m.user_name = h.userid
WHERE 
    (DATEDIFF(dd, kh.LatestDate, GETDATE()) > 90)
    AND NOT (m.group_name = 'inactive')
ORDER BY 
    userid

SELECT 
    h.userid, 'Person' as table_name, h.stamp, 
    DATEDIFF(dd, kh.LatestDate, GETDATE()) as days_since, 
    m.group_name
FROM 
    ([Animal].[SYSADM].[person_history] h
INNER JOIN 
    (SELECT userid, max(stamp) as LatestDate
     FROM [Animal].[SYSADM].[person_history]
     GROUP BY userid) kh ON h.userid = kh.userid AND h.stamp = kh.LatestDate)  
LEFT OUTER JOIN 
    [Animal].[SYSADM].secure_member m ON m.user_name = h.userid
WHERE 
    (DATEDIFF(dd, kh.LatestDate, GETDATE()) > 90)
    AND NOT (m.group_name = 'inactive')
ORDER BY 
    userid

我已经尝试了INTERSECT,但它没有返回任何行,我想看到两个select语句中的常见行(实际上我有4个所以我相信适用于2的行将适用于4)

提前致谢。

更新

我在2个select语句中尝试了内连接,它给了我想要的结果,但现在的问题是如何在4个select语句中使用内连接。

SELECT DISTINCT t1.userid as A_UserID, t2.userid as P_UserID, t1.stamp as A_stamp, t2.stamp as P_stamp, datediff(dd,t1.stamp,GetDate()) as A_days_since, datediff(dd,t2.stamp,GetDate()) as P_days_since, t1.group_name, t1.table_name, t2.table_name
from 
    (SELECT h.userid, 'Activity' as table_name, h.stamp, datediff(dd,kh.LatestDate,GetDate()) as days_since, m.group_name
  FROM 
 ( [Animal].[SYSADM].[activity_history] h
inner join (
    select userid, max(stamp) as LatestDate
  from [Animal].[SYSADM].[activity_history]
  group by userid
  ) kh on h.userid = kh.userid and h.stamp = kh.LatestDate
  )  
left outer join [Animal].[SYSADM].secure_member m on m.user_name = h.userid
where 
(datediff(dd,kh.LatestDate, GetDate()) > 90)
and not (m.group_name = 'inactive')) t1

inner join

    (SELECT h.userid, 'Person' as table_name, h.stamp, datediff(dd,kh.LatestDate,GetDate()) as days_since, m.group_name
  FROM 
 ( [Animal].[SYSADM].[person_history] h
inner join (
    select userid, max(stamp) as LatestDate
  from [Animal].[SYSADM].[person_history]
  group by userid
  ) kh on h.userid = kh.userid and h.stamp = kh.LatestDate
  )  
left outer join [Animal].[SYSADM].secure_member m on m.user_name = h.userid
where 
(datediff(dd,kh.LatestDate, GetDate()) > 90)
and not (m.group_name = 'inactive')) t2
on
    t1.userid = t2.userid
    order by T1.userid

Query Result

1 个答案:

答案 0 :(得分:1)

暂时忘掉UNION。想象一下,您将结果插入Table1

然后依赖你的意思"常见的行"。如果你想要精确的值,但在不同的表中

  SELECT userid, h.stamp, days_since, m.group_name
  FROM Table1
  GROUP BY userid, h.stamp, days_since, m.group_name
  HAVING COUNT( table_name ) = 2 -- in this case 2 because are two types 
                                 -- Activity and Persons

查看查询结果后,您还需要为DISTINCT上的每个查询添加UNION