在SQL Server中查找一年中的四分之一

时间:2018-05-16 23:38:39

标签: sql sql-server sql-server-2008

我有一个包含银行财务信息的数据库。

这是表格结构。为了简单起见,我做了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

感谢任何帮助

2 个答案:

答案 0 :(得分:2)

看起来您只需要从public static void main(String[] arg) { summ(); } public static void summ() { int i = 0; long startTime = System.currentTimeMillis()/1000; int timeExec = 2; while( ( System.currentTimeMillis() / 1000 ) - startTime < timeExec){ i++; } System.out.println(i); } 表中计算锚点日期的月数,然后将该数字转换为四分之一。

检查下面公式的中间结果,了解它的工作原理。

示例数据

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
;

答案 1 :(得分:0)

下面的查询应该会给你预期的结果。

alphabet = 'ABCDEFGHIJKLMNOPQRSTUVWXYZ'

parts, key = [int(s) for s in input().split()]
results = []

for part in range(parts):
    characters = input()
    for character in enumerate(characters):
        letter_idx = alphabet.index(character)
        if letter_idx > -1:
            # this is a letter in the alphabet, shift it with the key
            character = alphabet[letter_idx - key]
        results.append(character)

print("".join(results))