我有一个包含date
的表和列(以字符串表示)。我需要从同一日期列中获取两列,例如countin(2020)
和countin(2019)
,以便日期类似于'2020%'或'2019%',我希望将它们作为单独的列。
我的查询有点像。
select C.pin_code, count(distinct(C.customer_code)) as 2020
from table
group by C.pin_code
我的输出是这个
对我来说,除了2020年以外,还有一列叫2019年,它提供的数据与2019年的2020年相同。
如果我强调不足,请在评论中让我知道。
Select
Count(Year(year_col)) as year,
Pin_code
From T
Group_by pin_code
Order by pin_code desc;
如果需要在年份列中使用这些值,则可以使用数据透视表
对我来说,除了2020年以外,还有一列叫2019年,它提供的数据与2019年的2020年相同。
如果您的数据中有日期,那么我希望这样的查询:
select C.pin_code,
count(distinct case when year(C.date) = 2020 then C.customer_code end) as cnt_2020,
count(distinct case when year(C.date) = 2019 then C.customer_code end) as cnt_2019
from C
group by C.pin_code;
日期/时间功能众所周知是依赖数据库的。但是year()
很常见,并且所有数据库都以某种方式具有此功能。