Home > Mobile >  Use temporary table in query
Use temporary table in query

Time:11-26

Table contains multiple columns and multiple redundant rows which I don't need to work on. Let's say I selected columns and rows which I need to work on.

select column1, 
       column2, 
       column3 
from table 
where column1 > something 
and column2 == something;

Now how do I perform nested query on that selected data? I was thinking of doing something like.

 select column1, 
        sum(column2) from (
                           select column1, 
                                  column2, 
                                  column3 
                           from table 
                           where column1>something 
                           and column2 == something) 
group by column1;

And I am getting error. Any help would be appreciated

CodePudding user response:

I don't know if you really need the subquery or the column3 that you don't use in any condition , if you provide template datas and expected result would be better.

For your query to work you need an alias in the subquery, so it would be something like:

select t1.column1, 
        sum(t1.column2) from (
                           select column1, 
                                  column2, 
                                  column3 
                           from table 
                           where column1>something 
                           and column2 == something) as t1
group by t1.column1;
  • Related