简体   繁体   English

Xcode,iPhone,Sqlite是否需要用于分组的签名功能?

[英]Xcode, iphone, Sqlite, need a sign function for group by?

I need an sql sign function for my group by query to group positive and negative amounts. 我需要通过查询对我的组使用sql sign功能,以将正负金额分组。

Unfortunately sqlite doesn't include one. 不幸的是,sqlite不包含其中一个。

Can anyone suggest a workaround ? 有人可以建议解决方法吗? or how to ad one to work with the libsqlite3.dylib framework used in xcode ? 还是如何使它与xcode中使用的libsqlite3.dylib框架一起工作?

My query is quite complicated 我的查询很复杂

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;

What I'm trying to do, is a group on sign(price) so I get two result and negative value and a positive value. 我正在尝试做的是一组sign(price)所以我得到两个结果,负值和正值。 These will represent total expenses and total income. 这些将代表总支出和总收入。

Would like to have added these tags (but I'm not allowed to create new ones libsqlite3.dylib libsqlite3) 想要添加这些标签(但不允许创建新标签libsqlite3.dylib libsqlite3)

Don't know if it's your best choice, but you can try: 不知道这是否是您的最佳选择,但是您可以尝试:

select your_val > 0, sum(aggregated_value)
  from your_table
 group by your_val > 0;

This way, you should have 0 for ZERO or negative values and 1 for positive values. 这样,对于零或负值,您应该有0 ,对于正值,您应该有1

UPDATE: If fcid is the field you need the sign for, you can try: 更新:如果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;

Note that your order by clause is useless since you are grouping your results anyway. 请注意,您的order by子句是无用的,因为无论如何您都将结果分组。

You should just create your own sign function. 您应该只创建自己的sign函数。 See Create or Redefine SQL Functions . 请参见创建或重新定义SQL函数

Register a function using sqlite3_create_function : 使用sqlite3_create_function注册一个函数:

sqlite3_create_function( db, "sign", 1, SQLITE_ANY, NULL, signFunc,
                         NULL, NULL);

Then implement signFunc in C. 然后在C中实现signFunc。

static void signFunc(sqlite3_context *c, int argCount, sqlite3_value **args) {
    if ( argCount != 1 ) {
        sqlite3_result_null( c );
        return;
    }

    switch ( sqlite3_value_type( args[0] ) ) {
        case SQLITE_INTEGER: {
            sqlite3_int64 asInt = sqlite3_value_int64( args[0] );
            sqlite3_int64 sign = asInt < 0 ? -1 : (asInt > 0 ? 1 : 0);
            sqlite3_result_int64( c, sign );
            break;
        }
        case SQLITE_FLOAT: {
            double asDouble = sqlite3_value_double( args[0] );
            sqlite3_int64 sign = asDouble < 0 ? -1 : (asDouble > 0 ? 1 : 0);
            sqlite3_result_double( c, sign );
            break;
        }
        case SQLITE_NULL:
        default: {
            sqlite3_result_null( c );
            break;
        }
    }
}

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM