2014-09-02 41 views
-1

我有下面对应的数据2列:如何把在集团计数器通过使用SQL

Name Category 
A  SL 
B  SL 
C  SL 
A  SL 
A  SL 
C  SL 
现在在我的剧本

,我组他们在类别知道他们发生了多少次反:

select Name, count(*) from Customer 
group by Category 

,但我想他们有多少次出现在查询像下面计数:

Customer Number# 
A   1 (means its the first time it occurs) 
A   2 (means its the second time it occurs) 
A   3 (means its the third time it occurs) 

。 ...等等

B   1 (means its the first time it occurs) 
C   1 (means its the first time it occurs) 
C   2 (means its the second time it occurs) 

对不起,有点困惑,希望大家明白。

谢谢。

+0

什么RDBMS您使用的? – Mureinik 2014-09-02 06:28:37

+0

即时通讯使用MS SQL – 2014-09-02 06:29:54

回答

0

尝试以下查询: -

SELECT name + '-' + CAST(ROW_NUMBER() OVER (
      PARTITION BY NAME ORDER BY id) AS VARCHAR(24)) 
FROM YOUR_TABLE 
+0

谢谢,它的工作原理! – 2014-09-02 07:14:02

+0

我的快乐。但仍然-1 – 2014-09-02 08:19:12

+0

抱歉,我无法投票,因为它需要15点声望。 – 2014-09-02 15:33:15