2

我有一个 T-SQL 查询,但很难将它转换为 android 的 SQLite,我有相同的表名、字段名、表结构和关系,任何帮助将不胜感激,下面是查询

SELECT
   F.fieldID,F.name,F.description,F.sizeHA,F.sizeA
   ,F.owner,F.annualRent,F.latitude,F.longitude
   ,F.Farm_farmID,F.isActive,F.deactivationDate,F.areas,
   (SELECT COUNT(DISTINCT LOWER(culture)) FROM Usages 
          WHERE Field_fieldID = F.fieldID
          AND YEAR(date) = (SELECT TOP 1 YEAR(date) FROM Usages 
                            ORDER BY Date DESC)
   ) as UsageCount
   ,(SELECT TOP 1 u.culture FROM Usages u WHERE u.Field_fieldID = F.fieldID 
      ORDER BY u.date desc) as culture
   ,(SELECT TOP 1 u.sort FROM Usages u WHERE u.Field_fieldID = F.fieldID 
      ORDER BY u.date desc) as sort 
FROM Fields as F INNER JOIN Usages s on s.Field_fieldID = F.fieldID 
GROUP BY F.fieldID,F.name,F.description,F.sizeHA,F.sizeA
,F.owner,F.annualRent,
F.latitude,F.longitude,F.Farm_farmID,F.isActive,F.deactivationDate,F.areas
4

1 回答 1

1

替换用法TOP

SELECT TOP 1 ...

使用LIMIT 子句

SELECT ... LIMIT 1

此外,没有任何YEAR功能。如果您的日期以一种受支持的日期格式存储,请使用strftime以四位数字符串形式获取年份:

strftime('%Y', date)
于 2013-07-16T12:43:42.807 回答