How can I make this query in SQL Server Compact Edition?

前端 未结 4 436
太阳男子
太阳男子 2020-12-20 03:57

this subquery works in SQL Server:

select systemUsers.name, 
    (select count(id) 
     from userIncidences 
     where idUser = systemUsers.id ) 
from syst         


        
相关标签:
4条回答
  • 2020-12-20 04:37

    There are cases when you can't avoid a subquery, for instance if you have to include calculated columns that use data from the current and the previous row. Consider this query, for instance:

    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)
    

    It will cause a parsing error:

    SQL Execution Error.

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

    I found this thread on MSDN that has a workaround. By changing the subquery so that it returns a set instead of a scalar value, I was able to save and run the following query.

    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 讨论(0)
  • 2020-12-20 04:39

    Thanks guys, DoctaJonez, I found your little post the most helpful with my subquery. Your syntax seems to work with SQL Server Compact v3.5. Here is the query I tried (which works).

    By the way, the hardcoded value you see (38046), I know at the time of running the query

    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)
    
    0 讨论(0)
  • 2020-12-20 04:53

    Something like this? (using Northwind.Order Details]

    The Code Snippet:

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

    Source Airline Reservation System Project

    0 讨论(0)
  • 2020-12-20 05:03

    Try this:

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

    [Edit:]
    I originally had an INNER JOIN just like Tomalak, but I realized that this would exclude users with no incidents, rather than show them with a 0 count. That might even be what you want, but it doesn't match your original.

    0 讨论(0)
提交回复
热议问题