2011-11-16 52 views
-2

有人可以帮我重做这个查询,以便我可以获取outer_config.contractorsRef值。目前它没有找到价值。在代码中,我已经在下面高亮了。我有用countley写的查询与计数(*)和选择在一个子查询,因为它执行很快比计数(不同)。mysql查询 - 选择找不到值

SELECT outer_config.contractorsRef AS cref, outer_config.contractorsRef AS contractorsRef, noworkers 
FROM bis.request_config AS outer_config 
LEFT JOIN (
    SELECT request_config.contractorsRef, (
     SELECT COUNT(*) subcount 
     FROM (
      SELECT DISTINCT subcontractorRef 
      FROM bis.Request 
      INNER JOIN bis.request_config ON request_config.RIDGROUP = request.RIDGROUP 
      AND currenttaxyear =2011 
      AND weekno =33 
      AND contractorsRef=outer_config.contractorsRef ############ERROR HERE########### 
      GROUP BY contractorsRef 
     )x 
    )noworkers 
    FROM bis.Request 
    INNER JOIN bis.request_config ON request_config.RIDGROUP = request.RIDGROUP 
    AND currenttaxyear =2011 
    AND weekno =33 
)T1 ON T1.contractorsRef = outer_config.contractorsRef 
WHERE currenttaxyear =2011 
AND weekno =33 
AND outer_config.contractorsRef <>132 
GROUP BY outer_config.contractorsRef 

表防守

-

CREATE TABLE request_config (
    RIDGROUP int(11) NOT NULL AUTO_INCREMENT, 
    sessionstart text NOT NULL, 
    EmployeeID int(11) NOT NULL, 
    closedrequest tinyint(1) NOT NULL, 
    contractorsRef int(11) NOT NULL DEFAULT '0', 
    timesheetDateSubmited text, 
    requesttotal int(11) NOT NULL DEFAULT '0', 
    imported int(11) NOT NULL DEFAULT '0', 
    dateref text, 
    onlinespreadsheet int(11) NOT NULL DEFAULT '0', 
    marginamt double NOT NULL DEFAULT '0', 
    grossamt double NOT NULL DEFAULT '0', 
    feespaidbyclient int(11) NOT NULL DEFAULT '0', 
    currenttaxyear int(11) NOT NULL DEFAULT '0', 
    weekno int(11) NOT NULL DEFAULT '0', 
    subdedamt double NOT NULL DEFAULT '0', 
    timesheetfrequency int(11) NOT NULL DEFAULT '0', 
    onlinesubmission int(11) NOT NULL DEFAULT '0', 
    PRIMARY KEY (RIDGROUP), 
    KEY contractorsRef_2 (contractorsRef,currenttaxyear,weekno) 
) ENGINE=MyISAM DEFAULT CHARSET=latin1; 

CREATE TABLE request (
    RID int(11) NOT NULL, 
    RIDGROUP int(11) NOT NULL, 
    EmployeeID int(11) NOT NULL, 
    date_requested text NOT NULL, 
    hours double NOT NULL, 
    rate double NOT NULL, 
    agencydeduction double NOT NULL, 
    otherpay double NOT NULL, 
    totaltimesheet double NOT NULL, 
    subcontractorRef text NOT NULL, 
    candidatename text NOT NULL, 
    candidatename_sys text NOT NULL, 
    validated tinyint(1) NOT NULL DEFAULT '0', 
    requestclosed tinyint(1) NOT NULL DEFAULT '0', 
    paytypeID int(11) NOT NULL DEFAULT '0', 
    retrieved int(11) NOT NULL DEFAULT '0', 
    KEY RID (RID), 
    KEY RIDGROUP (RIDGROUP), 
    KEY subcontractorRef (subcontractorRef(20)) 
) ENGINE=MyISAM DEFAULT CHARSET=latin1; 

-

