0

I want to get grouped data from a table in sqlite. For example, the table is like below:

Name  Group  Price
a     1      10
b     1      9
c     1      10
d     2      11
e     2      10
f     3      12
g     3      10
h     1      11

Now I want get all data grouped by the Group column, each group in one array, namely

array1 = {{a,1,10},{b,1,9},{c,1,10},{h,1,11}}; 
array2 = {{d,2,11},{e,2,10}}; 
array3 = {{f,3,12},{g,3,10}}.

Because i need these 2 dimension arrays to populate the grouped table view. the sql statement maybe NSString *sql = @"SELECT * FROM table GROUP BY Group"; But I wonder how to get the data from the resultset. I am using the FMDB.

Any help is appreciated.

chancyWu
  • 14,073
  • 11
  • 62
  • 81

2 Answers2

1

Get the data from sql with a normal SELECT statement, ordered by group and name:

SELECT * FROM table ORDER BY group, name;

Then in code, build your arrays, switching to fill the next array when the group id changes.

TheEye
  • 9,280
  • 2
  • 42
  • 58
0

Let me clear about GroupBy. You can group data but that time its require group function on other columns. e.g. Table has list of students in which there are gender group mean Male & Female group so we can group this table by Gender which will return two set . Now we need to perform some operation on result column. e.g. Maximum marks or Average marks of each group

In your case you want to group but what kind of operation you require on price column ?. e.g. below query will return group with max price.

SELECT Group,MAX(Price) AS MaxPriceByEachGroup FROM TABLE GROUP BY(group)
Anvesh
  • 7,103
  • 3
  • 45
  • 43
  • I update my question now, actually i want to use the table data to make a 2 dimension array to populate the grouped table view. – chancyWu Apr 19 '13 at 13:42