[英]Multiple Selects into one select
I'm trying to put some data together for a High Charts Bar chart using ASP.NET. 我正在尝试使用ASP.NET将一些数据汇总到高图表条形图中。 Basically, i have three users who i need to track when they have logged into the system.
基本上,我有三个用户需要跟踪他们何时登录系统。 the variants to be used are:
使用的变体是:
1) Today 2) This Week 3) Last Week 4) Last Month 1)今天2)本周3)上周4)上个月
So, i've created individual tsql scripts for today and and last week, but i'm now a little stuck on how to combine the two statemets, which will eventually be four. 因此,我已经为今天和上周创建了单独的tsql脚本,但是现在我对如何结合这两个statemet(最终将是四个statemet)有些困惑。
SELECT Count(*) as CountToday from hitsTable WHERE Convert(date,hitDate) =
Convert(date,GETDATE()) Group by UserId
SELECT count(*) as CountLatWeek from hitTable
where hitDate between (DATEADD(week, DATEDIFF (week,0,GETDATE()),-1))
AND getDate() Group by UserId
Searhing on google, leads me to nested select statements, which all seem to form dependacies with the two statements. 在Google上进行的搜索使我进入了嵌套的select语句,这些语句似乎都与这两个语句形成依赖关系。 However, what i need to do is produce a table of results like this:
但是,我需要做的是生成如下结果表:
EDIT 编辑
I've set up a SQL Fiddle, so we can test out the examples 我已经设置了一个SQL Fiddle,因此我们可以测试示例
http://www.sqlfiddle.com/#!6/a21ec http://www.sqlfiddle.com/#!6/a21ec
the fiddle has tsql for today and tsql for last week (which may need some tweaking) 小提琴今天有tsql,上星期有tsql(可能需要进行一些调整)
Select Distinct
UserId
, ( Select Count(*) as CountToday from hitsTable h2
Where h2.UserId = h1.UserId
And Convert(date,hitDate) = Convert(date,GETDATE())
) As CountToday
, ( Select count(*) as CountLatWeek from hitsTable h2
Where h2.UserId = h1.UserId
And hitDate Between DATEADD(dd, -(DATEPART(dw, GetDate())-1)-7, GetDate())
And DATEADD(dd, 7-(DATEPART(dw, GetDate()))-7, GetDate())
) As CountLastWeek
FROM hitsTable h1
Try this query 试试这个查询
select
id,
sum(case when Convert(date,hitDate) = Convert(date,GETDATE()) then 1 else 0 end) as as CountToday,
sum(hitDate between (DATEADD(week, DATEDIFF (week,0,GETDATE()),-1)) AND getDate() then 1 else 0 end) as CountLatWeek,
...... -- Add more condition
from
hitsTable
group by
UserId
Edit 编辑
select
userid,
sum(case when Convert(date,hitDate) =
Convert(date,GETDATE()) then 1 else 0 end) as cnt
from
hitstable
group by userid
| USERID | CNT |
|--------|-----|
| User1 | 3 |
| User2 | 0 |
Here's another alternative based on @Avinash comment on the question. 这是另一个基于@Avinash评论的替代方法。
Select
UserId
, CountTodayTable.CountToday
, CountLatWeekTable.CountLatWeek
, ...
FROM hitsTable h1
Inner Join
( Select Count(*) as CountToday from hitsTable h2
Where h2.UserId = h1.UserId
And Convert(date,hitDate) = Convert(date,GETDATE())
) CountTodayTable
On CountTodayTable.UserId = h1.UserId
Inner Join
( Select count(*) as CountLatWeek from hitTable h2
Where h2.UserId = h1.UserId
And hitDate between (DATEADD(week, DATEDIFF (week,0,GETDATE()),-1)) And getDate()
) CountLatWeekTable
On CountLatWeekTable.UserId = h1.UserId
...
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.