2014-04-09 3 views
0

Microsoft Access에서 4 개의 테이블의 데이터를 쿼리하고 MAX 함수를 사용하여 가장 최근의 레코드를 표시합니다. 아래 코드는 작동하지만 두 개의 쿼리를 함께 사용하고 있습니다. 전체 코드와 함께 아래에 표시된 하위 쿼리를 사용하면 코드를 실행하는 데 한 시간이 걸리기 때문에 두 개의 쿼리를 사용합니다. 이 작업을 수행하는 더 좋은 방법이 있습니까?Access에서 하위 쿼리의 MAX 함수 사용

QUERY 1

SELECT 
    a.office_id AS ofid, 
    rc.recorder_id AS recorder, 
    a.cust_name, 
    r.account_id, 
    rc.lpc_phone, 
    rc.device_serial, 
    rc.device_mfg, 
    rc.device_type 
INTO RDS_INFO 
FROM MaxDates 
INNER JOIN (status AS s INNER JOIN ((config_recorder AS rc INNER JOIN recorders AS r ON rc.recorder_id = r.recorder_id) 
INNER JOIN accounts AS a ON r.account_id = a.account_id) ON s.status = rc.row_status) ON (MaxDates.MaxOftrans_datetime = rc.trans_datetime) AND (MaxDates.recorder_id = rc.recorder_id) 
WHERE (((rc.lpc_phone) Is Not Null 
And (rc.lpc_phone)<>" " 
And (rc.lpc_phone) Not Like "#,*") 
AND ((rc.call_mode)="AN") 
AND ((rc.row_status)=3 
Or (rc.row_status)=11)); 

MaxDates 쿼리 : 서브 쿼리와 함께 사용

SELECT 
    config_recorder.recorder_id, 
    Max(config_recorder.trans_datetime) AS MaxOftrans_datetime 
FROM config_recorder 
GROUP BY config_recorder.recorder_id; 

코드 :

 SELECT 
      a.cycle AS cyc, 
      a.office_id AS ofid, 
      rc.recorder_id AS recorder, 
      a.cust_name, 
      r.account_id, 
      rc.lpc_phone, 
      rc.device_serial, 
      rc.device_mfg, 
      rc.device_type 
     INTO Test 
FROM config_recorder AS rc, status AS s, recorders AS r, accounts AS a 
WHERE rc.recorder_id=r.recorder_id 
AND r.account_id=a.account_id 
AND ((rc.lpc_phone Is Not Null) 
AND (rc.lpc_phone<>" ") 
AND (rc.lpc_phone Not Like "#,*")) 
AND ((rc.call_mode="AN") 
AND (rc.row_status=s.status) 
AND ((rc.row_status="3") 
Or (rc.row_status="11"))) 
AND (rc.trans_datetime=(select max(r2.trans_datetime) from config_recorder r2 where r2.recorder_id = rc.recorder_id)); 

당신의 도움에 감사드립니다.

+0

대신 여러 데이터베이스에서 FROM을 수행하는 이유가 있습니까? f 조인? – Gyhth

+0

어떤 버전의 Microsoft Access를 사용하고 있습니까? –

답변

0

당신은 아래, 인라인보기에 하위 쿼리를 변경 한 후 다른 테이블로 합류 고려할 수 :

SELECT 
a.office_id AS ofid, 
rc.recorder_id AS recorder, 
a.cust_name, 
r.account_id, 
rc.lpc_phone, 
rc.device_serial, 
rc.device_mfg, 
rc.device_type 
INTO RDS_INFO 
FROM status AS s 
INNER JOIN 
(config_recorder AS rc INNER JOIN recorders AS r ON rc.recorder_id = r.recorder_id) 
ON s.status = rc.row_status 
INNER JOIN accounts AS a 
ON r.account_id = a.account_id 
INNER JOIN 
(SELECT 
    config_recorder.recorder_id, 
    Max(config_recorder.trans_datetime) AS MaxOftrans_datetime 
FROM config_recorder 
GROUP BY config_recorder.recorder_id) MaxDates 
ON (MaxDates.MaxOftrans_datetime = rc.trans_datetime) AND (MaxDates.recorder_id = rc.recorder_id) 
WHERE (((rc.lpc_phone) Is Not Null 
And (rc.lpc_phone)<>" " 
And (rc.lpc_phone) Not Like "#,*") 
AND ((rc.call_mode)="AN") 
AND ((rc.row_status)=3 
Or (rc.row_status)=11)); 

참고 :

  1. A related question on SO
  2. Inline Views Versus Temp Tables on MSDN Magazine