views:

7

answers:

1

sqlite3

I have two tables. One contains some lists and the other contains each list's items.

I want to create a select statement that grabs the rows in the lists table, but also creates a column which is a comma-delimited summary of the items in each list.

I have this working as follows:

select
    master._id as _id,
    master.name as name,
    master.created_on as created_on,
    group_concat(items.name, ', ')
from
    tablea master
join
    tableb items
on
    master._id = items.master_id
group by
    master._id

However, I would like to name the column returned by the group_concat as "summary" like so:

select
    master._id as _id,
    master.name as name,
    master.created_on as created_on,
    group_concat(items.name, ', ') as summary
from
    tablea master
join
    tableb items
on
    master._id = items.master_id
group by
    master._id

When I do this, I get an sql error: "SQL error: near "summary": syntax error

How can I achieve what I'm wanting to do?

I would also like to order the items in the group_concat alphabetically descending, but naming the column is my first priority.

+1  A: 

"AS" is optional. However, both with and without "AS" works fine for me (using SQLite version 3.6.14.2):

drop table tablea;
drop table tableb;
create table tablea(_id int, name varchar, created_on varchar);
create table tableb(master_id int, name varchar);
insert into tablea values(0, 'Hello', '2010');
insert into tableb values(0, 'x');
select
    master._id as _id,
    master.name as name,
    master.created_on as created_on,
    group_concat(items.name, ', ') as summary
from
    tablea master
join
    tableb items
on
    master._id = items.master_id
group by
    master._id
Thomas Mueller
Removing the "AS" makes the query run. Thank you. I've no idea why, but using as gives me the error I described in the OP
Andrew