字符串出现次数

时间:2015-06-09 11:19:30

标签: sql sql-server subquery

我有一个汽车列表

id | brand | model
1  | bmw   | 525
1  | bmw   | 533
2  | audi  | a8
...

我想采用特定于id的品牌匹配项,例如:

id | brand | model | n
1  | bmw   | 525   | 2
1  | bmw   | 533   | 2
2  | audi  | a8    | 1
...

拜托,我需要帮助。

3 个答案:

答案 0 :(得分:1)

使用#include <stdio.h> #include <string.h> void main() { char str[10]; printf ("\n Enter the string: "); gets (str); printf ("\n The value of string=%s",str); int str_len; str_len = strlen (str); printf ("\n Length of String=%d\n",str_len); } 作为窗口函数:

count()

答案 1 :(得分:0)

使用相关的子查询来计算:

select c1.id, c1.brand, c1.model, (select count(*) from cars c2
                                   where c2.id = c1.id)
from cars c1

答案 2 :(得分:0)

你可以这样做:

SELECT T1.id,T1.brand,T1.model,T2.n
FROM TableName T1 JOIN
(SELECT id,brand,COUNT(1) as n
 FROM TableName
 GROUP BY id,brand) as T2 ON T1.id=T2.id AND T1.brand=T2.brand

结果:

id  brand   model   n
1   bmw     525     2
1   bmw     533     2
2   audi    a8      1

SQL Fiddle中的示例结果。