如何从嵌套的多个select语句中计算每个zipcode? [英] How to count for each zipcode from nested multiple select statement?

查看:104
本文介绍了如何从嵌套的多个select语句中计算每个zipcode?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

对于每个邮政编码,我想显示预订次数=预订次数=(位置是取货地点的次数)+

(地点是最终的次数)目的地)+

(位置是中间目标点的次数)我已经将select语句和group by联合起来。但显示的结果不正确,对于每个邮政编码,所有邮政编码的计数结果都相同。





我的代码:

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:

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

推荐答案

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;


这篇关于如何从嵌套的多个select语句中计算每个zipcode?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