2011-11-16 100 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