簡體   English   中英

在SQL Server中查找季度

[英]Finding Quarter of year in SQL Server

我有一個包含銀行財務信息的數據庫。

這是表結構。 為了簡單起見,我將Assets int。

create table dbo.BankInfo
(
     id int, 
     asofdate date, 
     Assets int
)

insert into dbo.BankInfo Values(1,'2018-01-31',100)
insert into dbo.BankInfo Values(1,'2017-10-31',200)
insert into dbo.BankInfo Values(1,'2017-07-31',300)
insert into dbo.BankInfo Values(1,'2017-04-30',400)
insert into dbo.BankInfo Values(1,'2017-01-31',40)
insert into dbo.BankInfo Values(1,'2016-10-31',20)

insert into dbo.BankInfo Values(2,'2016-12-31 00:00:00',100)
insert into dbo.BankInfo Values(2,'2017-03-31 00:00:00',200)
insert into dbo.BankInfo Values(2,'2017-06-30 00:00:00',300)
insert into dbo.BankInfo Values(2,'2017-09-30 00:00:00',400)
insert into dbo.BankInfo Values(2,'2017-12-31 00:00:00',300)
insert into dbo.BankInfo Values(2,'2016-03-31 00:00:00',400)

我有另一個表,其中包含每個銀行的財政年末日期的信息

create table dbo.yearenddate
(
     id int, 
     enddate date
)

insert into dbo.yearenddate values(1,'2018-01-31  00:00:00')
insert into dbo.yearenddate values(2,'2017-06-30 00:00:00')

我想看看dbo.BankInfo ,獲取每個銀行的Fiscalyearenddate並設置Quarter(Qtr = 4),其他人遵循相同的模式

這就是輸出的樣子。 我無法按等級進行此操作。

create table dbo.outputqtr
(
     id int, 
     asofdate date, 
     Assets int, 
     qtr smallint
)

insert into dbo.outputqtr Values(1,'2018-01-31',100,4)
insert into dbo.outputqtr Values(1,'2017-10-31',200,3)
insert into dbo.outputqtr Values(1,'2017-07-31',300,2)
insert into dbo.outputqtr Values(1,'2017-04-30',400,1)
insert into dbo.outputqtr Values(1,'2017-01-31',40,4)
insert into dbo.outputqtr Values(1,'2016-10-31',20,3)

insert into dbo.outputqtr Values(2,'2016-12-31 00:00:00',100,2)
insert into dbo.outputqtr Values(2,'2017-03-31 00:00:00',200,3)
insert into dbo.outputqtr Values(2,'2017-06-30 00:00:00',300,4)
insert into dbo.outputqtr Values(2,'2017-09-30 00:00:00',400,1)
insert into dbo.outputqtr Values(2,'2017-12-31 00:00:00',300,2)
insert into dbo.outputqtr Values(2,'2016-03-31 00:00:00',400,3)

我的查詢是

SELECT 
    *,
    CASE 
       WHEN qtrr = 4 THEN 4
       ELSE DENSE_RANK() OVER(PARTITION BY ID, MONTH(asofdate)
                              ORDER BY asofdate DESC) 
    END AS qtr
FROM
    (SELECT
         t.*, y.enddate,
         CASE 
            WHEN AsofDate = enddate THEN 4
            ELSE 1
         END AS qtrr
     FROM
         dbo.BankInfo t
     LEFT JOIN
         dbo.yearenddate y ON y.id = t.id AND t.asofdate = y.enddate) t

任何幫助表示贊賞

看起來您所需要的就是從yearenddate表中的錨定日期算起的月數,然后將該數字轉換為季度。

檢查以下公式的中間結果以了解其工作原理。

樣本數據

DECLARE @BankInfo TABLE
(
    id int, 
    asofdate date, 
    Assets int
);

insert into @BankInfo Values(1,'2018-01-31',100)
insert into @BankInfo Values(1,'2017-10-31',200)
insert into @BankInfo Values(1,'2017-07-31',300)
insert into @BankInfo Values(1,'2017-04-30',400)
insert into @BankInfo Values(1,'2017-01-31',40)
insert into @BankInfo Values(1,'2016-10-31',20)

insert into @BankInfo Values(2,'2016-12-31',100)
insert into @BankInfo Values(2,'2017-03-31',200)
insert into @BankInfo Values(2,'2017-06-30',300)
insert into @BankInfo Values(2,'2017-09-30',400)
insert into @BankInfo Values(2,'2017-12-31',300)
insert into @BankInfo Values(2,'2016-03-31',400)

DECLARE @yearenddate TABLE
(
    id int, 
    enddate date
)

insert into @yearenddate values(1,'2018-01-31')
insert into @yearenddate values(2,'2017-06-30')

詢問

SELECT
    B.id
    ,B.asofdate
    ,B.Assets
    ,(DATEDIFF(month, EOY.enddate, B.asofdate) / 3 + 399) % 4 + 1 AS qtr
FROM
    @BankInfo AS B
    INNER JOIN @yearenddate AS EOY ON B.id = EOY.id
;

結果

+----+------------+--------+-----+
| id |  asofdate  | Assets | qtr |
+----+------------+--------+-----+
|  1 | 2018-01-31 |    100 |   4 |
|  1 | 2017-10-31 |    200 |   3 |
|  1 | 2017-07-31 |    300 |   2 |
|  1 | 2017-04-30 |    400 |   1 |
|  1 | 2017-01-31 |     40 |   4 |
|  1 | 2016-10-31 |     20 |   3 |
|  2 | 2016-12-31 |    100 |   2 |
|  2 | 2017-03-31 |    200 |   3 |
|  2 | 2017-06-30 |    300 |   4 |
|  2 | 2017-09-30 |    400 |   1 |
|  2 | 2017-12-31 |    300 |   2 |
|  2 | 2016-03-31 |    400 |   3 |
+----+------------+--------+-----+

下面的查詢應該給您預期的結果。

;with CTE AS 
     (
         SELECT t.*, y.enddate,
        datediff(MONTH, AsofDate,   enddate)%12 QTRY                              
         FROM dbo.BankInfo t   CROSS APPLY  dbo.yearenddate y 
         where y.id = t.id
     )
     select ID,AsOfDate,Assets, CASE WHEN QTRY=0 THEN 4 WHEN QTRY<0 THEN ABS(QTRY)/3 ELSE 4-QTRY/3 END AS QTR
     FROM CTE

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM