我創建了下面的代碼,該代碼爲我提供了按公司細分的唯一用戶數。我想添加一個日期修飾符,它會有兩列,一列用於「上週」,另一列用於「2周前」。我希望這些列分別顯示前一週日期的活躍用戶和基於字段「users.date」之前一週的活動用戶。任何幫助都會很棒。我正在使用SQL Server Management Studio 2008 R2。將「上週」和「2周前」添加到SQL查詢
--Active Users
SELECT company.companyName as 'Group Name',
COUNT(distinct users.userid) [Count]
FROM Users, Company
WHERE
Jobstate = '6'
and company.companyID = users.companyid
and company.companyID in (1,4,31)
GROUP BY company.companyName
目前我收到這個結果:
Group Name | Count |
------------------------
Company 1 | 104 |
Company 2 | 74 |
Company 3 | 46 |
我想看到的是:
Group Name | Last Week | 2 Weeks Ago |
--------------------------------------
Company 1 | 14 | 16 |
Company 2 | 7 | 12 |
Company 3 | 4 | 8 |
SQL服務器= ['DATEDIFF()'](http://msdn.microsoft.com/en-us/library/ms189794.aspx),MySQL = ['DATEDIFF'](http://dev.mysql.com/doc/refman /5.5/en/date-and-time-functions.html#function_datediff)和其他口味有類似的東西。從那裏,關於如果結果是7天的<=7 days or >,要知道命名比較。 –