2015-06-02 36 views
0

我想在Access 2013中創建一個顯示當前狀態和日期的查詢,同時忽略舊的狀態記錄。在Microsoft Access 2013中創建一個顯示當前狀態的查詢

我有三個表:

客戶 ID(PK) 最後 第一

狀態 ID(PK) 狀態 描述

狀態,客戶 ID(PK) customer_id status_id status_date 筆記

如果我使用字段last,first,customer_id和status_date創建一個查詢,它在最後一個status_date上進行分組,它很好用。但我無法查詢基於status_date列出當前狀態。無論我如何添加狀態,它都會打破分組並顯示具有多個狀態的同一個人。

例如:

Last  First  Customer_id   Status_date 
John   Smith    1    05/30/2015 
Mary   Johnson   2    05/26/2015 



Last  First  Customer_id   Status_date  Status 
John   Smith    1    05/25/2015  In Process 
John   Smith    1    05/30/2015  Complete 
Mary   Johnson   2    05/26/2015  Complete 

我想是第二次查詢,但沒有重複記錄。

謝謝。

回答

0

我上傳我的解決方案:http://sqlfiddle.com/#!6/ef4b9/1/0

SELECT c.first,c.last,c.id as customer_id,sc.status_date,s.name 
FROM customer c 
left join customer_status sc on sc.customer_id=c.id 
left join status s on s.id=sc.status_id 
where sc.id=(SELECT MAX(id) from 
      customer_status WHERE customer_id=sc.customer_id) 

評論:我已經採取customer_Status最後一個ID,而不是過去的日期,因爲如果有在同一日期的多個狀態更新,我們需要最後插入的狀態。 這是SQL服務器2014語法應該是相同的訪問...

+0

我想我是關閉的,我沒有看到任何錯誤在SQL中,但Access始終返回以下消息:Syntex錯誤(缺少運算符)查詢表達式'sc.customer_id = c.id left join status sn s.id = sc.status_i' – Marisa

+0

發佈您嘗試執行的查詢 –