两道小题儿 在别的论坛看到的

playyuer 2004-10-15 04:16:41
第一题:如下表 id字段为关键字;name 字段有若干字母
id name
------------------------
1 A,C
2 B,C
3 B
4 D,A
5 B,C,D
问如何生成表 显示 A B C D 各出现的次数。


第二题:如下表 id字段为关键字;name字段有若干字母;volue字段为等级

id name volue
-------------------------
1 A,C a
2 B,C c
3 B b
4 D,A c
5 B,C,D b

问如何生成表显示 A B C D 分别被评为等级a b c 的次数。
...全文
195 7 打赏 收藏 转发到动态 举报
AI 作业
写回复
用AI写文章
7 条回复
切换为时间正序
请发表友善的回复…
发表回复
yasusi 2004-10-15
  • 打赏
  • 举报
回复
大家都很厉害,,,值得学习,,,
playyuer 2004-10-15
  • 打赏
  • 举报
回复
如果数据如下:
create table 表(id int,name varchar(10),volue varchar(10))
insert 表 select 1,'A,C' ,'a'
union all select 2,'B,C' ,'c'
union all select 3,'B' ,'b'
union all select 4,'D,A' ,'c'
union all select 5,'B,C,C,D','d'
union all select 5,'B,C,C,D','d'

我认为:
C 与 d 组合出现的"次数"为 4,"行数" 为 2

我的答案包括 'B,C,C,D' 这种情况!


playyuer 2004-10-15
  • 打赏
  • 举报
回复
(XiYi) 这道题 是我在《精品论坛》昨天夜间回答的!
那里的题目中
name 的字母之间使用 "," 分割!
我的答案没错!


http://community.csdn.net/Expert/topic/3454/3454897.xml?temp=.6784632
而上贴
name 的字母之间使用 " " 分割!
导致我的答案结果不对!
就是因为:

LEN
返回给定字符串表达式的字符(而不是字节)个数,其中不包含尾随空格。
select len(' ')
结果总是 0

因此只要将 ' ' 都 replace 为 '_' 再计算即可

create table 表(id int,name varchar(10),volue varchar(10))
insert 表 select 1,'A,C' ,'a'
union all select 2,'B,C' ,'c'
union all select 3,'B' ,'b'
union all select 4,'D,A' ,'c'
union all select 5,'B,C,D','d'

select
sum((len(replace(name,' ','_'))-len(replace(replace(name,' ','_'),'A','')))/len('A')) as A
,sum((len(replace(name,' ','_'))-len(replace(replace(name,' ','_'),'B','')))/len('B')) as B
,sum((len(replace(name,' ','_'))-len(replace(replace(name,' ','_'),'C','')))/len('C')) as C
,sum((len(replace(name,' ','_'))-len(replace(replace(replace(name,' ','_'),' ','_'),'D','')))/len('D')) as D
from 表


select volue
,(select sum((len(replace(name,' ','_'))-len(replace(replace(name,' ','_'),'A','')))/len('A'))
from 表
where volue = a.volue) as A
,(select sum((len(replace(name,' ','_'))-len(replace(replace(name,' ','_'),'B','')))/len('B'))
from 表
where volue = a.volue) as B
,(select sum((len(replace(name,' ','_'))-len(replace(replace(name,' ','_'),'C','')))/len('C'))
from 表
where volue = a.volue) as C
,(select sum((len(replace(name,' ','_'))-len(replace(replace(name,' ','_'),'D','')))/len('D'))
from 表
where volue = a.volue) as D
from 表 a
group by volue

mgsray 2004-10-15
  • 打赏
  • 举报
回复
呵呵,之前刚刚收藏过,所以很快就翻到了,见笑:)
zjcxc 元老 2004-10-15
  • 打赏
  • 举报
回复
1楼这么快找到原帖?
zjcxc 元老 2004-10-15
  • 打赏
  • 举报
回复
本版已经有这个问题了.
mgsray 2004-10-15
  • 打赏
  • 举报
回复
有过了
http://community.csdn.net/Expert/topic/3454/3454897.xml?temp=.6784632

34,838

社区成员

发帖
与我相关
我的任务
社区描述
MS-SQL Server相关内容讨论专区
社区管理员
  • 基础类社区
  • 二月十六
  • 卖水果的net
加入社区
  • 近7日
  • 近30日
  • 至今
社区公告
暂无公告

试试用AI创作助手写篇文章吧