2009-01-22 96 views

回答

8

試試這個:

SELECT su.Name, COUNT(ui.ID) 
FROM systemUsers su 
LEFT JOIN userIncidences ui ON ui.idUser = su.ID 
GROUP BY su.Name 

[編輯:]
我原本的INNER JOIN就像託默勒格,但我意識到,這將排除用戶沒有事件,而不是一個0計數顯示他們。這可能甚至是你想要的,但它不符合你的原始。

+0

刪除我的,會有有兩個相同的答案,沒有用 – Tomalak 2009-01-22 17:25:34

+0

嗨託默勒格,至少我會upvoted。你也:) – 2009-01-22 17:28:38

10

有時您無法避開子查詢,例如,如果您必須包含使用當前行和上一行數據的計算列。考慮此查詢,例如:

SELECT  
    (Current.Mileage - Last.Mileage)/Quantity as MPG 
FROM   
    GasPurchases AS Current 
    LEFT OUTER JOIN GasPurchases AS Last 
    ON Last.Date = 
     (SELECT MAX(PurchaseDate) 
     FROM GasPurchases 
     WHERE PurchaseDate < Current.PurchaseDate) 

這將導致解析錯誤:

SQL Execution Error.

Error Source: SQL Server Compact ADO.NET Data Provider Error Message: There was an error parsing the query.

我發現this thread MSDN上,有一個解決方法。通過更改子查詢以便它返回一個集合而不是標量值,我可以保存並運行以下查詢。

SELECT  
    (Current.Mileage - Last.Mileage)/Quantity as MPG 
FROM   
    GasPurchases AS Current 
    LEFT OUTER JOIN GasPurchases AS Last 
    ON Last.Date IN 
     (SELECT MAX(PurchaseDate) 
     FROM GasPurchases 
     WHERE PurchaseDate < Current.PurchaseDate) 
0

謝謝你們,DoctaJonez,我發現你的小貼子對我的子查詢最有幫助。您的語法似乎與SQL Server Compact v3.5一起使用。這是我試過的查詢(哪些工作)。

順便說一句,硬編碼值,你看(38046),我知道在運行查詢

insert into tLink (start,stop,associativeobject,linktype,id,name,guid,createTime,modifyTime,externalID,description,linkLabel,LinkDetails) 
select newtable.id,stop,associativeobject,linktype,newtable.id,name,guid,createTime,modifyTime,externalID,description,linkLabel,LinkDetails from tLink l, (select id, '38046' as newid from tObject Where name = 'Step 1' and id <> '38046') as newtable 
where l.start = newtable.newid and start in (38046) 
-1

像這樣的時候? (使用Northwind.Order詳情]

代碼片段:

SELECT  [Unit Price] * Quantity AS Cost, 

[Unit Price] * Quantity * 1.25 AS CostWithVAT 
FROM   [Order Details] 

來源Airline Reservation System Project

相關問題