Welcome to OGeek Q&A Community for programmer and developer-Open, Learning and Share
Welcome To Ask or Share your Answers For Others

Categories

0 votes
311 views
in Technique[技术] by (71.8m points)

sql - 多年的每日总和(Daily sums for a range of years)

The following code is functional but I would like to add a constraint to the [COGS] sum.

(以下代码可以正常工作,但是我想对[COGS]总和添加一个约束。)

The constraint is: 'Nz(i.VENDOR_ACTUAL_PRICE, 0)*Nz(s.quantity, 0) WHERE INVENTORY.CONSIGNMENT = "No" AS COGS,` How can the constraint be added?

(约束是:'Nz(i.VENDOR_ACTUAL_PRICE,0)* Nz(s.quantity,0)WHERE INVENTORY.CONSIGNMENT =“ No”作为销,如何添加约束?)

SELECT a.[Month / Day / Year],
           Round(Sum(a.[Gross Sales]), 2) AS [Gross Sales],
           Round(Sum(a.[COGS]), 2) AS COGS,
           Round(Sum(a.[Sales Margin]), 2) AS [Sales Margin],
           Round([Sales Margin]/[Gross Sales], 2) AS [Profit Margin]
    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(s.SELLING_PRICE * s.quantity, 0) AS [Gross Sales],
              Nz(i.VENDOR_ACTUAL_PRICE, 0)*Nz(s.quantity, 0) AS COGS,
              Nz(s.SELLING_PRICE * s.quantity, 0) - Nz(i.VENDOR_ACTUAL_PRICE * s.quantity, 0) AS [Sales Margin]

       FROM INVENTORY AS i
       INNER JOIN SALES_RECEIPT AS s ON i.INVENTORY_ID = s.INVENTORY_ID
       WHERE DatePart("m", sale_date) BETWEEN 1 AND 12
         AND DatePart("d", sale_date) BETWEEN 1 AND 31
         AND 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];
  ask by Code Mechanik translate from so

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
Welcome To Ask or Share your Answers For Others

1 Reply

0 votes
by (71.8m points)
等待大神答复

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
OGeek|极客中国-欢迎来到极客的世界,一个免费开放的程序员编程交流平台!开放,进步,分享!让技术改变生活,让极客改变未来! Welcome to OGeek Q&A Community for programmer and developer-Open, Learning and Share
Click Here to Ask a Question

1.4m articles

1.4m replys

5 comments

57.0k users

...