2010-04-30 100 views
1

我有一個訪問數據庫中的幾個表:選擇查詢加入兩個字段?

ID | LocationName 
1 | Location1 
2 | Location2 

ID | LocationID | Date | NumProductsDelivered 
1 |  1  | 12/10 |  3 
2 |  1  | 01/11 |  2 
3 |  1  | 02/11 |  2 
4 |  2  | 11/10 |  1 
5 |  2  | 12/10 |  1 

ID | LocationID | Date | NumEmployees | EmployeeType 
1 |  1  | 12/10 |  10  |  1 (=Permanent) 
2 |  1  | 12/10 |  3  |  2 (=Temporary) 
3 |  1  | 12/10 |  1  |  3 (=Support) 
4 |  2  | 10/10 |  1  |  1 
5 |  2  | 11/10 |  2  |  1 
6 |  2  | 11/10 |  1  |  2 
7 |  2  | 11/10 |  1  |  3 
8 |  2  | 12/10 |  2  |  1 
9 |  2  | 12/10 |  1  |  3 

我想要做的就是在LocationID作爲參數傳遞,並取回東西如下表等。所以,如果我在2傳我的LocationID,我應該得到:

Date | NumProductsDelivered | NumPermanentEmployees | NumSupportEmployees 
10/10 |      |   1   |   
11/10 |   1   |   2   |   1 
12/10 |   1   |   2   |   1 

看起來這應該是一個非常簡單的查詢。我甚至不需要第一個表格,除非填寫用戶選擇他們想要報告的位置的表單上的組合框。不幸的是,我所做的一切都讓我得到了比我應得的更多的數據。我的困惑在於如何設置連接(大概是我在這裏尋找的),因爲我希望date和locationID對結果集中的每一行都是相同的。

任何幫助將不勝感激。

謝謝。

編輯: 好 - 答案下面也不太工作,但它確實讓我在正確的軌道上,我能夠使用以下查詢:

SELECT t1.Date, t2.NumProductsDelivered, 
    (SELECT t1a.NumEmployees 
    FROM table3 t1a 
    WHERE t1a.EmployeeType=1 AND t1a.LocationID=t1.LocationID AND t1a.Date= t1.Date) 
    AS "PermEmps", 
    (SELECT t1b.NumEmployees 
    FROM table3 t1b 
    WHERE t1b.EmployeeType=3 AND t1b.LocationID=t1.LocationID AND t1b.Date=t1.Date) 
    AS "SupportEmps" 
FROM table3 AS t1 LEFT JOIN table2 AS t2 ON (t2.Date=t1.Date) 
    AND (t2.LocationID=t1.LocationID) 
WHERE t1.LocationID=2 
GROUP BY t1.Date, t1.LocationID, t2.NumProductsDelivered; 

這讓我我正在尋找的結果。但是,如果產品交付地點有中斷,我看不到正確的結果。看起來,只要有空行,記錄就會停下來,然後再從頭回來。所以,在這裏我可能會看到這一點:

Date | NumProductsDelivered | NumPermanentEmployees | NumSupportEmployees 
10/10 |      |   1   |   
11/10 |   1   |   2   |   1 
12/10 |   1   |   2   |   1 
01/10 |   2   |      |   1 
06/10 |   1   |      | 

我只看到這一點:

Date | NumProductsDelivered | NumPermanentEmployees | NumSupportEmployees 
10/10 |      |   1   |   
11/10 |   1   |   2   |   1 
12/10 |   1   |   2   |   1 
01/10 |   2   |      |   1 
+0

你不應該GROUP BY t1.LocationID因爲你沒有選擇它,它始終是2。我也要推薦使用比「t1」更好的名字來表示可讀性。另一個問題是一個謎題...... t1和t2之間是否存在1對1的關係?另外,嘗試刪除join和subselect以查看是否全部獲取您需要的日期(我會在下面添加到我的答案中) – 2010-04-30 20:04:29

回答

0

像這樣的東西應該工作:

[刪除原創]

嘗試這取而代之(未經測試):

select t3.date, t2.numproductsdelivered, 
    (select sum(t3.numemployees) 
    from table3 t3a 
    where t3a.locationid = t3.locationid and t3a.date = t3.date and t3a.employeetype = 1 
) as numpermanentemployees, 
    (select sum(t3.numemployees) 
    from table3 t3b 
    where t3b.locationid = t3.locationid and t3b.date = t3.date and t3b.employeetype = 3 
) as numsupportemployees 
from table3 as t3 
left join table2 as t2 on t2.locationid = t3.locationid and t2.date = t3.date 
where t3.locationid = 2 
group by t3.date, t2.numproductsdelivered 

