Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How to do a Select in another Select with Postgresql

I must do this query in another Select in Postgresql :

SELECT COUNT(tn.autoship_box_transaction_id) 
FROM memberships.autoship_box_transaction tn 
WHERE tn.autoship_box_id = b.autoship_box_id

Do I must use the clause WITH ?

like image 901
Julie Levesque Avatar asked Jul 18 '18 14:07

Julie Levesque


People also ask

How do I create a sub query in PostgreSQL?

Subqueries must be enclosed within parentheses. A subquery can have only one column in the SELECT clause, unless multiple columns are in the main query for the subquery to compare its selected columns. An ORDER BY cannot be used in a subquery, although the main query can use an ORDER BY.

How do I use two selected PostgreSQL statements?

The UNION operator combines result sets of two or more SELECT statements into a single result set. To combine the result sets of two queries using the UNION operator, the queries must conform to the following rules: The number and the order of the columns in the select list of both queries must be the same.

Can we use subquery in SELECT clause?

You can use subqueries in SELECT, INSERT, UPDATE, and DELETE statements wherever expressions are allowed. For instance, you can use a subquery as one of the column expressions in a SELECT list or as a table expression in the FROM clause. A DML statement that includes a subquery is referred to as the outer query.


1 Answers

As long as the query produces a single data element, you can use it in place of an attribute:

SELECT (
          SELECT COUNT(tn.autoship_box_transaction_id) 
            FROM memberships.autoship_box_transaction tn 
           WHERE tn.autoship_box_id = b.autoship_box_id
       ) AS cnt
     , other_column
  FROM wherever
     ;

Have a look at this SQL fiddle demonstrating the use case.

This method often comes with a performance penalty if the db engine actually iterates over the result set and performs the query on each record encountered.

The db engine's optimizer may be smart enough to avoid the extra cost (and it should in the fiddle's toy example), but you have to look at the explain plan to be sure.

Note that its mostly an issue with 'correlated subqueries', ie. queries embedded as shown which depend on the embedding. Your example example appears to be of this kind as you use a table alias b which isn't defined anywhere.

There might be the option of moving the subselect to the from clause (beware: This statement is for explanatory purposes only; you must adapt it to your use case, I am just wild guessing here):

SELECT stats.cnt
     , b.other_column
  FROM b_table b
  JOIN (
          SELECT COUNT(tn.autoship_box_transaction_id) cnt
               , tn.autoship_box_id
            FROM memberships.autoship_box_transaction tn 
        GROUP BY tn.autoship_box_id
       ) stats
    ON (stats.autoship_box_id = b.autoship_box_id)
     ;
like image 70
collapsar Avatar answered Oct 21 '22 12:10

collapsar