2009-01-22 101 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

相关问题