1

我有这些数据显示按年份分组的综合报告。我使用的是一个简单的 GROUP BY YEAR(BookingDate)子句,一切正常。但我想要的是财政年度(1 April31 May)而不是正常的日历年,那么我该怎么做呢?查询是..

SELECT 
    CONVERT(VARCHAR(25), c.Duration) AS Duration, 
    COALESCE(c.totalbookings, 0) AS totalbookings
FROM
    (SELECT 
               YEAR(bookingdate) AS Duration, 
               COUNT(*) AS totalbookings 
           FROM 
               entbookings
            WHERE BranchId=@Branchid
           GROUP BY YEAR(bookingdate)
    ) AS c 
LEFT OUTER JOIN 
    (SELECT 
               Duration, 
               SUM(totalitems) 'bkdqty'
           FROM 
               [yrItemWISeTotalQty] (@BranchId) AS BkdQty
           GROUP BY Duration
    ) AS d 
ON c.Duration = d.Duration

yrItemWISeTotalQty定义为

CREATE FUNCTION [dbo].[yrItemWiseTotalQty]
(
@BrachId VARCHAR(MAX)
) 

RETURNS TABLE AS
    RETURN
SELECT 
    YEAR(EntBookings.BookingDate) AS Duration, 
    Sum(dbo.EntBookings.ItemCost) AS totalcost,
WHERE   EntBookings.BranchId = @BrachId
GROUP BY Year(EntBookings.BookingDate)

同样,我使用GROUP BY Year()它的工作效果很好,但我需要对其进行修改以采用 fy ie(4 月 1 日至 3 月 31 日)。我该怎么做?

4

2 回答 2

1

试试这个:

SELECT 
    case when month(EntBookings.BookingDate) <4
         then YEAR(EntBookings.BookingDate)-1
         else YEAR(EntBookings.BookingDate) end  AS Duration, 
    Sum(dbo.EntBookings.ItemCost) AS totalcost
WHERE   EntBookings.BranchId = @BrachId
GROUP BY case when month(EntBookings.BookingDate) <4 
         then YEAR(EntBookings.BookingDate)-1 
         else YEAR(EntBookings.BookingDate) end as [year]
于 2012-08-28T12:36:10.840 回答
0

定义一个 SQL 函数
getFinancialYear(Data[TimeStamp] )
来返回财政年度编号并使用它来分组和。
IE

...
GROUP BY getFinancialYear(BookingDate)
...
于 2012-08-28T13:06:37.277 回答