怎么查询同一个品牌在数据库里面出现的次数

如何查询同一个品牌在数据库里面出现的次数
sql2000
请问如何查询一个品牌在数据里面出现的次数
目前表情况如下

表一、表名称kehu_rhypp
         id   PP_mc
        1    引航
        2    壳牌
        3    龙蟠
        4    康普顿
        5    角马
        6   统一
 
表二、表名称Kehu_Info
 
        KehuMc       jyrhypp
        张三修理厂    引航,龙蟠
        李四修理厂    壳牌,引航
        王二修理厂   角马,引航 壳牌
        小小修理厂   康普顿,壳牌,龙蟠,引航
        小王修理厂  统一


想要实现效果 获得 3种占比最多的品牌名称 剩下的显示其他
品牌名称  次数
引航     4
壳牌     3
龙蟠     2
其他     3


现在的代码 是这样的

SELECT  CASE WHEN b.[PP_mc] IS NULL THEN N'其它'
             ELSE a.[PP_mc]
        END AS [PP_mc]
       ,SUM([次数]) AS [次数]
FROM    ( SELECT a.[PP_mc],COUNT(1) AS [次数]
          FROM kehu_rhypp AS a  INNER JOIN Kehu_Info AS b ON ',' + b.[jyrhypp] + ',' LIKE '%,' + a.[PP_mc] + ',%'  GROUP BY  a.[PP_mc]
        ) AS a  LEFT JOIN ( SELECT TOP 10 [PP_mc] FROM  kehu_rhypp AS a INNER JOIN Kehu_Info AS b ON ',' + b.[jyrhypp]  + ',' LIKE '%,' + a.[PP_mc] + ',%'
                    GROUP BY a.[PP_mc] ORDER BY COUNT(1) DESC ) AS b ON a.[PP_mc] = b.[PP_mc]
GROUP BY CASE WHEN b.[PP_mc] IS NULL THEN N'其它'
              ELSE a.[PP_mc]
         END ,b.[PP_mc]
ORDER BY CASE WHEN b.[PP_mc] IS NULL THEN 2 ELSE 1 END,[次数] DESC



现在的代码执行速度比较慢!各位大侠有没有更好或者更优化的办法 解决呢!
------解决思路----------------------
可以新建一个统计表和触发器,每次插入数据时自动统计存入统计表中,
查询是直接查询统计表即可.

create table kehu_rhypp
(id int,PP_mc varchar(10))

insert into kehu_rhypp
 select 1,'引航' union all
 select 2,'壳牌' union all
 select 3,'龙蟠' union all
 select 4,'康普顿' union all
 select 5,'角马' union all
 select 6,'统一'
 
create table Kehu_Info
(KehuMc varchar(20),jyrhypp varchar(50))


-- 新建统计表
create table ppReport
(id int,cnt int)

-- 新建自动统计触发器
create trigger tr_Kehu_Info on Kehu_Info
for insert
as
begin

 select c.id,
        count(1) 'cnt'
 into #t
 from inserted a
 inner join master.dbo.spt_values b 
   on b.number between 1 and len(a.jyrhypp) and substring(','+a.jyrhypp,b.number,1)=','
 inner join kehu_rhypp c 
   on substring(a.jyrhypp,b.number,charindex(',',a.jyrhypp+',',b.number)-b.number)=c.PP_mc
 where b.type='P' 
 group by c.id
 
 update a
  set a.cnt=isnull(a.cnt,0)+b.cnt
  from ppReport a
  inner join #t b on a.id=b.id
 
 insert into ppReport(id,cnt) 
  select id,cnt 
  from #t a
  where not exists(select 1 from ppReport b where b.id=a.id)
end

-- 插入数据,自动统计.
insert into Kehu_Info
 select '张三修理厂','引航,龙蟠' union all
 select '李四修理厂','壳牌,引航' union all
 select '王二修理厂','角马,引航 壳牌' union all
 select '小小修理厂','康普顿,壳牌,龙蟠,引航' union all
 select '小王修理厂','统一'
 

-- 结果
select u.PP_mc '品牌名称',
       t.cnt '次数'
 from 
 (select top 3 id,cnt
  from ppReport a
  order by cnt desc) t
 inner join kehu_rhypp u on t.id=u.id
union all
select '其他',sum(cnt)
 from ppReport
 where id not in(select top 3 id
                 from ppReport a
                 order by cnt desc)
                 
/*
品牌名称       次数
---------- -----------
引航         3
壳牌         2
龙蟠         2
其他         3

(4 row(s) affected)
*/   

------解决思路----------------------
引用:
Quote: 引用:

Quote: 引用:

Quote: 引用:

Quote: 引用:

我理解有誤

create function fn_GetStr()
returns nvarchar(4000)
begin
declare @str nvarchar(4000)
set @str=''
select @str=@str+jyrhypp+',' from kehu_info
return @str
end
declare @strs nvarchar(4000)
set @strs=dbo.fn_GetStr()
select  pp_mc,
(LEN(@strs)-LEN(REPLACE(@strs,PP_mc,'')))/len(PP_mc) as tm,identity(int,1,1) as rn into #temp from kehu_rhypp
select PP_mc as 品牌名稱,sum(tm) as 次數 from (
select case when rn<4 then  PP_mc else '其他' end as PP_mc,
tm  from #temp as a) as b 
group by PP_mc

還是有點問題,小調一下

create function fn_GetStr()
returns nvarchar(4000)
begin
declare @str nvarchar(4000)
set @str=''
select @str=@str+jyrhypp+',' from kehu_info
return @str
end
declare @strs nvarchar(4000)
set @strs=dbo.fn_GetStr()
select  pp_mc,
(LEN(@strs)-LEN(REPLACE(@strs,PP_mc,'')))/len(PP_mc) as tm,identity(int,1,1) as rn into #temp
 from kehu_rhypp order by tm desc
select PP_mc as 品牌名稱,sum(tm) as 次數 from (
select case when rn<4 then  PP_mc else '其他' end as PP_mc,
tm  from #temp as a) as b 
group by PP_mc


还是不行,能否加我QQ 呢!

能行啊,我這邊拿你的測試數據就可以查出來。你說的不行是?


执行结果完全不对啊
4S店专供油 0
515                 0
898                 0
其他        1285

能否把你現在的數據提供出來,否則,我這邊是查不出問題的。