Advertisement

12.17.2007 at 05:30PM PST, ID: 23029651
[x]
Attachment Details

Grouping data in SQL

Asked by singhch in SQL Query Syntax

Hi there,

The following is a sample of my table

client_no     type
================
1                  dep
2                  dep
1                  dep
1                  wth
2                  dep

and i would like to group my data as

client_no    dep    wth
==================
1                 2        1
2                 2        0

How would I go about doing this??

I tried the following but i am not gettting the desired result

select
   client_number,
   case when type = 'dep' then count(distinct type) else 0 end as 'dep',
  case when type = 'wth' then count(distinct type) else 0 end as 'wth'
from table
group by client_no,type

Ideally, I would like the sql to name the columns accordingly to the distinct 'type' int the table.

thanksStart Free Trial
[+][-]12.17.2007 at 05:44PM PST, ID: 20489526

At Experts Exchange, members can ask their questions to thousands of technology professionals, also known as Experts. Experts compete and collaborate to answer those questions by leaving comments like this one.

Start your 7-day free trial to view this Expert Comment or ask the Experts your question.

 
[+][-]12.17.2007 at 05:45PM PST, ID: 20489531

View this solution now by starting your 7-day free trial. Setting up your free trial is quick, easy, and secure. We will return you to this solution, unlocked, when you're done.

 

About this solution

Zone: SQL Query Syntax
Sign Up Now!
Solution Provided By: imitchie
Participating Experts: 1
Solution Grade: A
 
 
[+][-]12.17.2007 at 05:53PM PST, ID: 20489565

Often, when Experts are collaborating with members who have asked questions, they will request additional information about the problem. Askers respond with an author comment like this one.

Start your 7-day free trial to view this Author Comment or ask the Experts your question.

 
 
Loading Advertisement...
20080716-EE-VQP-32 / EE_QW_2_20070628