SQLite累积和和WHERE包含SUBSTR()

时间:2016-11-10 02:50:23

标签: database sqlite

如何根据标题记录列code的第一个字符获得累计金额?

考虑:

 tbl_A:
 --------------------------------
 |  is_title  |  code  | amount |
 --------------------------------
 | true       | "1"    |      0 |
 | false      | "11"   |     10 |
 | false      | "12"   |     15 |
 | true       | "2"    |      0 |
 | false      | "21"   |     30 |
 | false      | "22"   |     40 |
 --------------------------------

我想得到:

 -----------------------
 |  code  | cumulative |
 -----------------------
 | "1"    |         25 |
 | "2"    |         70 |
 -----------------------

1 个答案:

答案 0 :(得分:1)

您需要selectsubstr(code,1,1)表达式select substr(code,1,1) as code, sum(amount) as cumulative from tbl_A group by substr(code,1,1)

public static string ConvertEmoji2UnicodeHex(string emoji)
        {
            if (string.IsNullOrWhiteSpace(emoji))
                return emoji;

            byte[] bytes = Encoding.UTF8.GetBytes(emoji);
            string firstItem = Convert.ToString(bytes[0], 2);  

            int iv;
            if (bytes.Length == 1)
            {
                iv = Convert.ToInt32(firstItem, 2);
            }
            else
            {
                StringBuilder sbBinary = new StringBuilder();
                sbBinary.Append(firstItem.Substring(bytes.Length + 1).TrimStart('0'));
                for (int i = 1; i < bytes.Length; i++)
                {
                    string item = Convert.ToString(bytes[i], 2);
                    item = item.Substring(2);
                    sbBinary.Append(item);
                }

                iv = Convert.ToInt32(sbBinary.ToString(), 2);
            }

            return Convert.ToString(iv, 16).PadLeft(4, '0');
        }