如果你不介意爲每個員工類型它可以簡化單獨行:

select t3.date, t2.numproductsdelivered, t3.employeetype, sum(t3.numemployees) as numemployees 
from table3 as t3 
left join table2 as t2 on t2.locationid = t3.locationid and t2.date = t3.date 
where t3.locationid = 2 and t3.employeetype in (1, 3) 
group by t3.date, t2.numproductsdelivered, t3.employeetype 

編輯:嘗試此查詢:

SELECT t1.Date 
FROM table3 AS t1 
WHERE t1.LocationID=2 
GROUP BY t1.Date 

...看看你獲取所有日期。

然後添加左連接:

SELECT t1.Date, t2.NumProductsDelivered 
FROM table3 AS t1 LEFT JOIN table2 AS t2 ON (t2.Date=t1.Date) 
    AND (t2.LocationID=t1.LocationID) 
WHERE t1.LocationID=2 
GROUP BY t1.Date, t1.LocationID, t2.NumProductsDelivered; 

如果它做左INNER JOIN,那麼它會刪除從t1行沒有在T2匹配的行。嘗試明確設置一個左外部連接,看看是否有效。關係型數據庫管理系統我使用最多的默認設置爲外層,但也許你的(訪問)默認爲內層。

所以我想下面的工作將添加「外部」和刪除「t1。LocationId「):

SELECT t1.Date, t2.NumProductsDelivered, 
    (SELECT t1a.NumEmployees 
    FROM table3 t1a 
    WHERE t1a.EmployeeType=1 AND t1a.LocationID=t1.LocationID AND t1a.Date= t1.Date) 
    AS "PermEmps", 
    (SELECT t1b.NumEmployees 
    FROM table3 t1b 
    WHERE t1b.EmployeeType=3 AND t1b.LocationID=t1.LocationID AND t1b.Date=t1.Date) 
    AS "SupportEmps" 
FROM table3 AS t1 LEFT OUTER JOIN table2 AS t2 ON (t2.Date=t1.Date) 
    AND (t2.LocationID=t1.LocationID) 
WHERE t1.LocationID=2 
GROUP BY t1.Date, t2.NumProductsDelivered; 
+0

一些註釋: 1.我必須切換查詢或Acccess中的'group by'和'where'部分甚至不會讓我保存它。 2.在試圖運行它,我得到一個錯誤: 試圖執行一個查詢,不包括指定表達式「numproductsdelivered」作爲一個聚合功能的一部分。 – wtollett 2010-04-30 18:00:29

+0

是的,你不能選擇任何你沒有分組(除了agreggated值)...在這種情況下,我認爲你可以簡單地由t3.date,t2.numproductsdelivered更改爲組,因爲這個額外的分組贏得了' t改變結果(你只有一個numproducts在每個日期交付)。我修復了我的答案(也許並非完全) – 2010-04-30 18:03:22

+0

這仍然不起作用,但它幫助我找到了一個似乎正在工作的答案,除了一種情況(參見上文)。 – wtollett 2010-04-30 19:44:56

0

,我認爲這會工作:

DECLARE @LocationId int 

SET @LocationId=2 

SELECT L2.LocationId, L2.Date, COUNT(DISTINCT NumProductsDelivered) as NumProductsDelivered, 
SUM(case when L2.EmployeeType =1 then NumEmployees else 0 end) as NumPermanentEmployees, 
SUM(case when L2.EmployeeType =3 then NumEmployees else 0 end) as NumSupportEmployees 
FROM L1 
    RIGHT JOIN L2 
     ON L1.LocationID=L2.LocationID 
     AND L1.Date=L2.Date 
WHERE [email protected] 
GROUP BY L2.LocationId, L2.Date 
+0

突出的是「案例」...爲什麼我沒有想到這一點? :)不知道其餘的工作... – 2010-04-30 20:14:35

+0

這是一個有關Access/Jet/ACE數據庫的問題的T-SQL答案(我在問題本身或標籤中看不到SQL Server)。 -1不注意並提供非工作答案。 – 2010-04-30 21:12:17

+0

是的,你是對的。我沒有看到它是關於Access,我的錯誤。使用Access而不是CASE,您應該使用IIF來獲得相同的結果。 – Claudia 2010-05-03 12:54:27