mysql查询 - 选择不查找值

时间:2011-11-16 11:32:34

标签: mysql select count distinct

有人可以帮我修改这个查询,以便我可以获取outer_config.contractorsRef值。目前它没有找到价值。我在代码中高举了它。我有目的用count(*)和sub查询中的select编写查询,因为它执行的速度比count(distinct)快。

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;

1 个答案:

答案 0 :(得分:0)

它表示您正试图获得以下内容...... 每个承包商的清单。从每个承包商,他们正在与多少不同的分包商合作。由此,承包商的所有分包商的工人总数。如果这是正确的,下面的查询可能更接近您的需求。

第一个“来自”查询只是查询承包商+分包商的合格条件以及按承包商+子分组的计数。

从该结果中,查询承包商,然后查询分包商的COUNT,并将分包商工人的数量计算在内。

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

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

我还在更好地检查您的表结构之后调整了内部查询,以便根据请求配置表FIRST和第二个请求表进行查询。 “WHERE”子句直接应用于配置请求表(通过索引优化)。

SELECT
      PreQuery.ContractorsRef,
      COUNT(*) as SubContractors,
      SUM( PreQuery.subCount  ) as SubContRequests
   from 
      ( SELECT 
              RCNF.contractorsRef,
              REQ.SubContractorRef,
              COUNT( * ) subcount
           FROM
              bis.request_config RCNF
                 JOIN bis.Request REQ
                    ON request.RIDGROUP = request_config.RIDGROUP
           WHERE
                    RCNF.currenttaxyear = 2011
                AND RCNF.weekno = 33
                AND RCNF.ContractorsRef <> 132
           GROUP BY
              RCNF.contractorsRef,
              REQ.SubContractorRef ) PreQuery
   group by
      PreQuery.ContractorsRef

- 结果的另一个澄清 -

从你的上一次评论中得到重复计算 假设您有以下记录。

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

正如您所看到的,“SubA”仍然只出现在ContractorsRef 111的ONCE,并显示该子的4个唯一请求条目...因此,OUTER查询只有ContractorsRef才能分组

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

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

相关问题