简体   繁体   English

SQL Sum MTD和YTD

[英]SQL Sum MTD & YTD

I have only just started looking into SQL. 我才刚刚开始研究SQL。

I have a SQL Server 2008 r2 database that will return two fields DocDate & InvValue. 我有一个SQL Server 2008 r2数据库,它将返回两个字段DocDate和InvValue。 I need to sum the InvValues as MTD & YTD as of Today's Date So it looks like 我需要将InvValues汇总为今天的MTD和YTD,所以看起来像

**Period** ///////  **Total value**
MTD        ////////////111111.11
YTD       /////////////999999.99

I have done a fair amount of Googling and can do one or the other with SUM & DATEPART, but I am stuck with trying to do both. 我已经做了大量的Google搜寻,并且可以使用SUM&DATEPART进行一项或多项工作,但是我坚持尝试两者都做。

Can someone give me some pseudo-code that would help me google a little further. 有人可以给我一些伪代码,以帮助我进一步谷歌。

Thank you @Gordon Linoff, That helped a lot and I learned something, That I will find useful in the future. 谢谢@戈登·利诺夫(Gordon Linoff),这对我有很大帮助,我学到了一些东西,这对我将来会很有用。 My code now looks like: 我的代码现在看起来像:

SELECT
SUM(CASE WHEN YEAR(T1.[DocDate]) = YEAR(GETDATE()) THEN T0.[TotalSumSy] END) AS YTD,
SUM(CASE WHEN YEAR(T1.[DocDate]) = YEAR(GETDATE()) AND MONTH(T1.[DocDate]) = MONTH(GETDATE()) THEN T0.[TotalSumSy] END) AS MTD

FROM [dbo].[INV1] T0 INNER JOIN [dbo].[OINV] T1 ON T1.[DocEntry] = T0.[DocEntry]

However I now get 但是我现在得到

YTD.........MTD
99999.99....111111.11

And I need 我需要

YTD........99999.99
MTD........11111.11

Any further assistance would be appreciated. 任何进一步的帮助,将不胜感激。

You can do this with conditional aggregation: 您可以使用条件聚合来做到这一点:

select sum(case when year(docdate) = year(getdate()) then InvValue end) as YTD,
       sum(case when year(docdate) = year(getdate()) and month(docdate) = month(getdaate())
                then InvValue
           end) as MTD
from table t;

This assumes you have no future dates in the table. 假设您在表格中没有将来的日期。 If you do, add in docdate < getdate() to both clauses. 如果这样做,请将docdate < getdate()到两个子句中。

EDIT: 编辑:

If you need this in two rows, you can simply do this: 如果需要两行,只需执行以下操作:

select (case when n.n = 1 then 'YTD' else 'MTD' end) as which,
       (case when n.n = 1 then YTD else MTD end) as value
from (select sum(case when year(docdate) = year(getdate()) then InvValue end) as YTD,
             sum(case when year(docdate) = year(getdate()) and month(docdate) = month(getdaate())
                      then InvValue
                 end) as MTD
      from table t
     ) cross join
     (select 1 as n union all select 2) n;
SELECT
  Period = 'MTD',
  Total_value = SUM(T0.TotalSumSy) 
FROM dbo.INV1  T0 
  INNER JOIN dbo.OINV  T1 
     ON T1.DocEntry = T0.DocEntry
WHERE 
    T1.DocDate >= DATEADD(month,DATEDIFF(month,'20010101',GETDATE()),'20010101')
  AND 
    T1.DocDate < DATEADD(month,1+DATEDIFF(month,'20010101',GETDATE()),'20010101')

UNION ALL

SELECT
  'YTD', 
  SUM(T0.TotalSumSy) 
FROM dbo.INV1  T0 
  INNER JOIN dbo.OINV  T1 
     ON T1.DocEntry = T0.DocEntry
WHERE 
    T1.DocDate >= DATEADD(year,DATEDIFF(year,'20010101',GETDATE()),'20010101')
  AND 
    T1.DocDate < DATEADD(year,1+DATEDIFF(year,'20010101',GETDATE()),'20010101') ;

The (complicated) conditions at the WHERE clauses are used instead of the YEAR(column) = YEAR(GETDATE() and the other you had previously, so indexes can be used. WHen you apply a function to a column, you make indexes unsuable (with some minor exceptions for some functions and some verios of SQL-Server.) So, the best thing is to try to convert the conditions to this type: 使用WHERE子句中的(复杂)条件代替YEAR(column) = YEAR(GETDATE()和以前的其他条件,因此可以使用索引。在将函数应用于列时,使索引不可用(对于SQL Server的某些功能和某些版本,有一些较小的例外。)因此,最好的办法是尝试将条件转换为这种类型:

column <operator> AnyComplexFunction()

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM