| Author |
Topic  |
|
|
AskSQLTeam
Ask SQLTeam Question
USA
0 Posts |
|
|
Scott Pletcher
Starting Member
2 Posts |
Posted - 06/20/2007 : 11:33:37
|
| Very helpful info, and well presented. |
 |
|
|
addytude
Starting Member
India
12 Posts |
Posted - 02/17/2008 : 13:09:27
|
| most informative! Thanks |
 |
|
|
eldkir
Starting Member
USA
1 Posts |
Posted - 03/13/2008 : 12:11:11
|
Great Information!!
I have a question that may be applicable to this function. I am trying to do a count of the number of times customers have purchased from us. For example, I need these 3 buckets; Once, Twice and 3 or more. Would I incorporate the Over function here, or is there something else I should be using? Thanks in advance for your help! |
 |
|
|
visakh16
Very Important crosS Applying yaK Herder
India
48117 Posts |
Posted - 03/23/2008 : 20:18:24
|
quote: Originally posted by eldkir
Great Information!!
I have a question that may be applicable to this function. I am trying to do a count of the number of times customers have purchased from us. For example, I need these 3 buckets; Once, Twice and 3 or more. Would I incorporate the Over function here, or is there something else I should be using? Thanks in advance for your help!
You can use OVER in this scenario as follows:-
SELECT tmp.Category,Count(tmp.customer_id)
FROM
(
SELECT t.customer_id,
CASE
WHEN t.CustSum =1 THEN 'Once'
WHEN t.CustSum=2 THEN 'Twice'
ELSE '3 or more'
END AS Category
FROM
(
SELECT DISTINCT c.customer_id,
SUM(p.purchase_id) OVER (PARTITON BY c.customer_id) AS CustSum
FROM Customer c
INNER JOIN Purchase p
ON p.customer_id=c.customer_id
)t
)tmp
GROUP BY tmp.Category
|
 |
|
| |
Topic  |
|