我有桌子:
我希望按日期分组显示特定日期范围内特定用户ID的文档数,nispah和hozactions。 类似的东西:
如果每张桌子上有不同的日期,我如何按日期分组?
答案 0 :(得分:1)
这里有点猜测,但我认为您需要做的是生成一组所有日期并与用户进行交叉连接,然后将其他表连接到employee.ID上。 / p>
也许这就是你想要的:
select
dates.tscreatedate
, count(distinct d.docid) number_of_documents
, count(distinct n.nispahid) number_of_nispah
, count(distinct h.actid) number_of_hozactions
from
vwemployee e
cross join
(
select cast(tscreatedate as date) tscreatedate from documents
union all
select cast(tscreatedate as date) tscreatedate from nispah
union all
select cast(tscreatedate as date) tscreatedate from hozactions
) dates
left join
documents d on e.id = d.tscreatedby
and cast(d.tscreatedate as date) = dates.tscreatedate
left join
nispah n on e.id = n.tscreatedby
and cast(n.tscreatedate as date) = dates.tscreatedate
left join
hozactions h on e.id = h.tscreatedby
and cast(h.tscreatedate as date) = dates.tscreatedate
where
e.id = 1
group by
dates.tscreatedate
或许我完全误解了,如果是的话,我会删除这个答案。