db2 - How can I create a temporary numbers table with SQL? -
so came upon question asked list of unused account numbers. query wrote works, kind of hacky , relies on existence of table more records existing accounts:
with tmp (select row_number() over( order cusno) custtable fetch first 999999 rows only) select tmp.a tmp not in (select cusno custtable)
this works because customer numbers reused , there more records unique customer numbers. but, said, feels hacky , i'd generate temporary table 1 column , x records numbered 1 through x. looked @ recursive solutions, of looked way more involved solution wound using. there easier way doesn't rely on existing tables?
i think simple answer no. able make determination of absence, platform needs know expected data set. can either generate temporary table or data set @ runtime - using method you've used (or variation thereof) - or can create reference table once, , compare against each time. i'd favour latter - table single column of integers won't put of dent in disk space , doesn't make sense compute identical result set on , on again.
here's article aaron bertrand deals issue:
https://sqlperformance.com/2013/01/t-sql-queries/generate-a-set-1
(edit: queries in article tsql specific, should adaptable db2 - , underlying analysis relevant regardless of platform)
Comments
Post a Comment