简体   繁体   English

如何从表格中选择唯一年份

[英]How select unique years from table

I have rows in a table in SQL Server 2008 我在SQL Server 2008中的表中有行

行

Tell me please how select only unique years from table? 请告诉我如何从表格中仅选择唯一年份?

PS: in this table unique year is 2013 PS:在此表中,唯一年份是2013年

Use the YEAR function, with DISTINCT like this: YEAR函数与DISTINCT一起使用,如下所示:

SELECT DISTINCT YEAR([date])
FROM Tablename;

SQL Fiddle Demo SQL小提琴演示

This will give you: 这将为您提供:

| YEAR |
--------
| 2013 |

To use the order by clause, give it an alias and order by this alias not the original name like this: 要使用order by子句,请给它一个别名,并按此别名而不是像这样的原始名称来排序:

SELECT DISTINCT YEAR([date]) AS Year
FROM Tablename
ORDER By Year;

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM