使用SQL Server对会计年度进行分组

时间:2010-10-09 17:26:31

标签: sql sql-server

SQL Server中是否有一种方法可以从具有日期列(1998年到2010年)的表中显示会计年度(从10月1日开始到9月30日结束)。这就是我所做的:

select 'FY1999' as FY, site, count(*)
from mytable
where mydate >='10/1/1998' 
    and mydate <'10/1/1999'
group by site

select 'FY2000' as FY, site, count(*)
from mytable
where mydate >='10/1/1999' 
    and mydate <'10/1/2000'
group by site

select 'FY2001' as FY, site, count(*)
from mytable
where mydate >='10/1/2000' 
    and mydate <'10/1/2001'
group by site

在这个年度超过10年的时候,重复性是不是太多了?

8 个答案:

答案 0 :(得分:9)

这是一个可以为您提供所需信息的查询。

SELECT DATEPART(yyyy, DATEADD(mm, 3, mydate)) AS FY, site, COUNT(*) AS row_count
FROM mytable
GROUP BY DATEPART(yyyy, DATEADD(mm, 3, mydate)), site

答案 1 :(得分:6)

您甚至可以在SQL Server中创建用户定义的函数,该函数采用日期参数并将会计年度作为int返回:

CREATE FUNCTION GetFiscalYear(@TheDate date)
RETURNS int
AS
BEGIN
    DECLARE @FiscalYear int  

    IF DATEPART(month, @TheDate) < 10
        SELECT @FiscalYear = DATEPART(year, @TheDate)
    ELSE
        SELECT @FiscalYear = DATEPART(year, @TheDate) + 1  

    RETURN @FiscalYear
END

然后您可以将其用作例如:

SELECT Id, ShippingDate, GetFiscalYear(ShippingDate)
FROM SomeTable

答案 2 :(得分:1)

是的,它有点重复。我将使用DatePart和一些易于辨别的规则:

  • 如果月份是,则会计年度是该日期的年份。 10。
  • 如果月份> = 10
  • ,则会计年度为日期的年份+ 1

答案 3 :(得分:1)

这是英国4月到3月的动态脚本, 对于您可以用作参考的不同日期,

祝你好运

DECLARE @StartDate DATETIME
DECLARE @EndDate DATETIME

SET @StartDate = DATEADD(dd,0, DATEDIFF(dd,0, DATEADD( mm, -(((12 + DATEPART(m, getDate())) - 4)%12), getDate() ) - datePart(d,DATEADD( mm, -(((12 + DATEPART(m, getDate())) - 4)%12),getDate() ))+1 ) )  

SET @EndDate = DATEADD(SS,-1,DATEADD(mm,12,@StartDate))

SELECT @StartDate,@EndDate

答案 4 :(得分:1)

会计年度开始:

DATEADD(MONTH, DATEDIFF(MONTH, '20100401', getdate()) / 12 * 12, '20100401')

财年结束

DATEADD(MONTH, DATEDIFF(MONTH, '20100401', getdate()) / 12 * 12, '20110331')

如果需要,将getdate()替换为您自己的日期

答案 5 :(得分:0)

昨天有一个问题的答案,后来被删除了。 我不知道为什么。它有什么问题吗?

请不要将此答案投票,我只是想知道它被删除的原因。

经过严格的测试,我仍然无法设法做错。在我的例子中,财政年度从7月1日开始。

答案是:

SELECT SUM(value), CAST(Year(DateAdd(Month, -6, TransactionDate)) as varchar) + ' - ' + CAST(Year(DateAdd(Month, 6, TransactionDate)) as varchar) as 'FY'
FROM          mytable
GROUP BY CAST(Year(DateAdd(Month, -6, mydate)) as varchar) + ' - ' + CAST(Year(DateAdd(Month, 6, mydate)) as varchar)

答案 6 :(得分:0)

这是一些基于Simon的答案的测试Sql

    DECLARE @basestartdate datetime, @baseenddate datetime
SET @basestartdate = CAST('1 April 1753' AS datetime)
SET @baseenddate = CAST('31 March 1754' AS datetime)

;
WITh TestData as
(
SELECT
    CAST('1 April 2015' AS datetime) input,
    CAST('1 April 2015' AS datetime) expectedstartdate,
    CAST('31 March 2016' AS datetime) expectedenddate UNION SELECT
    CAST('2 April 2015' AS datetime),
    CAST('1 April 2015' AS datetime),
    CAST('31 March 2016' AS datetime) UNION SELECT
    CAST('31 December 2015' AS datetime),
    CAST('1 April 2015' AS datetime),
    CAST('31 March 2016' AS datetime) UNION SELECT
    CAST('1 January 2016' AS datetime),
    CAST('1 April 2015' AS datetime),
    CAST('31 March 2016' AS datetime) UNION SELECT
    CAST('28 February 2016' AS datetime),
    CAST('1 April 2015' AS datetime),
    CAST('31 March 2016' AS datetime) UNION SELECT
    CAST('31 March 2016' AS datetime),
    CAST('1 April 2015' AS datetime),
    CAST('31 March 2016' AS datetime) UNION SELECT
    CAST('1 April 2016' AS datetime),
    CAST('1 April 2016' AS datetime),
    CAST('31 March 2017' AS datetime)
),
Results AS
(
SELECT
    input,
    expectedstartdate,
    DATEADD(MONTH, 
            12 * (DATEDIFF(MONTH, @basestartdate, input) / 12),
            @basestartdate) startdate,
    expectedenddate,
    DATEADD(MONTH, 
            12 * (DATEDIFF(MONTH, @basestartdate, input) / 12),
            @baseenddate) enddate
FROM testdata
)
SELECT
    CASE
        WHEN (expectedstartdate = startdate) THEN 'Pass' ELSE 'Fail'
    END startdateresult,
    CASE
        WHEN (expectedenddate = enddate) THEN 'Pass' ELSE 'Fail'
    END startdateresult
FROM results
ORDER BY input

答案 7 :(得分:-1)

我没有方便的SQL服务器参考,但这是我在MySQL中的方式:

select date_format (date_add(mydate, interval 92 days), 'FY%Y') as FY, site, count(*)
 from mytable
 group by FY, site;

十月,十一月和十二月有92天,所以我抵消了那么多。