Click here to Skip to main content
15,885,546 members
Please Sign up or sign in to vote.
0.00/5 (No votes)
See more:
For each zipcode, i want to display the count of bookings which is count of bookings = (Number of times the location is a pickup location) +
(Number of times the location is a final destination) +
(Number of times the location is an intermediate destination point) which i already union the select statements and group by. But the results shown are not correct, for each zipcode the result of the count is the same for all zipcode.


My code:
SQL
select l.zip_code,sum(dum.tab) from location l,
(select  count(  Pickup_location) as tab from booking b,location l where b.pickup_location = l.Zip_Code  group by l.zip_Code 
union select count( destination) as tab from booking_destinations bd inner join location l on bd.destination = l.Zip_Code where bd.sequence in(1,2,3) group by l.zip_Code 
union select count( destination) as tab from booking_destinations bd,location l where bd.destination = l.Zip_Code and sequence = 4   group by l.zip_Code 
 ) as dum group by l.zip_Code ;


What I have tried:

searching online and removing group by from inner select statement
Posted
Updated 25-Mar-16 13:16pm
v2

1 solution

SQL
SELECT  zip_code
       ,sum(tab)
FROM    (
    SELECT  Count(Pickup_location) AS tab
           ,l.zipcode
    FROM    booking b
    INNER JOIN location l
        ON  b.pickup_location = l.Zip_Code
    GROUP BY l.zip_Code
    UNION
    SELECT  Count(destination) AS tab
           ,l.zipcode
    FROM    booking_destinations bd
    INNER JOIN location l
        ON  bd.destination = l.Zip_Code
    WHERE   bd.SEQUENCE IN(1,2,3,4)
    GROUP BY l.zip_Code 
 ) as dum
GROUP BY zip_Code;
 
Share this answer
 
v2
Comments
Angel17033 25-Mar-16 22:39pm    
@Jörgen Andersson it has an error stating that theres a unknown column l.zipcode in fieldlist so still need to specify l.zipcode comes from location l?
Jörgen Andersson 26-Mar-16 4:14am    
Updated solution

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)



CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900