SQL编辑年

SQL编辑年,sql,sql-server,Sql,Sql Server,该查询有效,但只给出年份值。如何添加1985-2014年的无限年数 use baseball; SELECT CAST(tf.franchname AS CHAR(20)), s.yearID, s.lgid, AVG(s.salary) FROM salaries s, teams t, teamsfranchises tf WHERE s.teamID = t.teamID AND t.franchID = tf.franchID AND s.yearID = 1985 AND (

该查询有效,但只给出年份值。如何添加1985-2014年的无限年数

use baseball; 

SELECT CAST(tf.franchname AS CHAR(20)), s.yearID, s.lgid, AVG(s.salary) 
FROM salaries s, teams t, teamsfranchises tf 
WHERE s.teamID = t.teamID AND
t.franchID = tf.franchID AND 
s.yearID = 1985 AND
(s.lgid='AL' OR s.lgid='NL') GROUP BY tf.franchname,  s.yearID, s.lgid order BY
s.yearID;
你可以用它

您的where子句应该如下所示

(s.yearID BETWEEN 1985 AND 2014) and
或者,您可以使用以下运算符:

(s.yearID >= 1984 and <= 2014)
你可以用它

您的where子句应该如下所示

(s.yearID BETWEEN 1985 AND 2014) and
或者,您可以使用以下运算符:

(s.yearID >= 1984 and <= 2014)

您的查询对年份和s.yearID=1985进行条件筛选,您可能希望使用关键字BETWEEN对其进行更改,或根据需要将其完全删除。

您的查询对年份和s.yearID=1985进行条件筛选,您可能希望使用关键字“介于”或“完全删除”来更改它,具体取决于您的需要。

这是另一种视图,当没有数据时,您仍然希望获得零计数的年份。看看这个

在这里,您可以创建一个临时表,返回您的年份列表,即1985年至2015年,然后只需使用左外连接进行连接,即可看到神奇之处

我刚收到您的查询,您也可以用接受的答案替换查询

Declare @Startyear int = 1985
--1st approach to get continues year
;with yearlist as 
(
    select 1985 as year
    union all
    select yl.year + 1 as year
    from yearlist yl
    where yl.year + 1 <= YEAR(GetDate())
)

select year from yearlist order by year desc;

--2nd approach to get continues year
;WITH n(n) AS
(
    SELECT 0
    UNION ALL
    SELECT n+1 FROM n WHERE n <  (year(getdate()) -@Startyear)
)
SELECT year(DATEADD( YY, -n, GetDate())) 
FROM n ORDER BY n


--take anyone approach and then join with your query 
;with yearlist as 
(
    select 1985 as year
    union all
    select yl.year + 1 as year
    from yearlist yl
    where yl.year + 1 <= YEAR(GetDate())
)

select year from yearlist 
left join
(
    SELECT CAST(tf.franchname AS CHAR(20)), s.yearID, s.lgid, AVG(s.salary) 
    FROM salaries s, teams t, teamsfranchises tf 
    WHERE s.teamID = t.teamID AND
    t.franchID = tf.franchID AND 
    s.yearID = 1985 AND
    (s.lgid='AL' OR s.lgid='NL') GROUP BY tf.franchname,  s.yearID, s.lgid order BY
    s.yearID
) yourtable on yourtable.yearID  = yearlist.year
order by year desc;

这是另一个视图,当没有数据时,仍然希望得到零计数的年份。看看这个

在这里,您可以创建一个临时表,返回您的年份列表,即1985年至2015年,然后只需使用左外连接进行连接,即可看到神奇之处

我刚收到您的查询,您也可以用接受的答案替换查询

Declare @Startyear int = 1985
--1st approach to get continues year
;with yearlist as 
(
    select 1985 as year
    union all
    select yl.year + 1 as year
    from yearlist yl
    where yl.year + 1 <= YEAR(GetDate())
)

select year from yearlist order by year desc;

--2nd approach to get continues year
;WITH n(n) AS
(
    SELECT 0
    UNION ALL
    SELECT n+1 FROM n WHERE n <  (year(getdate()) -@Startyear)
)
SELECT year(DATEADD( YY, -n, GetDate())) 
FROM n ORDER BY n


--take anyone approach and then join with your query 
;with yearlist as 
(
    select 1985 as year
    union all
    select yl.year + 1 as year
    from yearlist yl
    where yl.year + 1 <= YEAR(GetDate())
)

select year from yearlist 
left join
(
    SELECT CAST(tf.franchname AS CHAR(20)), s.yearID, s.lgid, AVG(s.salary) 
    FROM salaries s, teams t, teamsfranchises tf 
    WHERE s.teamID = t.teamID AND
    t.franchID = tf.franchID AND 
    s.yearID = 1985 AND
    (s.lgid='AL' OR s.lgid='NL') GROUP BY tf.franchname,  s.yearID, s.lgid order BY
    s.yearID
) yourtable on yourtable.yearID  = yearlist.year
order by year desc;