2017-08-23 37 views
0

通信的次数我已经尝试了这一点 -我想登录,然后它的前一周周报等在SQL

declare @maxD datetime 
select @maxD= max(loggingdate) FROM CommunicationLogs 

SELECT Count(loggingdate),@maxD FROM CommunicationLogs 
WHERE loggingdate BETWEEN (@maxD - 6) AND @maxD and clientid ='20154' 

4 2017-08-21 11:18:24.930 

以上输出装置通信之前,2017年8月21日11:18登录: 24.930和7天前是4.但问题是它具体到最大日期。我想为所有记录的日期得到相同的结果。

我试过,但给人错误的数据 -

SELECT COUNT(id),loggingdate FROM CommunicationLogs 
WHERE loggingdate BETWEEN (loggingdate - 6) AND loggingdate and clientid ='20154' 
group by loggingdate 
ORDER BY loggingdate desc 

的SQL数据是 -

31935 2017-08-21 11:18:24.930 
31936 2017-08-21 00:00:00.000 
31933 2017-08-18 14:53:03.707 
31934 2017-08-18 00:00:00.000 
31924 2017-08-10 12:06:00.397 
31918 2017-07-26 00:00:00.000 
31919 2017-07-26 00:00:00.000 
31920 2017-07-26 00:00:00.000 
31921 2017-07-26 00:00:00.000 
31922 2017-07-26 00:00:00.000 
31923 2017-07-26 00:00:00.000 
31898 2017-07-25 00:00:00.000 
31899 2017-07-25 00:00:00.000 
31900 2017-07-25 00:00:00.000 
31901 2017-07-25 00:00:00.000 
31902 2017-07-25 00:00:00.000 
31903 2017-07-25 00:00:00.000 
31904 2017-07-25 00:00:00.000 
31905 2017-07-25 00:00:00.000 
31906 2017-07-25 00:00:00.000 
31907 2017-07-25 00:00:00.000 
31908 2017-07-25 00:00:00.000 
31909 2017-07-25 00:00:00.000 
31910 2017-07-25 00:00:00.000 
31911 2017-07-25 00:00:00.000 
31912 2017-07-25 00:00:00.000 
31913 2017-07-25 00:00:00.000 
31914 2017-07-25 00:00:00.000 
31915 2017-07-25 00:00:00.000 
31916 2017-07-25 00:00:00.000 
31917 2017-07-25 00:00:00.000 
31889 2017-07-24 00:00:00.000 
31890 2017-07-24 00:00:00.000 
+0

什么是“错误的数据“你正在从你当前的查询中得到什么,而”正确的数据“会是什么呢? –

回答

0

试试这个,希望有帮助,或者让你和你的想法。

SQL Server 2005中

--to获得测井数据每周取决于最大日期

select Count(loggingDate),max(loggingDate) from (select CommunicationLogs.id,CommunicationLogs.loggingDate,cast(DATEDIFF(day,loggingDate,maxLoggingDate)/7 as int)WeekGroup from CommunicationLogs 
cross apply (select max(loggingDate) maxLoggingDate from CommunicationLogs)maxData)weekGroupData 
group by WeekGroup 

--to每周获得测井数据取决于最大日期为每个ID

select id,Count(*),max(loggingDate) from 
(select CommunicationLogs.id,CommunicationLogs.loggingDate,cast(DATEDIFF(day,loggingDate,maxLoggingDate)/7 as int)WeekGroup from CommunicationLogs 
    left join (select id,max(loggingDate) maxLoggingDate from CommunicationLogs group by id) maxData 
    on maxData.id=CommunicationLogs.id 
)weekGroupData 
group by id,WeekGroup 

SQL server 2008+您可以使用CTE子句使其更具可读性

--to获得测井数据每周取决于最大日期

with maxData as 
(
select max(loggingDate) maxLoggingDate from CommunicationLogs 
), 
weekGroupData as(
select CommunicationLogs.id,CommunicationLogs.loggingDate,cast(DATEDIFF(day,loggingDate,maxLoggingDate)/7 as int)WeekGroup from CommunicationLogs 
cross apply maxData 
) 
select Count(loggingDate),max(loggingDate) from weekGroupData 
group by WeekGroup 

--to获得测井数据每周取决于最大日期为每个ID

with maxData as 
(
select id, max(loggingDate) maxLoggingDate from CommunicationLogs 
group by id 
), 
weekGroupData as(
select CommunicationLogs.id,CommunicationLogs.loggingDate,cast(DATEDIFF(day,loggingDate,maxLoggingDate)/7 as int)WeekGroup from CommunicationLogs 
left join maxData on maxData.id=CommunicationLogs.id 
) 
select id,Count(*),max(loggingDate) from weekGroupData 
group by id,WeekGroup