此子查询适用于 SQL Server:
select systemUsers.name,
(select count(id)
from userIncidences
where idUser = systemUsers.id )
from systemUsers
如何在 SQL Compact 中制作它?
谢谢!
此子查询适用于 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 计数显示他们。这甚至可能是您想要的,但它与您的原件不符。
谢谢大家,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)
像这样的东西?(使用Northwind.Order Details]
代码片段:
SELECT [Unit Price] * Quantity AS Cost,
[Unit Price] * Quantity * 1.25 AS CostWithVAT
FROM [Order Details]
源 航空订票系统项目