currysensei
currysensei

Reputation: 187

How can I define a query as a variable in sqlite3?

I was wondering if there was a way to save a query as a variable and use it later on, instead of dealing with inline subqueries. Is there a way to do this in SQLite3?

An example query of mine would be:

select Name
from (
    select Name, Count(*) c
    from branch
    group by Name
) f
where f.c = 1;

I would like to define the subquery f as a variable and then use it in this fashion, if possible:

select Name
from f
where f.c = 1;

Upvotes: 1

Views: 435

Answers (1)

radar
radar

Reputation: 13425

You can create in memory temp table and store the result of subquery and can use it later

  CREATE TEMP TABLE f(Name TEXT , NameCount INTEGER);

  INSERT into f 
  select Name, Count(*) c
  from branch
  group by Name;

  DROP TABLE if exists f; -- to clean up the temp table

Upvotes: 1

Related Questions