我正在尝试计算每个工作日的表中用户ID的不同数量(例如工作日1 =星期一545用户ID,工作日2 =星期二等120用户ID)。我在Access Visual Basic中这样做,但语法应该是SQL的通用。这是我的VB代码:
sSQL = " SELECT Weekday(" & tablename & ".[DATE]) AS WEEKDAY, Count(DISTINCT " & tablename & ".[UserID]) AS User_COUNT"
sSQL = sSQL & " FROM " & tablename
sSQL = sSQL & " GROUP BY Weekday(" & tablename & ".[DATE])"
qdf.SQL = sSQL
纯SQL语法应该如下所示(根据注释和测试编辑):
SELECT Weekday(tbl.[Date]) AS WEEKDAY, Count(DISTINCT tbl.[UserID]) AS User_COUNT
FROM tbl
GROUP BY Weekday(tbl.[Date])
..而[Date]是tbl中格式为Datetime的字段,[UserID]是格式为Long(带有重复项)的字段。
当我尝试运行该命令时,它告诉我“查询语法中缺少运算符......” 这是我的VB代码的问题还是SQL语法错了?
答案 0 :(得分:1)
MS Access数据库引擎不支持COUNT(DISTINCT ...)
。
要对它进行工作,请参阅此主题:SQL : how can i count distinct record in MS ACCESS作者建议使用子查询来解决问题:
SELECT
user_id
, COUNT(*) AS count_distinct_clients
FROM
( SELECT DISTINCT
user_id,
client_id
FROM tbl_sActivity
) AS tmp
GROUP BY
user_id ;
根据需要更改查询代码。
<强> [编辑] 强>
SELECT
wday, COUNT(UserId) AS count_distinct_users
FROM
( SELECT DISTINCT WEEKDAY([Date]) AS wday, UserId
FROM tblName
) AS tmp
GROUP BY
wday;