0

I saw the question Write a While loop in Impala SQL?, but this doesn't address the "for" keyword.

Is "for" not used for looping? And if not, what is it used for? Something similar to "UNIQUE FOR"?

1 Answers1

0

SQL is a different sort of language, it doesn't have that sort of looping/iterative capability.

Treating data as blocks and your query acting over every record at the same time is a good approach conceptually.

If you haven't used it before it worthwhile knowing a bit about set logic

shaine
  • 549
  • 5
  • 12
  • Thanks for the response. I understand set logic and SQL; I do not mean to loop like a CURSOR. I'm referring to partitioning a set by an iterated value, like a normalized invoice date column and looping month over month in batches. From a code maintainability/readability perspective, iterative approaches can be valuable instead of using lag/lead approaches, especially when lag/lead is a value you're joining on. Do you have an explanation of the Impala "for" keyword, by any chance? It looks like "while" is being introduced in the near future. – Christian DiMare Apr 11 '18 at 17:17
  • Also, T-SQL has WHILE capabilities. https://msdn.microsoft.com/en-us/library/ms178642(v=sql.100).aspx – Christian DiMare Apr 11 '18 at 18:00
  • Group by would be the obvious answer with a case statement. For each of the normalised columns. My point was you can use a view or a plain select to create batches but record level processing in my view isn't a good choice in sql. If you are processing in batches, use the wrapper script you are using for execution to do batch control. – shaine Apr 12 '18 at 08:14
  • Remember that impala and hive aren't full sql, they are sql like – shaine Apr 12 '18 at 08:15