0

我可能有十年没有使用 Access,所以如果这很简单,我很抱歉。我有三张桌子。表 1 - 用户信息 表 2 - 报告信息 表 3 - 更正信息。

表 2 和表 3 通过表 1 链接在一起。(表 2 使用表 1 的 id,表 3 使用表 1 的缩写名称)。

我需要一个查询,按日期计算每个用户的所有报告,并按日期按用户计算更正信息。

示例查询:

-用户-| -日期- | -表1计数- | -表2计数-

用户 1 | 2012 年 8 月 1 日 | 33 份报告 | 2 修正

用户 2 | 2012 年 8 月 1 日 | 4 份报告 | 0 修正

用户 1 | 2012 年 8 月 2 日 | 0 份报告 | 1 更正

(零表示未找到记录,但在另一个表中找到了该日期的记录)。

到目前为止,这是我对 SQL 的了解。

SELECT 
    tblUsers.userfullname, Count(txtReports.reportnumber) AS CountOfreportnumber,
    Count(tblRawData.reportnum) AS CountOfRawReport
FROM (tblUsers INNER JOIN txtReports ON tblUsers.userID = txtReports.userID) 
    INNER JOIN tblRawData ON tblUsers.userabbrev = tblRawData.username  
GROUP BY tblUsers.userfullname;

但它给我的只是部分匹配,每列中的总数相同。

任何指针?

更新:示例数据:tblUsers:

userID,userfullname,userabbrev
850,Test ML,test-ml
817,Test RL,test-rl
872,Test LS,test-ls
1071,Test MF,test-mf
934,Test TF,test-tf
796,Test JK,test-jk
1073,TEST RA,test-ra
779,TEST AW,test-aw
852,TEST EP,test-ep
987,TEST MM,test-mm
938,TEST SS,test-ss
19,TEST MT,test-mt
1095,TEST JL,test-jl
780,TEST LN,test-ln
909,TEST JR,test-jr
290,TEST BY,test-by
892,TEST JC,test-jc
920,TEST TR,test-tr
894,TEST PW,test-pw
873,TEST JR,test-jr
827,TEST AC,test-ac
783,TEST RP,test-rp
829,TEST AP,test-ap

txt报告

userID,reportdate,reportnumber
850,8/13/2012,823
817,8/13/2012,835
872,8/13/2012,841
872,8/13/2012,842
872,8/13/2012,850
1071,8/13/2012,862
934,8/13/2012,863
796,8/13/2012,800
796,8/13/2012,803
1073,8/13/2012,804
1073,8/13/2012,808
1073,8/13/2012,809
1073,8/13/2012,815
1073,8/13/2012,816
796,8/13/2012,817
779,8/13/2012,818
779,8/13/2012,819
779,8/13/2012,820
779,8/13/2012,821
779,8/13/2012,822
850,8/13/2012,824
850,8/13/2012,826
850,8/13/2012,827
852,8/13/2012,829
852,8/13/2012,830
850,8/13/2012,831
817,8/13/2012,832
817,8/13/2012,833
987,8/13/2012,834
1095,8/13/2012,836
850,8/13/2012,837
872,8/13/2012,839
852,8/13/2012,840
987,8/13/2012,843
852,8/13/2012,845
852,8/13/2012,846
850,8/13/2012,847
817,8/13/2012,848
938,8/13/2012,849
817,8/13/2012,851
850,8/13/2012,853
817,8/13/2012,854
19,8/13/2012,855
780,8/13/2012,856
780,8/13/2012,857
909,8/13/2012,858
1071,8/13/2012,859
19,8/13/2012,860
909,8/13/2012,861
290,8/13/2012,838
892,8/13/2012,844
987,8/13/2012,852
892,8/14/2012,864
872,8/14/2012,866
892,8/14/2012,869
872,8/14/2012,870
872,8/14/2012,871
872,8/14/2012,872
934,8/14/2012,880
850,8/14/2012,865
850,8/14/2012,867
850,8/14/2012,868
1095,8/14/2012,873
850,8/14/2012,874
817,8/14/2012,875
850,8/14/2012,876
1095,8/14/2012,877
852,8/14/2012,881
852,8/14/2012,882
1095,8/14/2012,885
920,8/14/2012,919
920,8/14/2012,922
19,8/14/2012,879
872,8/14/2012,878
894,8/15/2012,884
873,8/15/2012,886
827,8/15/2012,889
796,8/15/2012,891
796,8/15/2012,892
796,8/15/2012,893
852,8/15/2012,883
852,8/15/2012,887
852,8/15/2012,888
873,8/15/2012,890
783,8/15/2012,895
783,8/15/2012,896
852,8/15/2012,902
808,8/15/2012,904
872,8/15/2012,914
872,8/15/2012,915

