如何使用sql计算列的总和

时间:2018-01-30 13:20:52

标签: sql-server crystal-reports

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;

我编写此查询以获取销售表中所选类别的总金额。

喜欢..,

分类名称:饼干

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

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

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

提前致谢,

enter image description here

1 个答案:

答案 0 :(得分:0)

如果你想使用SUM(),你需要一个分组,也许是这样的?

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