du3979 2014-02-12 22:50
浏览 46

来自第三表的内部连接的两个表的SQL计数

My database has two similar tables with different names. One of the columns contained in both tables is called "zips", which contains a zip code.

I have a different table (US zip code table) where each entry contains a county name, county ID, state, state ID, and zip code.

I need a query that will tell me how many entries in the first two tables match a given county ID, and will group the results by county.

That is, lets say table A has 4 entries for zip code '30017', and table B has 1 entry for '30017'. In the US zip code table, '30017' corresponds to the county name "Gwinnett" and the county ID '839'. When I run the query the desired result is:

Gwinnett    5

Here is the latest query I've tried. I have no idea what numbers it's returning. It's definitely not what I'm expecting, as I've tried the individual component queries to test the results.

SELECT b.County, COUNT(*) as Calls FROM (aLeads a, pLeads p)
INNER JOIN zipCodes b ON a.zip = b.ZipCode
WHERE b.countyID IN (2897, 2146, 839) 
AND a.callDate BETWEEN '2013-10-01' AND '2013-11-30'
GROUP BY b.County

Any ideas?

  • 写回答

1条回答 默认 最新

  • dstk51319 2014-02-12 22:54
    关注

    Try starting with the zipcode table, and joining it with aLeads and pLeads:

    SELECT b.County, COUNT(*) as Calls 
    FROM zipCodes b 
    INNER JOIN aLeads a ON a.zip = b.ZipCode
    INNER JOIN pLeads p ON p.zip = b.ZipCode
    WHERE b.countyID IN (2897, 2146, 839) 
    GROUP BY b.County
    
    评论

报告相同问题?

悬赏问题

  • ¥15 msix packaging tool打包问题
  • ¥28 微信小程序开发页面布局没问题,真机调试的时候页面布局就乱了
  • ¥15 python的qt5界面
  • ¥15 无线电能传输系统MATLAB仿真问题
  • ¥50 如何用脚本实现输入法的热键设置
  • ¥20 我想使用一些网络协议或者部分协议也行,主要想实现类似于traceroute的一定步长内的路由拓扑功能
  • ¥30 深度学习,前后端连接
  • ¥15 孟德尔随机化结果不一致
  • ¥15 apm2.8飞控罗盘bad health,加速度计校准失败
  • ¥15 求解O-S方程的特征值问题给出边界层布拉休斯平行流的中性曲线