伤脑筋啊!!!大伙来帮忙瞅瞅啊
select id1 as 'Name',count(id1) as 'count' from table1
从数据库取得的数据形式如下:
Name count
电影-1 10
电影-2 20
电影-3 30
... ...
... ...
... ...
体育-1 10
体育-2 20
体育-3 30
... ...
... ...
... ...
游戏1 10
游戏2 20
游戏3 30
...
现在想得到的数据形式是这样的:
Name count
电影 60
体育 60
游戏 60
Sql语句该怎么写啊!!!求大侠们帮忙,俺脑袋笨,弄了半天弄不出来......
------解决方案--------------------如果楼主确定name 字段里面都含有'-',并且是取'-'前面的字符
可以这样
SQL code
SELECT
substring(name,1,charindex('-',name)-1) as name,SUM(count) as count AS count
FROM
(SELECT
id1 as Name,
count(id1) as count
FROM table)A
GROUP BY Name
------解决方案--------------------
select left(Name,charindex('-',Name)-1),sum(count) from
group by left(Name,charindex('-',Name)-1)