Get a count of the number of times distinct entries occur in database query (Android)

旧巷老猫 提交于 2019-12-11 17:51:30

问题


I have a bit of a tricky problem. My app inputs user data into the SQLite database and I have managed to write queries to get the data back out.

This includes a query that gets a list of distinct values, an example may be as follows. Say you have the following data:

Column a  |  Column b  | Column c
   a            a           a
   a            b           b
   a            b           b
   b            b           b

Using the 'group by' function, the query would create the following list:

aaa
abb
bbb

So what I am trying, but failing to do is expand this and get a count of the number of times each distinct entry occurs - so in the above case it would look like this:

1 x aaa
2 x abb
1 x bbb

So is there a way, once you have queried the database, to get a count of the occurancies of each distinct entry, from the Cursor - or do I need to write a seperate query to get this information..

Here is my query as it looks at the moment:

public Cursor fetchDamagedComponentSpecForInspection(long inspectionId, String componentType) {
    Cursor mCursor = rmDb.query(true, DAMAGED_COMPONENTS_TABLE, new String[] {
            DAMAGED_COMPONENT_ID,
            LOCATION_LINK,
            RUN_LINK,
            AREA_LINK,
            INSPECTION_LINK,
            LOCATION_REF,
            RACKING_SYSTEM,
            COMPONENT,
            COMPONENT_TYPE,
            QUANTITY,
            POSITION,
            RISK,
            ACTION_REQUIRED,
            NOTES_GENERAL,
            MANUFACTURER,
            TEXT1,
            TEXT2,
            TEXT3,
            TEXT4,
            NOTES_SPEC,
            SPEC_SAVED}, 
            INSPECTION_LINK + " = " + inspectionId + " AND " + COMPONENT_TYPE + " = ? AND " + SPEC_SAVED + " = ? ", 
            new String[] {componentType, "Yes"},
            MANUFACTURER + ", " + TEXT1 + ", " + TEXT2 + ", " + TEXT3 + ", " + TEXT4 + ", " + NOTES_SPEC,
            null, null, null);
    if (mCursor != null) {
        mCursor.moveToFirst();
    }
    return mCursor;
}

For additional information, here is the code in my main activity where I call the query and retrieve the data:

final Cursor componentsAndSpecCursor = (Cursor) rmDbHelper.fetchComponentsAndSpecForManufacturer(inspectionId, rackingSystem, manufacturer);
                if(componentsAndSpecCursor.moveToFirst()){
                    do {
                        componentType = componentsAndSpecCursor.getString(componentsAndSpecCursor.getColumnIndex(RMDbAdapter.COMPONENT_TYPE));
                        specText1 = componentsAndSpecCursor.getString(componentsAndSpecCursor.getColumnIndex(RMDbAdapter.TEXT1));
                        specText2 = componentsAndSpecCursor.getString(componentsAndSpecCursor.getColumnIndex(RMDbAdapter.TEXT2));
                        specText3 = componentsAndSpecCursor.getString(componentsAndSpecCursor.getColumnIndex(RMDbAdapter.TEXT3));
                        specText4 = componentsAndSpecCursor.getString(componentsAndSpecCursor.getColumnIndex(RMDbAdapter.TEXT4));
                        specNotes = componentsAndSpecCursor.getString(componentsAndSpecCursor.getColumnIndex(RMDbAdapter.NOTES_SPEC));
                        message.append(componentType).append(" ").append(specText1).append(" ").append(specText2).append(" ").append(specText3).append(" ").append(specText4).append(" ").append(specNotes).append("\r\n");
                    }
                    while (componentsAndSpecCursor.moveToNext());
                }
                componentsAndSpecCursor.close();

So I am also unaware of how I would get the Count once I have included this bit of code in my query (which I still can't see how it is done!).


回答1:


Did u try adding COUNT(*) function to your columns list?

see this: count columns group by




回答2:


The query that you want is:

select count(*) as cnt, cola, colb, colc
from YourTable
group by cola, colb, colc

I'm not sure how to put this into your code. You don't seem to have a query that returns the combinations in the snippet of code that you provided.




回答3:


Try this.

Get all unique values and temporarily store them somewhere and just loop and do another query to try and find those values again and every time u find one, update some counter.



来源:https://stackoverflow.com/questions/14173359/get-a-count-of-the-number-of-times-distinct-entries-occur-in-database-query-and

标签
易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!