1

再会,

我有一个包含用户活动信息的表格。

该表基本上如下:

Eventdate,     UserId,       Activity
2013-09-09     SusanM        Support Call
2013-09-09     BrandonP      Meeting
2013-09-09     MumbaM        Administration
2013-09-16     SusanM        Support Call
2013-09-16     BrandonP      Meeting

所以你可以看到每个人都更新了 9 号的工作,但用户 MumbaM 还没有记录他 16 号的条目。如果我选择第 16 届 MumbaM 的日期,是否可以在报告中显示,即使他在数据库中没有条目?有没有办法根据以前的用户 ID 条目来纠正 sql 查询,以包括 16 日的 Mumbam。

所以我希望 16 日的报告显示如下:

2013-09-16     SusanM        Support Call
2013-09-16     BrandonP      Meeting
2013-09-16     MumbaM        no update yet
4

4 回答 4

1

你可以试试这个:

SELECT t1.Eventdate, t1.UserId, t1.Activity
FROM YourTable t1
WHERE t1.Eventdate = '2013-09-16'

UNION

SELECT t1.Eventdate, t1.UserId, 'No update yet' as Activity
FROM YourTable t1
WHERE t1.userid not in (SELECT UserId FROM YourTable WHERE Eventdate = '2013-09-16')
    AND t1.Eventdate = 
        (SELECT TOP 1 t3.Eventdate FROM YourTable as t3 WHERE t3.UserID = t1.UserId)
GROUP BY t1.UserID, t1.Eventdate
ORDER BY Eventdate DESC
于 2013-09-26T09:46:41.920 回答
0

我认为你可以尝试这样的事情,但不是“还没有更新”,你会得到 NULL;

SELECT t1.Eventdate, t1.UserId, t2.Activity
From Table t1
Left Outer Join Table t2
on t1.UserId = t2.UserId
Where Eventdate = '2013/09/16'
于 2013-09-26T09:27:56.653 回答
0

您可以使用 decode 或 case,而不是加入:

SELECT TRUNC (SYSDATE),
       userid,
       CASE TRUNC (eventdate)
          WHEN TRUNC (SYSDATE) THEN Activity
          ELSE 'no update yet'
       END
  FROM TABLE
于 2013-09-26T09:45:48.873 回答
0

假设您有两个表“活动”和“用户”,如下所示:

用户

Id,   UserId
1     SusanM
2     BrandonP
3     MumbaM
4     SusanM
5     BrandonP

活动

Eventdate,     UserId,       Activity
2013-09-09     SusanM        Support Call
2013-09-09     BrandonP      Meeting
2013-09-09     MumbaM        Administration
2013-09-16     SusanM        Support Call
2013-09-16     BrandonP      Meeting

以下查询将为您提供所需的结果:

select 
    u.UserId, NVL(a.Activity, 'no update yet') as Activity 
from 
    user u 
left outer join 
    activity a 
on 
    a.UserId=u.UserId 
where 
    a.Eventdate=sysdate
于 2013-09-26T10:15:18.973 回答