group by - How can I use SQL to return a list of customers with both a home phone & cell phone, home phone only, cell phone only, and no phone? -


i hope making more difficult is. here problem: need write sql query count customers in customer_table have phone_type of "home phone" , "cell phone" (i.e., these customers have 2 rows in table) customers have "home phone" (these customers have 1 row in table) customers have "cell phone" (these customers have 1 row in table) , customers have neither phone_type (these customers have @ least 1 row in table). here brief example of how should work:

sample data in customer_address table:

account_no   phone_type ----------   ----------            home phone            cell phone b            home phone c            home phone d            cell phone e            work phone f            no phone 

on paper, query return like:

phone types                           | account numbers ------------------------------------------------------- both "home phone" , "cell phone"    | ------------------------------------------------------- "home phone"                     | b, c ------------------------------------------------------- "cell phone"                     | d ------------------------------------------------------- neither "home phone" nor "cell phone" | e, f 

i don't believe union work. rudimentary stab @ creating query looks this:

select phone_type, account_no customer_address  where...help!!!!!!  group phone_type, account_no order phone_type, account_no 

thank you!

you can information @ per-customer level. here 1 method, using mysql syntax:

select phone_type, group_concat(account_no) accounts (select ca.account_no,              group_concat(distinct phone_type order phone_type separator ' , ') phone_types       customer_address ca       group ca.account_no      ) ca group phone_type; 

this basic query. databases have function similar group_concat().


Comments

Popular posts from this blog

javascript - how to protect a flash video from refresh? -

android - Associate same looper with different threads -

visual studio 2010 - Connect to informix database windows form application -