SQLite相当于Over(Partition)

时间:2018-04-05 11:20:13

标签: sql sqlite window-functions

我在SQL server中有以下查询。现在,数据库正在使用SQLite在移动设备上使用。有人可以帮助使用可在SQLite中运行的等效文件吗?

Select DISTINCT TblSites.Customer, TblSites.Digit, Count(TblContractTasks.TaskNumber) OVER (PARTITION BY TblSites.Digit)  As TaskCount 
FROM TblContractTasks INNER Join (TblSites INNER Join TblContractDetails On TblSites.Digit = TblContractDetails.SiteDigit) On TblContractTasks.ContractNumber = TblContractDetails.ContractNumber 
WHERE TblSites.Consultant='xx'  
ORDER BY TblSites.Digit

表:

Customer   Site   TaskNumber
------------------------
Ca          Sa      100
Ca          Sa      101
Cb          Sb      88
Ca          Sa      103

查询结果:

Customer    Site   TaskCount
-----------------------
Ca           Sa      3
Cb           Sb      1

1 个答案:

答案 0 :(得分:1)

您是否只是在寻找GROUP BY

SELECT s.Customer, s.Digit, COUNT(*) As TaskCount 
FROM TblSites s INNER JOIN
     TblContractDetails cd
     ON s.Digit = cd.SiteDigit INNER JOIN
     TblContractTasks t
     ON t.ContractNumber = cd.ContractNumber 
WHERE s.Consultant = 'xx'  
GROUP BY s.Customer, s.Digit
ORDER BY s.Digit;

这将返回您指定的数据的结果集。它似乎比从SELECT DISTINCT和您的COUNT() OVER获得的结果集更实用。