Skip to content

Commit

Permalink
Fix sort in the query to keep linear index
Browse files Browse the repository at this point in the history
  • Loading branch information
datejada committed Feb 5, 2025
1 parent 17c4e3b commit dddbf16
Showing 1 changed file with 49 additions and 23 deletions.
72 changes: 49 additions & 23 deletions src/variables/create.jl
Original file line number Diff line number Diff line change
Expand Up @@ -88,37 +88,63 @@ function _create_variables_tables(connection)
connection,
"CREATE OR REPLACE TEMP SEQUENCE id START 1;
CREATE OR REPLACE TABLE var_storage_level_rep_period AS
WITH filtered_data AS (
SELECT
t_low.asset,
t_low.year,
t_low.rep_period,
t_low.time_block_start,
t_low.time_block_end
FROM t_lowest_all AS t_low
LEFT JOIN asset
ON t_low.asset = asset.asset
WHERE
asset.type = 'storage'
AND asset.is_seasonal = false
ORDER BY
t_low.asset,
t_low.year,
t_low.rep_period,
t_low.time_block_start
)
SELECT
nextval('id') as index,
t_low.asset,
t_low.year,
t_low.rep_period,
t_low.time_block_start,
t_low.time_block_end
FROM t_lowest_all AS t_low
LEFT JOIN asset
ON t_low.asset = asset.asset
WHERE
asset.type = 'storage'
AND asset.is_seasonal = false;
nextval('id') AS index,
asset,
year,
rep_period,
time_block_start,
time_block_end
FROM filtered_data
",
)

DuckDB.query(
connection,
"CREATE OR REPLACE TEMP SEQUENCE id START 1;
CREATE OR REPLACE TABLE var_storage_level_over_clustered_year AS
CREATE OR REPLACE TABLE var_storage_level_over_clustered_year AS
WITH filtered_data AS (
SELECT
attr.asset,
attr.year,
attr.period_block_start,
attr.period_block_end
FROM asset_timeframe_time_resolution AS attr
LEFT JOIN asset
ON attr.asset = asset.asset
WHERE
asset.type = 'storage'
ORDER BY
attr.asset,
attr.year,
attr.period_block_start
)
SELECT
nextval('id') as index,
asset.asset,
attr.year,
attr.period_block_start,
attr.period_block_end,
FROM asset_timeframe_time_resolution AS attr
LEFT JOIN asset
ON attr.asset = asset.asset
WHERE
asset.type = 'storage'
nextval('id') AS index,
asset,
year,
period_block_start,
period_block_end
FROM filtered_data
",
)

Expand Down

0 comments on commit dddbf16

Please sign in to comment.