Sub-Query not Aggregating in Select

Ayesha Shahid

I have this select statement:

SELECT p.WPD, count(b.PID) as ParcelsPerBlck, 
(SELECT count(PID) 
FROM bos05eb 
WHERE ptype BETWEEN 009 AND 133) as ResdOnly 
FROM ebos_parcels05 p, bos05eb b 
WHERE p.PID_LONG = b.PID
GROUP BY p.WPD;

I want the results of the sub-query in the select statement to also group by p.WPD. But I get the result of the query in each row as follows:

WPD           ParcelsPerBlck    ResdOnly
01-02-030     38                6198
01-08-035     56                6198    
01-04-060     11                6198

and so on.

Can the results of a sub-query not be grouped so that I only get the result count of the sub-query ResdOnly for each row?

sgeddes

You can use conditional aggregation for this without the need for a correlated subquery:

select 
    p.WPD, 
    count(b.PID) as ParcelsPerBlck, 
    sum(case when b.ptype between 009 AND 133 then 1 else 0 end) as ResdOnly 
from 
    ebos_parcels05 p
join 
    bos05eb b on p.PID_LONG = b.PID
group by 
    p.WPD

Please note the usage of the explicit join syntax -- no need to use commas in the from clause.

Collected from the Internet

Please contact [email protected] to delete if infringement.

edited at
0

Comments

0 comments
Login to comment

Related

From Dev

Sql select sub query with count

From Dev

SQL Select within Sub Query

From Dev

SQL Server Sub query in select insert

From Dev

correlated sub query to select ids of earliest entry

From Dev

DELETE with SELECT sub-query gets stuck

From Dev

Sub query in t sql select statement

From Dev

SQL Query to select all sub folders

From Dev

Sub select SQL query using where and groupby

From Dev

SQL Query to select all sub folders

From Dev

How to rewrite a sub query in a select clause as a CTE

From Dev

PostgreSQL; Using a SELECT sub-query in an INSERT

From Dev

Sub select SQL query using where and groupby

From Dev

Reference select sub-query field

From Dev

SELECT IN with empty sub query, gives weird result

From Dev

Oracle select column as Sub query with case statement

From Dev

optimize Select sub query using alias

From Dev

MySQL: improving efficiency of sub-select query

From Dev

Aggregating results from SPARQL query

From Dev

how to use select query in a Group_concat sub query in mysql

From Dev

Select more than one row inside sub query or evade sub query

From Dev

Using sub-query to SELECT MAX value along with LEFT JOIN

From Dev

SQL return exactly one row or null in a select sub-query

From Dev

Select multiple results from sub query into single row (as array datatype)

From Dev

Query with broken sub-select should result in error but returns rows

From Dev

MySQL SQL Select Query with Multiple Sub-Selects

From Dev

Hive star schema query with 'SELECT *' in sub-selects

From Dev

MySQL Entity Framework Wraps query into sub-select for Order By

From Dev

SELECT inner Join Group by sub query extract in a single row

From Dev

MySQL: Select MAX() from sub-query with COUNT()

Related Related

HotTag

Archive