deanmachine333
asked on
Create DateTime table with Grouped minutes columns - SQL
Hello,
Im trying to create a SQL table that has a datetime (date , hour and minute only) column starting from say 2017-11-17T00:00 and columns with heading say 30min , 45min, 77min, 240min, 343min which is basically grouping the datetime column in those specific minutes .
But the groupings only happen between 00:00 and 23:59 each day , and then starts the grouping again at 00:00 onwards .
have spreadsheet with starting what it should look like , would like to have about 3 years datetime from 2017-11-17 please :-)
datetimeGrouped.xlsx
Im trying to create a SQL table that has a datetime (date , hour and minute only) column starting from say 2017-11-17T00:00 and columns with heading say 30min , 45min, 77min, 240min, 343min which is basically grouping the datetime column in those specific minutes .
But the groupings only happen between 00:00 and 23:59 each day , and then starts the grouping again at 00:00 onwards .
have spreadsheet with starting what it should look like , would like to have about 3 years datetime from 2017-11-17 please :-)
datetimeGrouped.xlsx
ASKER
Hello , i have ran that and it only gives me the date column , was looking for the others columns as well , that was in the spreadsheet? did you see it by any chance?
Working on the other columns. :)
SOLUTION
membership
This solution is only available to members.
To access this solution, you must be a member of Experts Exchange.
ASKER
ah thanks , also did you see that using the 77 and 240 and 343 minutes they wont be grouped evenly , i want it grouped each day for those minutes so if say there 5 groups of 343minutes in 00:00 - 23:59 the last goup in that might be 320 , i would like the next grouping for new day start from 00:00
that make sense?
thanks again
that make sense?
thanks again
ASKER
@Pawan, thanks for your awesome help , I have another question after seeing the results from the script, I have seen that the row number reset after each day which is perfect thanks, but I have noticed that I have missing minutes ( rows ) which return as null, is there a way to basically return last row value up until the next row that has data.
I have attached spreadsheet that shows the results of the script which has missing rows and the next to it the end results would like to achieve.
Are you able to advise please?
thanks
PreviousPrice.xlsx
I have attached spreadsheet that shows the results of the script which has missing rows and the next to it the end results would like to achieve.
Are you able to advise please?
thanks
PreviousPrice.xlsx
>>Are you able to advise please?
yes we can do that
SELECT othercolumns,
CASE WHEN marketname IS NULL THEN LAG(marketname) OVER (PARTITION BY CAST(DateKey AS DATE) ORDER BY DateKey) ELSE marketname END marketname
,CASE WHEN ClosedPrice IS NULL THEN LAG(ClosedPrice) OVER (PARTITION BY CAST(DateKey AS DATE) ORDER BY DateKey) ELSE ClosedPrice END ClosedPrice
From
(
/*Put your existing select here */
)r
yes we can do that
SELECT othercolumns,
CASE WHEN marketname IS NULL THEN LAG(marketname) OVER (PARTITION BY CAST(DateKey AS DATE) ORDER BY DateKey) ELSE marketname END marketname
,CASE WHEN ClosedPrice IS NULL THEN LAG(ClosedPrice) OVER (PARTITION BY CAST(DateKey AS DATE) ORDER BY DateKey) ELSE ClosedPrice END ClosedPrice
From
(
/*Put your existing select here */
)r
ASKER
Hello , thanks for script it seems to work for 1 row , but not when there is more than 1 row missing - see attached.
its on sheet 2 :-)
thanks
PreviousPrice.xlsx
its on sheet 2 :-)
thanks
PreviousPrice.xlsx
ASKER CERTIFIED SOLUTION
membership
This solution is only available to members.
To access this solution, you must be a member of Experts Exchange.
Question abandoned.
Provided solution
Provided solution
Open in new window