我正在尝试将两个结果列合二为一。 我的疑问是:
SELECT Datepart(Year, Date) As Annee, Datepart(Quarter, Date) AS Trimestre,
Sum(CASE WHEN [Type] = 'A' THEN 1 ELSE 0 END) A,
Sum(CASE WHEN [Type] = 'B' THEN 1 ELSE 0 END) B
FROM myTable
WHERE Date between '20150401' and '20160930'
GROUP BY Datepart(Year, Date), DATEPART(Quarter, Date)
ORDER BY 1,2
我想得到第一列如下结果:
| Period | A | B |
2015 - 2
2015 - 3
2015 - 4
...
我尝试使用“选择Concat(Datepart(年份,日期),Datepart(季度,日期)”以及其他一些不起作用的东西。我想做的甚至可能吗?
答案 0 :(得分:3)
Datepart返回一个数字。在连接之前将其转换为字符串。
select convert(varchar(4),datepart(year, getdate())) + ' - ' + convert(varchar(2),datepart(month, getdate()))
答案 1 :(得分:1)
Ue datename()
:
SELECT datename(Year, Date) + ' - ' + datename(Quarter, Date) as Trimestre,
Sum(CASE WHEN [Type] = 'A' THEN 1 ELSE 0 END) as A,
Sum(CASE WHEN [Type] = 'B' THEN 1 ELSE 0 END) as B
FROM myTable
WHERE Date between '20150401' and '20160930'
GROUP BY datename(Year, Date) + ' - ' + datename(Quarter, Date)
ORDER BY 1, 2;
这会返回一个字符串,因此您不必担心转换。
答案 2 :(得分:0)
SELECT
CONCAT(CONVERT(VARCHAR(4),
Datepart(Year,Date)), ' - ',
CONVERT(VARCHAR(1),
Datepart(Quarter, Date))), ...