英文:
Snowflake Problem: Populate Table with Dates and its Attributes
问题
Here's the translation of the code part you provided:
我有一个问题陈述:
编写一个存储过程,用于填充一个带有特定日期属性的表格。数据将被填充一年。例如,将日期14-07-2020作为输入参数传递,然后存储过程将为2020年内所有日期填充这些属性。此表格的主键将是日期列。
约束:不能使用多个插入语句
表格的结构如图所示。
我针对这个问题陈述的Snowflake代码实现如下:
create or replace table DateTimeDimensions (SKDate varchar(50), KeyDate varchar(50), Date varchar(50), CalendarDay int, CalendarMonth int, CalendarQuarter int, CalendarYear int, DayNameLog varchar(50), DayNameShort varchar(50), DayNumOfWeek int, DayNumofYear int, DaySuffix varchar(10), FiscalWeek int, FiscalPeriod int, FiscalQuarter int, FiscalYear int, FiscalYear_Period int);
select * from DateTimeDimensions
truncate table DateTimeDimensions
drop table DateTimeDimensions
CREATE OR REPLACE PROCEDURE uspPopulateDateTimeTable(startDate TIMESTAMP)
RETURNS STRING
LANGUAGE JAVASCRIPT
AS
$$
{
var currentDate = startDate;
var endDate = DATEADD('YEAR', 1, startDate);
while (currentDate < endDate) {
var SKDate = currentDate.format('YYYYMMDD');
var KeyDate = currentDate.format('MM/DD/YYYY');
var Date = currentDate.format('MM/DD/YYYY');
var CalendarDay = currentDate.getDate();
var CalendarMonth = currentDate.getMonth() + 1;
var CalendarQuarter = Math.ceil(CalendarMonth / 3);
var CalendarYear = currentDate.getFullYear();
var DayNameLong = currentDate.toLocaleString('en-US', { weekday: 'long' });
var DayNameShort = currentDate.toLocaleString('en-US', { weekday: 'short' });
var DayNumberOfWeek = currentDate.getDay() + 1;
var DayNumberOfYear = Math.ceil((currentDate - new Date(currentDate.getFullYear(), 0, 1)) / 86400000);
var DaySuffix = CalendarDay + (CalendarDay % 10 === 1 && CalendarDay !== 11 ? 'st' : (CalendarDay % 10 === 2 && CalendarDay !== 12 ? 'nd' : (CalendarDay % 10 === 3 && CalendarDay !== 13 ? 'rd' : 'th')));
var FiscalWeek = currentDate.getWeek(5);
var FiscalPeriod = CalendarMonth;
var FiscalQuarter = CalendarQuarter;
var FiscalYear = CalendarYear;
var FiscalYear_Period = CalendarYear + padLeft(CalendarMonth.toString(), 2, '0');
var sqlStatement = 'INSERT INTO DateTimeDimensions (SKDate, KeyDate, Date, CalendarDay, CalendarMonth, CalendarQuarter, CalendarYear, DayNameLong, DayNameShort, DayNumberOfWeek, DayNumberOfYear, DaySuffix, FiscalWeek, FiscalPeriod, FiscalQuarter, FiscalYear, FiscalYear_Period) ' +
'VALUES (\'' + SKDate + '\', \'' + KeyDate + '\', \'' + Date + '\', ' + CalendarDay + ', ' + CalendarMonth + ', ' + CalendarQuarter + ', ' + CalendarYear + ', \'' + DayNameLong + '\', \'' + DayNameShort + '\', ' + DayNumberOfWeek + ', ' + DayNumberOfYear + ', \'' + DaySuffix + '\', ' + FiscalWeek + ', ' + FiscalPeriod + ', ' + FiscalQuarter + ', ' + FiscalYear + ', \'' + FiscalYear_Period + '\');';
snowflake.execute({ sqlText: sqlStatement });
currentDate = DATEADD('DAY', 1, currentDate);
}
return 'DateTimeDimensions表格成功填充。';
}
$$;
CALL uspPopulateDateTimeTable('2020-07-14 00:00:00');
I hope this helps. If you have any more questions or need further assistance, please feel free to ask.
英文:
I have a problem statement:
Write a Stored Procedure that populates a table with certain date attributes. The data would be populated for 1 year. For example the date 14-07-2020 is passed as an input parameter, then the stored procedure will populate those attributes for all the dates present within the year 2020. The primary key for this table would be date column.
Constraint: More than one insert statement cannot be used
The structure of table is given in the image.
My implementation of snowflake code for the problem statement is:
create or replace table DateTimeDimensions (SKDate varchar(50), KeyDate varchar(50), Date varchar(50), CalendarDay int, CalendarMonth int, CalendarQuarter int, CalendarYear int, DayNameLog varchar(50), DayNameShort varchar(50), DayNumOfWeek int, DayNumofYear int, DaySuffix varchar(10), FiscalWeek int, FiscalPeriod int, FiscalQuarter int, FiscalYear int, FiscalYear_Period int);
select * from DateTimeDimensions
truncate table DateTimeDimensions
drop table DateTimeDimensions
CREATE OR REPLACE PROCEDURE uspPopulateDateTimeTable(startDate TIMESTAMP)
RETURNS STRING
LANGUAGE JAVASCRIPT
AS
$$
{
var currentDate = startDate;
var endDate = DATEADD('YEAR', 1, startDate);
while (currentDate < endDate) {
var SKDate = currentDate.format('YYYYMMDD');
var KeyDate = currentDate.format('MM/DD/YYYY');
var Date = currentDate.format('MM/DD/YYYY');
var CalendarDay = currentDate.getDate();
var CalendarMonth = currentDate.getMonth() + 1;
var CalendarQuarter = Math.ceil(CalendarMonth / 3);
var CalendarYear = currentDate.getFullYear();
var DayNameLong = currentDate.toLocaleString('en-US', { weekday: 'long' });
var DayNameShort = currentDate.toLocaleString('en-US', { weekday: 'short' });
var DayNumberOfWeek = currentDate.getDay() + 1;
var DayNumberOfYear = Math.ceil((currentDate - new Date(currentDate.getFullYear(), 0, 1)) / 86400000);
var DaySuffix = CalendarDay + (CalendarDay % 10 === 1 && CalendarDay !== 11 ? 'st' : (CalendarDay % 10 === 2 && CalendarDay !== 12 ? 'nd' : (CalendarDay % 10 === 3 && CalendarDay !== 13 ? 'rd' : 'th')));
var FiscalWeek = currentDate.getWeek(5);
var FiscalPeriod = CalendarMonth;
var FiscalQuarter = CalendarQuarter;
var FiscalYear = CalendarYear;
var FiscalYear_Period = CalendarYear + padLeft(CalendarMonth.toString(), 2, '0');
var sqlStatement = 'INSERT INTO DateTimeDimensions (SKDate, KeyDate, Date, CalendarDay, CalendarMonth, CalendarQuarter, CalendarYear, DayNameLong, DayNameShort, DayNumberOfWeek, DayNumberOfYear, DaySuffix, FiscalWeek, FiscalPeriod, FiscalQuarter, FiscalYear, FiscalYear_Period) ' +
'VALUES (\'' + SKDate + '\', \'' + KeyDate + '\', \'' + Date + '\', ' + CalendarDay + ', ' + CalendarMonth + ', ' + CalendarQuarter + ', ' + CalendarYear + ', \'' + DayNameLong + '\', \'' + DayNameShort + '\', ' + DayNumberOfWeek + ', ' + DayNumberOfYear + ', \'' + DaySuffix + '\', ' + FiscalWeek + ', ' + FiscalPeriod + ', ' + FiscalQuarter + ', ' + FiscalYear + ', \'' + FiscalYear_Period + '\');';
snowflake.execute({ sqlText: sqlStatement });
currentDate = DATEADD('DAY', 1, currentDate);
}
return 'DateTimeDimensions table populated successfully.';
}
$$;
CALL uspPopulateDateTimeTable('2020-07-14 00:00:00');
But it is giving me error as:
JavaScript execution error: Uncaught ReferenceError: startDate is not defined in USPPOPULATEDATETIMETABLE at ' var currentDate = startDate;' position 22 stackstrace: USPPOPULATEDATETIMETABLE line: 3
Basically the error says that
var currentDate = startDate;
is not working because startDate is not defined.
I have searched online and read the documentation properly but can't find out the solution for this error. Help me in solving this.
I tried to search thoroughly through the Snowflake documentation for writing stored procedure using Javascript and SQL but it was of no help.
Again and Again same error was occuring.
I tried to implement using SQL sripting instead of Javascript but it gave even more error for the same logic.
I have not tried it using CTE because I think it will be more complicated.
答案1
得分: 1
创建或替换存储过程 uspPopulateDateTimeTable("startDate" TIMESTAMP)
返回 STRING
语言 JAVASCRIPT
AS
$$
{
var currentDate = startDate;
return currentDate;
}
$$
;
调用 uspPopulateDateTimeTable('2023-05-18');
这会正常工作。
英文:
CREATE OR REPLACE PROCEDURE uspPopulateDateTimeTable("startDate" TIMESTAMP)
RETURNS STRING
LANGUAGE JAVASCRIPT
AS
$$
{
var currentDate = startDate;
return currentDate;
}
$$
;
call uspPopulateDateTimeTable('2023-05-18');
this would work
答案2
得分: 0
你必须以大写字母指定存储过程参数的名称。
var currentDate = STARTDATE;
英文:
You have to specify the stored procedure argument names in uppercase.
var currentDate = STARTDATE;
通过集体智慧和协作来改善编程学习和解决问题的方式。致力于成为全球开发者共同参与的知识库,让每个人都能够通过互相帮助和分享经验来进步。
评论