我觉得这应该是一件容易的事,但我正在努力。
我有一个问题表,表中有以下内容:
我通过用户ID总结了这一点,其中包含日期创建的问题数和日期解决的问题数。
例如,
User ID /// Date /// Issues Received /// Issues Resolved
JonSmith1 /// 1/12/2015 /// 28 /// 34
问题源于这样一个事实,即在我计算已解决的问题时,我并不关心何时收到问题。因此,昨天,上周,去年等都可以收到已解决计算中的问题。
非常感谢任何帮助。
谢谢, PE
答案 0 :(得分:0)
对这两个日期执行单独查询,然后将它们与UNION
:
SELECT UserID, Date, MAX(IssuesReceived) AS IssuesReceived, MAX(IssuesResolved) AS IssuesResolved
FROM (
SELECT UserID, CreatedDate AS Date, COUNT(*) AS IssuesReceived, 0 AS IssuesResolved
FROM Issues
GROUP BY UserID, Date
UNION
SELECT User, ResolvedDate AS Date, 0 AS IssuesReceived, COUNT(*) AS IssuesResolved
FROM Issues
GROUP BY UserID, Date) AS u
GROUP BY UserID
答案 1 :(得分:0)
试试这个。
select userid,createddate as date,
count(`created date`) as `Issues Received`,
count(`Resolved Date`) as `Issues Resolved`
from yourtable
where `Resolved Date` = '1/12/2015'
Group by userid,createddate
考虑到创建新问题时解决的日期将为空
答案 2 :(得分:0)
不确定这是否完全回答了你的问题,但很可能你需要做这样的联盟:
select user_id
, action_date
, count(case when action = 'CREATED' then 1 else null end)
, count(case when action = 'RESOLVED' then 1 else null end)
from (
select user_id, created_date action_date, 'CREATED' action
from table
union
select user_id, resolved_date, 'RESOLVED' action
from table
where resolved_date is not null
) x
group by user_id, action_date
这有用吗?
答案 3 :(得分:0)
如果您确实使用的是Teradata,则可以使用full outer join
:
SELECT COALESCE(i.UserID, r.UserId) as UserId,
COALESCE(i.Date, r.Date) as Date,
COALESCE(IssuesReceived, 0) AS IssuesReceived,
COALESCE(IssuesResolved, 0) AS IssuesResolved
FROM (SELECT UserID, CreatedDate AS Date, COUNT(*) AS IssuesReceived
FROM Issues
GROUP BY UserID, Date
) i FULL OUTER JOIN
(SELECT User, ResolvedDate AS Date, COUNT(*) AS IssuesResolved
FROM Issues
GROUP BY UserID, Date
) r
ON i.UserId = r.UserId and i.Date = r.Date;