此子查询在SQL Server中有效:
select systemUsers.name, (select count(id) from userIncidences where idUser = systemUsers.id ) from systemUsers
如何在SQL Compact中创建?
谢谢!
有些情况下,您无法避免子查询,例如,如果您必须包含使用当前行和上一行数据的计算列.考虑这个查询,例如:
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执行错误.
错误源:SQL Server Compact ADO.NET数据提供程序错误消息:解析查询时出错.
我在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)
试试这个:
SELECT su.Name, COUNT(ui.ID) FROM systemUsers su LEFT JOIN userIncidences ui ON ui.idUser = su.ID GROUP BY su.Name
[编辑:]
我最初有一个像Tomalak一样的INNER JOIN,但我意识到这会排除没有事件的用户,而不是用0计数显示它们.这甚至可能是你想要的,但它与你原来的不符.