tblRawData

reportnum,username,reportdate
832,test-rl,08/13/12
837,test-mf,08/13/12
797,test-jk,08/13/12
811,test-ra,08/13/12
838,test-by,08/13/12
739,test-ra,08/13/12
817,test-jk,08/13/12
818,test-aw,08/13/12
819,test-aw,08/13/12
820,test-aw,08/13/12
821,test-aw,08/13/12
822,test-aw,08/13/12
852,test-mm,08/14/12
857,test-ln,08/14/12
753,test-ap,08/14/12
868,test-mf,08/14/12
745,test-ra,08/14/12
765,test-ra,08/14/12
748,test-ra,08/14/12
766,test-ra,08/14/12
794,test-ra,08/14/12
807,test-ra,08/14/12
812,test-ra,08/14/12
814,test-ra,08/14/12
815,test-ra,08/14/12
767,test-ra,08/14/12
768,test-ra,08/14/12
804,test-ra,08/14/12
873,test-jl,08/14/12
875,test-rl,08/14/12
837,test-mf,08/15/12
881,test-ep,08/15/12
883,test-ep,08/15/12
498,test-jr,08/15/12
887,test-ep,08/15/12
890,test-jr,08/15/12

通过添加按日期过滤进行更新。

SELECT u.userfullname, nz(rd.rawcount,0) AS rawcount, nz(rp.rptcount,0) AS rptcount
FROM (tblUsers AS u LEFT JOIN (SELECT username, reportdate, count(reportnum) AS rawcount FROM tblRawData GROUP BY username, reportdate)  AS rd ON u.userabbrev = rd.username) LEFT JOIN (SELECT userID, reportdate, count(reportnumber) AS rptcount FROM txtReports GROUP BY userID, reportdate)  AS rp ON u.userID = rp.userID
WHERE (((rd.reportdate)>=[start date] And (rd.reportdate)<=[end date])) OR (((rp.reportdate)>=[start date] And (rp.reportdate)<=[end date]));
4

2 回答 2

1

尝试:

SELECT tblUsers.userfullname, tr.CountReport, td.CountRaw
FROM (tblUsers 
LEFT JOIN (
    SELECT r.userID, Count(r.reportnumber) AS CountReport
    FROM txtReports r
    GROUP BY r.userID)  AS tr 
ON tblUsers.userID = tr.userID) 
LEFT JOIN (
    SELECT tblRawData.username, Count(tblRawData.reportnum) AS CountRaw
    FROM tblRawData
    GROUP BY tblRawData.username)  AS td 
ON tblUsers.userabbrev = td.username
于 2012-09-13T17:29:19.807 回答
1

您需要获取每个表的计数,然后加入它们并显示它们,否则您将得到一团糟,并且您的计数都不正确。

SELECT u.userfullname, nz(rd.rawcount,0) AS rawcount, nz(rp.rptcount,0) AS rptcount 
FROM (tblUser AS u 
    LEFT JOIN (SELECT username, count(reportnum) AS rawcount FROM tblRawData GROUP BY username) AS rd 
    ON u.userabbrev = rd.username) 
    LEFT JOIN (SELECT userID, count(reportnumber) AS rptcount FROM txtReports GROUP BY userID) AS rp 
    ON u.userID = rp.userID
WHERE (Not p.Payments Is Null) OR (Not n.Notes Is Null);
于 2012-09-13T18:00:21.493 回答