Unix Technical Forum

Query Help, thanks!

This is a discussion on Query Help, thanks! within the SQL Server forums, part of the Microsoft SQL Server category; --> Hello, I am trying to count the number of customers for each product group. One customer may have multiple ...


Go Back   Unix Technical Forum > Database Server Software > Microsoft SQL Server > SQL Server

FAQ Members List Calendar Search Today's Posts Mark Forums Read
  #1 (permalink)  
Old 02-29-2008, 07:32 AM
rong.guo@gmail.com
 
Posts: n/a
Default Query Help, thanks!

Hello,

I am trying to count the number of customers for each product group.
One customer may have multiple products. Now I am doing it one by one,
i.e., when I do product_id like '111%', I make all other 3 groups
comments (please see query below). Is there a way that I can get the #s
of the 4 groups at the same time? In fact, I have more than 10 groups,
feel cumbersome to do it one by one. Many thanks!

SELECT count(distinct customer_id)
FROM account
WHERE year(close_date)=1900
and product_id like '111%'
--Product Group 1
--and product_id in ('222', '333')
--Product Group 2
--and product_id in ('444', '555')
--Product Group 3
--and product_id like '666%'
--Product Group 4

Digg this Post!Add Post to del.icio.usBookmark Post in TechnoratiFurl this Post!
Reply With Quote
  #2 (permalink)  
Old 02-29-2008, 07:32 AM
Hugo Kornelis
 
Posts: n/a
Default Re: Query Help, thanks!

On 9 Mar 2005 13:19:51 -0800, rong.guo@gmail.com wrote:

>Hello,
>
>I am trying to count the number of customers for each product group.
>One customer may have multiple products. Now I am doing it one by one,
>i.e., when I do product_id like '111%', I make all other 3 groups
>comments (please see query below). Is there a way that I can get the #s
>of the 4 groups at the same time? In fact, I have more than 10 groups,
>feel cumbersome to do it one by one. Many thanks!
>
>SELECT count(distinct customer_id)
>FROM account
>WHERE year(close_date)=1900
>and product_id like '111%'
> --Product Group 1
>--and product_id in ('222', '333')
> --Product Group 2
>--and product_id in ('444', '555')
> --Product Group 3
>--and product_id like '666%'
> --Product Group 4


Hi rong.guo,

Try if this works:

SELECT ProductGroup, COUNT(DISTINCT customer_id)
FROM (SELECT customer_id,
CASE
WHEN product_id LIKE '111%' THEN 1
WHEN product_id IN ('222', '333') THEN 2
WHEN product_id IN ('444', '555') THEN 3
WHEN product_id LIKE '666%' THEN 4
END AS ProductGroup
FROM account
WHERE year(close_date) = 1900) AS x
GROUP BY ProductGroup

(untested)

Best, Hugo
--

(Remove _NO_ and _SPAM_ to get my e-mail address)
Digg this Post!Add Post to del.icio.usBookmark Post in TechnoratiFurl this Post!
Reply With Quote
  #3 (permalink)  
Old 02-29-2008, 07:34 AM
rong.guo@gmail.com
 
Posts: n/a
Default Re: Query Help, thanks!

Thanks Hugo! It works well!

Digg this Post!Add Post to del.icio.usBookmark Post in TechnoratiFurl this Post!
Reply With Quote
Reply


Thread Tools
Display Modes

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

vB code is On
Smilies are On
[IMG] code is On
HTML code is Off
Trackbacks are On
Pingbacks are On
Refbacks are On
Forum Jump


All times are GMT. The time now is 01:26 PM.


Powered by vBulletin® Version 3.6.5
Copyright ©2000 - 2008, Jelsoft Enterprises Ltd.
SEO by vBSEO 3.2.0
www.UnixAdminTalk.com