嗨,有人可以帮助我,我想创建一个带有月份参数的脚本,我要求的结果是接下来的12个月的名称作为列标题。
例如,如果参数为2月,则结果应为:
March | April | May | June | July | August | September | October | November | December | January | February |
作为我想用于在稍后阶段填写值的列名。 有人可以告诉我这个以及如何实现这个目标
到目前为止,我的脚本不知道如何处理这个
declare @MonthParam varchar(20) = 'March'
declare @MonthNumber int = (
Select CASE WHEN @MonthParam = 'January' THEN 1
WHEN @MonthParam = 'February' THEN 2
WHEN @MonthParam = 'March' THEN 3
WHEN @MonthParam = 'April' THEN 4
WHEN @MonthParam = 'May' THEN 5
WHEN @MonthParam = 'June' THEN 6
WHEN @MonthParam = 'July' THEN 7
WHEN @MonthParam = 'August' THEN 8
WHEN @MonthParam = 'September' THEN 9
WHEN @MonthParam = 'October' THEN 10
WHEN @MonthParam = 'November' THEN 11
WHEN @MonthParam = 'December' THEN 12
END)
WHILE(@MonthNumber < 13)
BEGIN
declare @month varchar(20) = DateName( month , DateAdd( month , @MonthNumber , 0 ) - 1 )
declare @sql varchar(max)
set @sql = 'select val as ' + @month + ' from t'
exec (@sql)
SET @MonthNumber = @MonthNumber + 1;
END
答案 0 :(得分:0)
declare @date_entered varchar(50) = 'February'
set @date_entered = @date_entered + ' 01 ' + CONVERT(char(4),year(getdate()))
declare @start_month datetime = @date_entered
declare @loop_month datetime = @start_month
declare @sql nvarchar(max)
set @sql = 'CREATE TABLE MyDates ('
WHILE @loop_month < DATEADD(year,1,@start_month)
BEGIN
set @sql += DATENAME(month,@loop_month) + ' varchar(100),'
set @loop_month = DATEADD(month,1,@loop_month)
END
-- Get rid of the comma at the end of the string and close the table paranthesis (edit was to the following bit)
set @sql = LEFT(@sql,len(@sql)-1) + ')'
print @sql
--Uncomment below to run
--exec(@sql)
答案 1 :(得分:0)
另外,你可以使用&#34;简化&#34;方法
declare @MonthParam varchar(20) = 'March'
declare @Dte date = CAST(concat(@MonthParam, ' 01 1900') AS DATE)
declare @sql varchar(max) = concat('select null as ', DATENAME(MONTH, DATEADD(MONTH, 1, @Dte)),
', null as ', DATENAME(MONTH, DATEADD(MONTH, 2, @Dte)),
',null as ',DATENAME(MONTH, DATEADD(MONTH, 3, @Dte)),
',null as ',DATENAME(MONTH, DATEADD(MONTH, 4, @Dte)),
',null as ',DATENAME(MONTH, DATEADD(MONTH, 5, @Dte)),
',null as ',DATENAME(MONTH, DATEADD(MONTH, 6, @Dte)),
',null as ',DATENAME(MONTH, DATEADD(MONTH, 7, @Dte)),
',null as ',DATENAME(MONTH, DATEADD(MONTH, 8, @Dte)),
',null as ',DATENAME(MONTH, DATEADD(MONTH, 9, @Dte)),
',null as ',DATENAME(MONTH, DATEADD(MONTH, 10, @Dte)),
',null as ',DATENAME(MONTH, DATEADD(MONTH, 11, @Dte)),
',null as ',DATENAME(MONTH, DATEADD(MONTH, 12, @Dte)))
exec (@sql)
答案 2 :(得分:0)
我使用了带有一些虚拟数据的临时表来将最终输出显示为表
DECLARE @PivotColumnHeaders VARCHAR(MAX);
DECLARE @date_entered varchar(50) = 'February';
SET @date_entered = @date_entered + ' 01 ' + CONVERT(CHAR(4),YEAR(GETDATE()));
DECLARE @s INT= 1, @e INT = 12;
CREATE TABLE PivotTemp (Dummy INT, [MonthName] VARCHAR(15));
WHILE ( @s <= @e)
BEGIN
SELECT @PivotColumnHeaders =
COALESCE(
@PivotColumnHeaders + ',[' + DATENAME(MONTH,DATEADD(MONTH, @s, @date_entered)) + ']',
'[' + DATENAME(MONTH,DATEADD(MONTH, @s, @date_entered))+ ']'
);
INSERT INTO PivotTemp ( Dummy, [MonthName] ) VALUES ( @s, DATENAME(MONTH,DATEADD(MONTH, @s, @date_entered)) );
SET @s = @s + 1;
END;
--SELECT @PivotColumnHeaders ;
--SELECT * FROM PivotTemp ;
DECLARE @PivotTableSQL NVARCHAR(MAX);
SET @PivotTableSQL = N'
SELECT *
FROM (
SELECT
H.Dummy [DummyValue],
H.[MonthName] [MonthName]
FROM dbo.PivotTemp H
) AS PivotData
PIVOT (
MAX(DummyValue)
FOR [MonthName] IN (
' + @PivotColumnHeaders + '
)
) AS PivotTable
';
EXECUTE(@PivotTableSQL);
DROP TABLE PivotTemp;