0

我有查询(如下)显示“每月”列中的数据。

结果

但我需要将此查询重写为累积总和,如图第三列所示。我该怎么做?感谢您的回复。

我目前的查询:

select cast( ( Prodej.Ce_Jedn * Prodej.Mnoz ) / 1000 as numeric(15,2) ) as "Monthly" ,
       YEAR(  FAV.vatDate ) as "Rok" ,
       month( FAV.VatDate ) as "Měsíc" 
from Prodej
join FAKTVYDA FAV on FAV.Ci   = PRODEJ.C_Fak
                 and FAV.Rada = PRODEJ.R_Fak
where YEAR(FAV.VATDate) > year(getdate())-3
  and FAV.Rada in ('10','20','30','60') 
  and PRODEJ.C_Fak <> 0
  and '@{Stredisko.ParameterValue}' = case Prodej.Str 
                                        when ''  then FAV.Str
                                        when '-' then FAV.Str 
                                        else     Prodej.str
                                      end
order by month(FAV.VatDate)
4

2 回答 2

0

当您使用 SUM 语句时,查询还需要 GROUP BY 语句。这不包括在上面的查询中,所以它不起作用。

于 2014-05-09T06:47:51.720 回答
-1

您可以通过在要汇总的字段前面使用 SUM 语句来实现这一点。请参阅这篇文章,它提供了适合您问题的解决方案。

select cast( ( Prodej.Ce_Jedn * Prodej.Mnoz ) / 1000 as numeric(15,2) ) as "Monthly" ,
YEAR(  FAV.vatDate ) as "Rok" ,
month( FAV.VatDate ) as "Měsíc" 
SUM(( Prodej.Ce_Jedn * Prodej.Mnoz ) / 1000 as numeric(15,2) ) as "Cumulative"
from Prodej
join FAKTVYDA FAV on FAV.Ci   = PRODEJ.C_Fak
and FAV.Rada = PRODEJ.R_Fak
where YEAR(FAV.VATDate) > year(getdate())-3
and FAV.Rada in ('10','20','30','60') 
and PRODEJ.C_Fak <> 0
and '@{Stredisko.ParameterValue}' = case Prodej.Str 
when ''  then FAV.Str
when '-' then FAV.Str 
else     Prodej.str
end
order by month(FAV.VatDate)

希望有帮助,

克里斯

于 2013-10-12T00:58:10.510 回答