默认基于月份值的年份

时间:2015-06-22 10:40:24

标签: sql sql-server

我有一个查询来显示表中的年份值并默认当前年份。

select distinct year(startdate) as syear, year(startdate) as eyear, 1 as sOrder from [TTS].[dbo].[Class] where year(startdate) <> year(getdate())
    union all
    select year(getdate()) as syear, year(getdate()) as eyear, 0 as sOrder
    order by sOrder asc, syear desc

enter image description here

此处默认年份为2015年(Sorder为0)。现在我需要根据月份值显示默认年份。例如,如果年份是2015年,月份是1月,1月,3月,那么默认年份是2015年。否则默认年份是2016年。我已经尝试过以下查询

select distinct year(startdate) as syear, year(startdate) as eyear, 1 as sOrder  from [TTS].[dbo].[Class]  where
 year(startdate) <> year(getdate())
union all
select year(getdate())+1 as syear, year(getdate())+1 as eyear, 0 as sOrder where month(getdate()) not in(1,2,3,4) 
union all
select year(getdate()) as syear, year(getdate()) as eyear, 0 as sOrder where month(getdate())in(1,2,3,4)

order by sOrder asc, syear desc

但年份价值正在重复。能帮到我吗?

3 个答案:

答案 0 :(得分:1)

SQL Server在正在执行的操作中是正确的,因为您要求返回额外的行,如果现在运行,则会返回“2016” 您的独特作品仅适用于您已完成的第一个选择,因此这些是您的选择:

1)使用cte's与区别

with subq1 (syear, eyear, sOrder) as (select year(getdate())+1 as syear, year(getdate())+1 as eyear, 0 as sOrder where month(getdate()) not in(1,2,3,4) 
union all
select year(getdate()) as syear, year(getdate()) as eyear, 0 as sOrder where month(getdate())in(1,2,3,4)),
subq2 as (select distinct year(startdate) as syear, year(startdate) as eyear, 1 as sOrder  from [TTS].[dbo].[Class])

Select distinct syear, eyear, sOrder
from subq1
UNION ALL
select syear, eyear, sOrder
from subq2 s2
where s2.syear <> (select syear from subq1)

order by sOrder asc, syear desc

可以在此处找到SQL小提琴:http://sqlfiddle.com/#!6/f89b3a/3/0

2)您可以尝试使用分组:

select distinct syear, eyear, min(sOrder)
from
(select distinct year(startdate) as syear, year(startdate) as eyear, 1 as sOrder 
from [TTS].[dbo].[Class]
union all
select year(getdate())+1 as syear, year(getdate())+1 as eyear, 0 as sOrder where month(getdate()) not in(1,2,3,4) 
union all
select year(getdate()) as syear, year(getdate()) as eyear, 0 as sOrder where month(getdate())in(1,2,3,4)) subq

Group by syear, eyear
order by min(sOrder) asc, syear desc

可以在此处找到SQL小提琴:http://sqlfiddle.com/#!6/f89b3a/5/0

我个人会建议选项1而不是选项2。

答案 1 :(得分:0)

替换你的代码(你试图找到当前年份的地方),下面的代码应该有用。

SELECT YEAR(DATEADD(MM,-4,GETDATE())) + 1

like 


SELECT DISTINCT year(startdate) AS syear
    ,year(startdate) AS eyear
    ,1 AS sOrder
FROM [TTS].[dbo].[Class]
WHERE year(startdate) <> year(getdate())

UNION ALL

SELECT YEAR(DATEADD(MM, - 4, GETDATE())) + 1 AS syear
    ,YEAR(DATEADD(MM, - 4, GETDATE())) + 1 AS eyear
    ,0 AS sOrder
ORDER BY sOrder ASC
    ,syear DESC

答案 2 :(得分:0)

您可以使用UNION代替UNION ALL来排除重复项:

SELECT  YEAR(startdate) AS syear ,
        YEAR(startdate) AS eyear ,
        1 AS sOrder
FROM    [TTS].[dbo].[Class]
WHERE   YEAR(startdate) <> YEAR(GETDATE())
UNION
SELECT  YEAR(GETDATE()) + 1 AS syear ,
        YEAR(GETDATE()) + 1 AS eyear ,
        0 AS sOrder
WHERE   MONTH(GETDATE()) NOT IN ( 1, 2, 3, 4 )
UNION
SELECT  YEAR(GETDATE()) AS syear ,
        YEAR(GETDATE()) AS eyear ,
        0 AS sOrder
WHERE   MONTH(GETDATE()) IN ( 1, 2, 3, 4 )
ORDER BY sOrder ASC ,
        syear DESC