Table Schema
(
DeviceLogId int
DeviceId int
UserId int
LogDate datetime
)
112 25 66 2015-07-22 11:02:15.000
332 25 66 2015-07-22 17:29:25.000
555 25 88 2015-07-23 19:09:35.000
779 25 67 2015-07-24 16:23:49.000
1003 29 17 2015-07-18 13:03:04.000
我想輸出像
Intime Outtime logdate Incount OutCount
2015-01-01 10:22:29.000 2015-01-01 19:58:43.000 2015-01-01 7 11
2015-01-02 09:52:26.000 2015-01-02 20:25:25.000 2015-01-02 2 2
我已創建查詢一個用戶ID的工作,但如果我想它用於多種用途
select e.Intime,e.Outtime,e.logdate,e.Incount,e.OutCount from
(
select a.intime as Intime,b.outtime as Outtime,c.logdate,c.InCount as Incount,d.OutCount as OutCount from
(
select min(logdate) intime,cast(LogDate as date) logdate,userid
from DeviceLogs where deviceid in (26,31) and cast(LogDate as date) between '2015-01-01' and '2015-01-02' and UserId=7
group by cast(LogDate as date), UserId
)a inner join
(
select max(logdate) outtime,cast(LogDate as date) logdate,userid from DeviceLogs
where deviceid in (25,30) and cast(LogDate as date) between '2015-01-01' and '2015-01-02' and UserId=7
group by cast(LogDate as date), UserId
) b on a.logdate = b.logdate
left join
(
select UserId, cast(LogDate as date) logdate ,count(DeviceLogId) as InCount from DeviceLogs
where deviceid in (26,31) and cast(LogDate as date) between '2015-01-01' and '2015-01-02' and UserId=7
Group by UserId, cast(LogDate as date)
)c on b.logdate = c.logdate
left join
(
select UserId, cast(LogDate as date) logdate ,count(DeviceLogId) as OutCount from DeviceLogs
where deviceid in (25,30) and cast(LogDate as date) between '2015-01-01' and '2015-01-02' and UserId=7
Group by UserId, cast(LogDate as date)
)d on c.logdate = d.logdate
)e
所以我想f蝕刻所有用戶數據。 等待回覆
我想檢索所有的用戶數據,並與電網 結合具有更好的性能
表格數據:2014(年)。結果:2015年。你的結果是什麼? –
我已將表格數據更改爲2015 – bharat
和月份,日期? –