SQL Query从两个表中获取匹配数据的计数

时间:2013-02-28 05:54:57

标签: sql ms-access-2007

我有两张桌子:

Travellers_Report

Name    Country Place_of_visit  Return_date
A   United States   Los Angeles 2/27/2013
B   United Arab Emirates    DUBAI   2/21/2013
C   Puerto Rico San Juan    2/28/2013
D   United States   Los Angeles 2/27/2013
E   United States   Boston  2/27/2013
F   Malaysia    Kulalampur  3/1/2013
G   United Arab Emirates    DUBAI   2/15/2013
H   Malaysia    Kulalampur  3/15/2013
I   United States   Los Angeles 2/27/2013

Company_presence

Country City
United States   Los Angeles
United Arab Emirates    DUBAI
United States   Boston

我需要将'Travellers_Report'表格的'Place_of_visit'字段与'Company_presence'表格的'City field'进行比较并获取计数。

预期输出为:

Country               Place_of_visit    Count
United States         Los Angeles       3
United Arab Emirates  DUBAI             2
United States         Boston            1

截至目前,我有两个问题,但我不知道如何将它们结合起来以获得预期的结果。

sqlstr = "SELECT * " & _
  "FROM Travellers_report " & _
  "WHERE Travellers_report.Return_Date >= #" & DOI & "# AND    
Travellers_report.Return_Date <=#" & tempd & "#;"

SELECT Travellers_report.Place_of_Visit, count(1) FROM Travellers_report 
GROUP BY Place_of_Visit

2 个答案:

答案 0 :(得分:0)

SELECT  a.Country, a.City, COUNT(b.Place_of_visit) totalCount
FROM    Company_Presence a
        LEFT JOIN Travellers_Report b
            ON  a.Country = b.Country AND
                a.City = b.Place_of_visit AND
                b.return_DATE BETWEEN #date1_HERE# AND #date2_HERE#
GROUP   BY a.Country, a.City

答案 1 :(得分:0)

@kiran尝试这个...
    SELECT c.country,c.city,count(*) from company_presence as c,Travellers_Report as t where t.city=c.city group by c.city;