【问题标题】:Classic ASP Access database query, sum and group经典 ASP Access 数据库查询、求和和分组
【发布时间】:2014-02-25 12:03:46
【问题描述】:

我有一个 Access 数据库 (2003) 的经典 ASP 前端,并且想对我的数据进行一些计算。我有每天租用的物品,并且有与之相关的每日运行成本。这是表格的详细信息;

  • 表名:rent_table
  • 字段:
    • rentid
    • 开始日期
    • 结束日期
    • 租金
    • 租金成本
    • 预订费

目前在 Excel 中我这样做;

(startdate-enddate) *rentfee+bookingfee -minus (startdate-enddate)*rentcost

这给了我每个rentid的利润。我按 enddate 排序,然后按月(月末)对其进行分组,这样当 enddate 到期时,我知道可以释放利润。在 Excel 中,我手动进行月份分组/求和。

目标是在 ASP 中自动化该过程,以便我得到总结的结果;

  • 报告标题:利润,月度
  • 结果行 1:2.00 英镑,1 月 14 日
  • 结果行 2:4.00 英镑,2 月 14 日
  • 结果行 N:6.00 英镑,1 月 16 日

我知道如何构建 ASP 页面、连接数据库和显示查询结果。这确实是我需要的查询。提前谢谢你(请温柔,我的第一篇文章)。

【问题讨论】:

    标签: sql ms-access asp-classic


    【解决方案1】:

    类似下面的内容应该能让你接近。该查询未经测试,但应该为您提供一个良好的起点。月份将以 1 到 12 的数字形式返回;您可能希望按照您认为合适的方式对其进行格式化。

    SELECT Month(enddate) AS Mo, Year(enddate) as Yr, 
           Sum( ((enddate - startdate) * rentfee) + bookingfee - ((enddate - startdate) * rentcost) ) as Profit
    FROM rent_table
    WHERE enddate Is Not Null
    GROUP BY Year(enddate), Month(enddate)
    ORDER BY Year(enddate), Month(enddate)
    

    【讨论】:

    • 效果很好,谢谢!我需要在 Sum 的 endate/startdate 前面添加 CDate,因为我发现它们是数据库中的文本字段(而且我不想打扰数据库)。此外,我在 Sum 中添加了 Round(...,2) 以去除多余的小数。最后,我使用 IF 语句将月份编号转换为月份名称。工作完成。
    猜你喜欢
    • 2011-10-14
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2015-08-13
    相关资源
    最近更新 更多