美好的一天,
我有一个包含用户活动信息的表。
表格基本如下: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查询以包含Mumbam为16日。
所以我希望16日的报告显示如下:
2013-09-16 SusanM Support Call
2013-09-16 BrandonP Meeting
2013-09-16 MumbaM no update yet
答案 0 :(得分: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
答案 1 :(得分: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'
答案 2 :(得分:0)
您可以使用解码或案例,而不是加入:
SELECT TRUNC (SYSDATE),
userid,
CASE TRUNC (eventdate)
WHEN TRUNC (SYSDATE) THEN Activity
ELSE 'no update yet'
END
FROM TABLE
答案 3 :(得分:0)
假设您有两个表'Activity'和'User',如下所示:
用户强>
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