CREATE TABLE contractors (
    contractorsRef int(11) NOT NULL AUTO_INCREMENT, 
    contractorsName text, 
    contractName text, 
    TELEPHONE text, 
    MOBILE text, 
    FAX text, 
    contractorsAddress1 text, 
    contractorsAddress2 text, 
    contractorsAddress3 text, 
    contractorsAddress4 text, 
    contractorsAddress5 text, 
    contractorsAddresspostcode text, 
    emailaddress text, 
    websiteadd text, 
    Contractsent int(11) DEFAULT '0', 
    Contractreceived int(11) DEFAULT '0', 
    officeno int(11) DEFAULT '0', 
    clientID text, 
    jobtype int(11) DEFAULT '0', 
    weeknopaymentfilereceived int(11) DEFAULT '0', 
    timetogenerateemail text, 
    daytogenerateemail text, 
    weeknoremindersent int(11) DEFAULT '0', 
    weeknoremindersent_O2 int(11) DEFAULT '0', 
    disabledreminder int(11) DEFAULT '0', 
    active int(11) NOT NULL DEFAULT '0', 
    createdbyEmployeeID int(11) NOT NULL DEFAULT '0', 
    marginagreed double NOT NULL DEFAULT '0', 
    rebateagreed double NOT NULL DEFAULT '0', 
    marketing int(11) NOT NULL DEFAULT '0', 
    ARDENTORO2 int(11) NOT NULL DEFAULT '0', 
    www text, 
    clientID2 text, 
    attentionneeded int(11) NOT NULL DEFAULT '0', 
    UNREFCOUNTER int(11) NOT NULL DEFAULT '0', 
    feespaidbyclient int(11) NOT NULL DEFAULT '0', 
    request_manual_entry int(11) NOT NULL DEFAULT '0', 
    payupon int(11) NOT NULL DEFAULT '0', 
    weektostartreminder text, 
    reminder_duration int(11) NOT NULL DEFAULT '0', 
    dayofweekpaymentexpected int(11) NOT NULL DEFAULT '0', 
    Correspondence text, 
    timesheetfrequency int(11) NOT NULL DEFAULT '0', 
    atnc int(11) NOT NULL DEFAULT '0', 
    nextts_expected int(11) NOT NULL DEFAULT '0', 
    PRIMARY KEY (contractorsRef), 
    KEY clientID (clientID(8)) 
) ENGINE=InnoDB DEFAULT CHARSET=latin1; 
+0

请提供您的表格定义。 – Polynomial

+0

添加的定义 – user984314

回答

0

看来,你试图让每个承包商以下... 列表。从每个承包商那里得到他们合作了多少个不同的分包商。由此,承包商的所有分包商中的工人总数。如果这是正确的,下面的查询可能会更接近您的需求。

第一个“来自”查询只是查询承包商+分包商的合格条件,并按承包商+分组进行计数。

从这个结果中,得到承包商的查询,然后查询分包商的COUNT,SUM查看分包商的工人数量。

在你的Request_Config表上创建一个INDEX(CurrentTaxYear,WeekNo,ContractorsRef)。您希望索引代表寻找公共组的最小粒度。在这种情况下,要么是纳税年度/周(而不是周/纳税年度,否则会隔一个星期的所有年份,因此记录更多)。按年份+周编制索引,您只能获得那些特定于没有交错的时间段,如上所述。在这个集合内,通过跳过你不想要的一个承包商,它将轻而易举地轻而易举。

此外,为了帮助group by(内部查询),您可能需要RIDGROUP和SubContractorRef(20)上Request表上的另一个索引,以帮助保持它们在子结果集中的预先排序。

我也调整了内部查询后更好地审查您的表结构查询基于请求配置表第一和JOIN请求表第二。 “WHERE”子句直接应用于配置请求表(通过索引进行优化)。

​​

- 结果的另一种澄清 -

从你最后的评论......越来越重复计算 比方说你有如下记录。

Request_Config 
RIDGroup ContractorsRef 
1   111  Even by this example, if a contractor ref can be 
2   222  duplicated as a different "RIDGroup" and would appear 
3   333  two times. This COULD/WOULD cause duplications when 
4   111  joining to the Request table on just the ContractorsRef column 


Request file 
RID RIDGroup SubContractorRef 
52 1   SubA 
53 2   SubB 
54 3   SubC 
55 4   SubA 
56 1   SubA (second work/hours/pay request from same job?) 
57 4   SubA (second request for second job under same contractor)? 
58 2   SubD 
59 2   SubE 
60 1   SubF 

The INNER part of the query would return 
ContractorsRef SubContractorRef Count 
111    SubA    4 (as derived from 
              RIDGroup 1 having 2 entries and 
              RIDGroup 4 having 2 entries 
111    SubF    1 (since only against RIDGroup #1, (not #4) 
222    SubB    1 
222    SubD    1 
222    SubE    1 
333    SubC    1 

正如你所看到的,“苏巴”仍只出现一次的ContractorsRef 111,并显示该子4个独特的请求项...所以,外部查询它只能由ContractorsRef组将有

ContractorsRef SubContractors SubContRequests 
111    2    5 
222    2    2 
333    1    1 

(我更改了查询列的最终名称以正确反映总数的上下文)。

+0

这有效,但查询花费的时间比我预期的要长。任何其他的选择,thx? – user984314

+0

@ user984314,请参阅修订的答案和索引注释。这应该对性能有很大的帮助。 – DRapp

+0

不好意思,结果没有给我独特的subcontworkers。 1名工人可能在RIDGROUP下有超过1条记录。我想提出一种更快的计数记录方法,而不是计数(不同的)方法。有任何想法吗?非常感谢您的时间! – user984314