如何使用 sql 计算列的总和

How to calculate sum of a column using sql

SELECT 
    [si].[total] AS [netamount],
    *, 
    IIF([invoicedate] >= '01-07-2017', 1, 0) AS [IsGstBill],
    [p].[languagename] AS [arabicname],
    [si].[tax] AS [stax], [si].[Total] AS [NetAmount]
FROM 
    [sales] [s], [accounthead] [a],
    [salesitem] [si], [product] [p]
WHERE 
    [s].[customerid] = [a].[id]
    AND [a].[entrytype] = 'C'
    AND [si].[salesid] = [s].[id]
    AND [si].[productid] = [p].[id]
    AND [s].[invoicedate] >= '01-01-2018'
    AND [s].[invoicedate] <= '30-01-2018'
    AND [CategoryID] = 1025;

我编写此查询是为了获取我销售中所选类别的总金额 table。

喜欢..,

类别名称:饼干

你在我的问题上看到了这张图片,实际上 2760 出现在顶部但我收到了 2300 ..实际上我的查询只代表第一行值。 我如何填充这个特定的金额。

实际上我在查询中尝试了 sum() 方法。但它会填充所有账单的总金额。

我现在做什么来获得我的输出,请指导我。

提前致谢,

如果你想用SUM(),你需要一个group by,大概像这样?

SELECT SUM ([si].[total]) AS [netamount],
   InvoiceNO -- not sure about your column name here, pls edit.
FROM [sales] [s]
   , [accounthead] [a]
   , [salesitem] [si]
   , [product] [p]
WHERE [s].[customerid] = [a].[id]
  AND [a].[entrytype] = 'C'
  AND [si].[salesid] = [s].[id]
  AND [si].[productid] = [p].[id]
  AND [s].[invoicedate] >= '01-01-2018'
  AND [s].[invoicedate] <= '30-01-2018'
  AND [CategoryID] = 1025
GROUP BY InvoiceNr -- Same here