此子查询在SQL Server中起作用:
select systemUsers.name,
(select count(id)
from userIncidences
where idUser = systemUsers.id )
from systemUsers
如何在SQL Compact中创建?
谢谢!
答案 0 :(得分: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执行错误。
错误源:SQL Server Compact ADO.NET数据提供程序 错误消息:解析查询时出错。
我在MSDN上发现this thread有一个解决方法。通过更改子查询以便它返回一个集而不是标量值,我能够保存并运行以下查询。
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)
答案 1 :(得分:8)
试试这个:
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计数显示它们。这甚至可能是你想要的,但它与你原来的不符。
答案 2 :(得分: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)
答案 3 :(得分:-1)
这样的东西? (使用Northwind.Order详细信息]
代码段:
SELECT [Unit Price] * Quantity AS Cost,
[Unit Price] * Quantity * 1.25 AS CostWithVAT
FROM [Order Details]