Select
CIDetail.Itemname,
sum(CIDetail.TaxAmount+ CIDetail.LineAmount) As [TotalAmount]
From
CIDetail (Nolock)
INNER JOIN CIHeader On CIDetail.InvoiceNo= CIHeader.InvoiceNo
Where
CIHeader.InvoiceDate Between '2010-04-01' AND '2014-04-01'
Group By
CIDetail.Itemname
答案 0 :(得分:0)
将order by子句添加到查询中。在这里,正如您所说,您希望按日期,我将假设一个项目名称只有一个 InvoiceDate 。因此, max 将具有相同的值。您必须按照以下方式执行此操作
Select CIDetail.Itemname,
sum(CIDetail.TaxAmount+ CIDetail.LineAmount) As [TotalAmount],
From CIDetail (Nolock) INNER JOIN CIHeader
On CIDetail.InvoiceNo= CIHeader.InvoiceNo
Where CIHeader.InvoiceDate Between '2010-04-01' AND '2014-04-01' Group By
CIDetail.Itemname
Order By max(CIHeader.InvoiceDate) ASC
答案 1 :(得分:0)
有一个派生表,您使用ANSI SQL的EXTRACT
来获取日期部分,并同时添加金额。在主要级别,您GROUP BY
同时为Itemname和year:
Select Itemname, "year", SUM(Amount) as TotalAmount
from
(
CIDetail.Itemname,
extract(year from CIHeader.InvoiceDate) as "year",
CIDetail.TaxAmount + CIDetail.LineAmount As Amount
From
CIDetail (Nolock)
INNER JOIN CIHeader On CIDetail.InvoiceNo= CIHeader.InvoiceNo
) dt
Group By
Itemname, "year"
没有标记有问题的dbms,但如果您的dbms不支持EXTRACT
,请尝试使用YEAR(CIHeader.InvoiceDate)
或其他内容。