我需要通过查询将sql sign
函数用于我的组,以对正数和负数进行分组。
不幸的是,sqlite不包括一个。
有人可以建议解决方法吗?还是如何使它与xcode中使用的libsqlite3.dylib框架一起工作?
我的查询很复杂
select fcid, sum(price), (select sum(price) from tmp b where ((b.due < a.due)
or ((b.due = a.due) and (b.pid <= a.pid)))) as accumulated_price from tmp a
where due >= '2011-01-25' and due < '2011-02-24' and price <> 0 group by fcid
order by due, pid;
我想做的是在
sign(price)
上进行分组,所以我得到两个结果,负值和正值。这些将代表总支出和总收入。想要添加这些标签(但不允许创建新标签libsqlite3.dylib libsqlite3)
最佳答案
不知道这是否是您的最佳选择,但是您可以尝试:
select your_val > 0, sum(aggregated_value)
from your_table
group by your_val > 0;
这样,您应将
0
设置为零或负值,将1
设置为正值。更新:如果
fcid
是您需要签名的字段,则可以尝试:select fcid > 0, sum(price),
(
select sum(price)
from tmp b
where ((b.due < a.due) or ((b.due = a.due) and (b.pid <= a.pid)))
) as accumulated_price
from tmp a
where due >= '2011-01-25'
and due < '2011-02-24'
and price <> 0
group by fcid > 0;
请注意,您的
order by
子句是无用的,因为无论如何您都要对结果进行分组。
关于iphone - Xcode,iPhone,Sqlite是否需要用于分组的签名功能?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/4796688/