VB6 ms访问sql查询distinct count两个表

Posted

技术标签:

【中文标题】VB6 ms访问sql查询distinct count两个表【英文标题】:VB6 ms access sql query distinct count two tables 【发布时间】:2014-10-22 21:58:29 【问题描述】:

我需要为每个特定员工输出以下数据: employee_Id、employee_name、presence.year、presence.month、count(day)、sum(work hours)、sum(holiday hours)、[work hours] - [holiday hours]。 我在 vb6 项目上使用了这个查询,但似乎效果不佳:

SELECT
     Employees.Employee_ID AS ID
     , Employees.Full_Name AS Name
     , Presence.Yr AS Year
     , Presence.Mnth AS Month
     , SUM(IIF(Presence.Dy, 1, 0)) Work_Days
     , SUM(IIF(Presence.Hr, Presence.Hr, 0)) AS Work_Hours
     , SUM(IIF(Presence.Holidays, Presence.Holidays, 0)) AS Holidays_Hours
     , SUM(IIF(Presence.Hr, Presence.Hr, 0)) - SUM(IIF(Presence.Holidays, Presence.Holidays, 0) AS Total_Work_Hours
FROM Employees
INNER JOIN Presence ON Presence.Employee_ID = Employees.Employee_ID
WHERE Employees.Company = 'aCompany'
     AND Employees.Employee_ID = 'anEmmployee'
GROUP BY  Employees.Employee_ID, Employees.Full_Name, Presence.Yr, Presence.Mnth

如图中这样:http://oi60.tinypic.com/122c6xz.jpg

有人可以帮忙吗?

【问题讨论】:

你能给我们提供预期的结果吗?首先,从 GROUP BY 子句中删除 Presence.Dy, Presence.Hr, Presence.Holidays,您也可以从 SELECT 子句中删除 DISTINCT 需要这个表作为结果:oi60.tinypic.com/122c6xz.jpg 好的,您查询的当前结果是什么?您是否尝试过我提到的更正? 在 ID 列中显示我插入的那个,但在名称列中显示表中的所有其他员工姓名 这个问题的标题太可怕了。看起来你所做的只是向它抛出关键字和标签。 【参考方案1】:

这是您的查询并进行了一些调整:

SELECT
     Employees.Employee_ID AS ID
     , Employees.Full_Name AS Name
     , Presence.Yr AS Year
     , Presence.Mnth AS Month
     , SUM(IIF(Presence.Dy, 1, 0)) Work_Days
     , SUM(IIF(Presence.Hr, Presence.Hr, 0)) AS Work_Hours
     , SUM(IIF(Presence.Holidays, Presence.Holidays, 0)) AS Holidays_Hours
     , SUM(IIF(Presence.Hr, Presence.Hr, 0)) - SUM(IIF(Presence.Holidays, Presence.Holidays, 0) AS Total_Work_Hours
     , SUM(Presence.Hr) / Employees.HoursPerDay + Employees.TotalAnnualHolidays - Employees.DoneHolidays AS AvailableHolidays
FROM Employees
INNER JOIN Presence ON Presence.Employee_ID = Employees.Employee_ID
WHERE Employees.Company = 'aCompany'
     AND Employees.Employee_ID = 'anEmmployee'
GROUP BY  Employees.Employee_ID, Employees.Full_Name, Employees.HoursPerDay, Employees.TotalAnnualHolidays, Employees.DoneHolidays, Presence.Yr, Presence.Mnth

您能否测试这个新查询并提供有关结果以及与您要查找的内容的差异的信息?

希望这会有所帮助。

【讨论】:

查询似乎运行良好,但是当涉及到空值时,它会显示错误。如何避免空值或将其设置为 0? 我更新了查询以替换 NULL 值,你能测试它并告诉我它是否更好用吗? 我在第一个 CASE 中显示语法错误。这种语法是否也适用于 ms 访问?? 我使用 IIF 语句而不是 CASE 修复了它,但是当我运行软件时,它显示错误:对象不支持此属性或方法。结果显示在 DBGrid 中,但出现此错误。 您能用您使用的新查询和错误的详细信息更新您的问题吗?

以上是关于VB6 ms访问sql查询distinct count两个表的主要内容,如果未能解决你的问题,请参考以下文章

访问 Vb6 查询

[2]SQL SELECT DISTINCT

VB6 / SQL“长文本”比较不正确(MS Access 2013)

SELECT Count Distinct Syntax MS Access SQL [重复]

对 SQL Server 的 Ms Access 查询 - DistinctRow

MS ACCESS SQL QUERY COUNT DISTINCT