SQL Query - Count column values separately
        Posted  
        
            by 
                user575535
            
        on Stack Overflow
        
        See other posts from Stack Overflow
        
            or by user575535
        
        
        
        Published on 2011-01-14T11:45:45Z
        Indexed on 
            2011/01/14
            11:53 UTC
        
        
        Read the original article
        Hit count: 520
        
I have a hard time getting a Query to work right.
This is the DDL for my Tables
CREATE TABLE Agency (
id SERIAL not null,
city VARCHAR(200) not null,
PRIMARY KEY(id)
);
CREATE TABLE Customer (
id SERIAL not null,
fullname VARCHAR(200) not null,
status VARCHAR(15) not null CHECK(status IN ('new','regular','gold')),
agencyID INTEGER not null REFERENCES Agency(id),
PRIMARY KEY(id)
);
Sample Data from the Tables
AGENCY
id|'city'
1 |'London'
2 |'Moscow'
3 |'Beijing'
CUSTOMER
id|'fullname'      |'status' |agencyid
1 |'Michael Smith' |'new'    |1
2 |'John Doe'      |'regular'|1
3 |'Vlad Atanasov' |'new'    |2
4 |'Vasili Karasev'|'regular'|2
5 |'Elena Miskova' |'gold'   |2
6 |'Kim Yin Lu'    |'new'    |3
7 |'Hu Jintao'     |'regular'|3
8 |'Wen Jiabao'    |'regular'|3
I want to produce the following output, but i need to count separately for ('new','regular','gold')
'city'   |new_customers|regular_customers|gold_customers
'Moscow' |1            |1                |1
'Beijing'|1            |2                |0
'London' |1            |1                |0
© Stack Overflow or respective owner