【问题标题】:Daily sums for a range of years多年的每日总和
【发布时间】:2020-04-04 00:17:11
【问题描述】:

下面的代码返回一系列年份的每日总和。我想将约束添加到 COGS 列 WHERE IVENTORY.CONSIGNMENT = "No"。我尝试添加一个子查询,但我尝试的一切都导致了错误。应如何编码子查询以使用以下代码?

SELECT a.[Month / Day / Year],
       Round(Sum(a.[COGS]), 2) AS COGS
FROM
  (SELECT Format(DatePart("m", sale_date), "00") & "/" & Format(DatePart("d", sale_date), "00") & "/" & DatePart("yyyy", sale_date) AS [Month / Day / Year],
          Format(DatePart("d", s.sale_date), "00") AS [Sale Day],
          Format(DatePart("m", s.sale_date), "00") AS [Sale Month],
          DatePart("yyyy", s.sale_date) AS [Sale Year],
          Nz(i.VENDOR_ACTUAL_PRICE, 0)*Nz(s.quantity, 0) AS COGS
   FROM INVENTORY AS i
   INNER JOIN SALES_RECEIPT AS s ON i.INVENTORY_ID = s.INVENTORY_ID
   WHERE DatePart("yyyy", sale_date) BETWEEN 2000 AND 2100) AS a
GROUP BY a.[Month / Day / Year],
         a.[Sale Month],
         a.[Sale Day],
         a.[Sale Year]
ORDER BY a.[Sale Year],
         a.[Sale Month],
         a.[Sale Day];

【问题讨论】:

    标签: sql ms-access subquery aggregate-functions


    【解决方案1】:

    假设我已经正确理解了您的要求,我相信您可以在不使用子查询的情况下完成此操作,例如:

    select 
        format(s.sale_date,"mm/dd/yyyy") as [Month / Day / Year],
        round(sum(nz(i.vendor_actual_price, 0)*nz(s.quantity, 0)),2) as COGS
    from
        inventory i inner join sales_receipt s on i.inventory_id = s.inventory_id
    where
        i.consignment = 'No' and 
        year(s.sale_date) between 2000 and 2100
    group by
        format(s.sale_date,"mm/dd/yyyy"), s.sale_date
    order by
        s.sale_date
    

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2018-08-29
      • 1970-01-01
      • 2021-04-13
      • 2013-12-31
      • 1970-01-01
      • 1970-01-01
      相关资源
      最近更新 更多