sql操作字符串统计

kevin_xu4365 2010-06-07 04:46:09
记录表info
地区代码 code 所有者 ower
320502 0001
320502 0011
320503 1100
。。。
分类表category

code name
0 国家
1 集体
2 个人
3 其他

一条记录可能属于多个分类,选中就记录1,否则是0。
现在按地区代码,统计出国家、集体、个人、其他的数量
求助!!!
...全文
135 11 打赏 收藏 转发到动态 举报
写回复
用AI写文章
11 条回复
切换为时间正序
请发表友善的回复…
发表回复
_Nick_ 2010-06-08
  • 打赏
  • 举报
回复

select 地区代码, sum(国家),sum(集体),sum(个人),sum(其他的数量) group by 地区代码
ShenLiang2025 2010-06-07
  • 打赏
  • 举报
回复



--1)表和数据

CREATE TABLE Record
(code varchar(32),
ower varchar(32)
)
INSERT INTO Record
SELECT '320502','0001'
UNION ALL
SELECT '320502','0011'
UNION ALL
SELECT '320503','1100'


CREATE TABLE category
(code varchar(32),
name varchar(32)
)
INSERT INTO category
SELECT '0', '国家'
UNION ALL
SELECT '1', '集体'
UNION ALL
SELECT '2', '个人'
UNION ALL
SELECT '3', '其他'

--2)查询

SELECT CODE,'国家'= SUM(CAST (SUBSTRING(ower,1,1)AS int )) ,
'集体' = SUM(CAST (SUBSTRING(ower,2,1)AS int )) ,
'个人' = SUM(CAST (SUBSTRING(ower,3,1)AS int )) ,
'其他' = SUM(CAST (SUBSTRING(ower,4,1)AS int ))
FROM Record GROUP BY code

--3结果
CODE 国家 集体 个人 其他
-------------------------------- ----------- ----------- ----------- -----------
320502 0 0 1 2
320503 1 1 0 0

(2 row(s) affected)



htl258_Tony 2010-06-07
  • 打赏
  • 举报
回复
到现在才看明白,可以写个函数。
nianran520 2010-06-07
  • 打赏
  • 举报
回复
--> 测试数据:[info]
if object_id('[info]') is not null drop table [info]
go
create table [info]([code] int,[ower] varchar(4))
insert [info]
select 320502,'0001' union all
select 320502,'0011' union all
select 320503,'1100'
--> 测试数据:[category]
if object_id('[category]') is not null drop table [category]
go
create table [category]([code] int,[name] varchar(4))
insert [category]
select 0,'国家' union all
select 1,'集体' union all
select 2,'个人' union all
select 3,'其他'

declare @sql varchar(8000)

select @sql = isnull(@sql+',','')+'sum(case substring(ower,'+rtrim(code)+'+1,1) when ''1'' then 1 else 0 end ) as ['+name+']'
from category


select @sql = 'select code,'+@sql+' from info group by code'

exec(@sql)

code 国家 集体 个人 其他
----------- ----------- ----------- ----------- -----------
320502 0 0 1 2
320503 1 1 0 0

kevin_xu4365 2010-06-07
  • 打赏
  • 举报
回复
4楼的方法是可以实现这样的效果,但是能不能更加高效一点,000100000000000000000因为有的 会有这么长的
jaydom 2010-06-07
  • 打赏
  • 举报
回复
[Quote=引用楼主 kevin_xu4365 的回复:]
记录表info
地区代码 code 所有者 ower
320502 0001
320502 0011
320503 1100
。。。
分类表category

code name
0 国家
1 集体
2 个人
3 其他

一条记录可能属于多个分类,选中就记录1,否则是0。
现在按地区代码,统计出国家、集体、个人、其他的数量
求助!!!
[/Quote]

通过什么关联的?字段怎么对不起来?
kevin_xu4365 2010-06-07
  • 打赏
  • 举报
回复
320503 是地区代码,要按这个统计
caixia615 2010-06-07
  • 打赏
  • 举报
回复
select sum(case when substring(ower,1,1)='1' then 1 else 0 end)cl1,
sum(case when substring(ower,2,1)='1' then 1 else 0 end)cl2,
sum(case when substring(ower,3,1)='1' then 1 else 0 end)cl3,
sum(case when substring(ower,4,1)='1' then 1 else 0 end)cl4 from table
wqmgxj 2010-06-07
  • 打赏
  • 举报
回复
没懂!!
jwdream2008 2010-06-07
  • 打赏
  • 举报
回复
需求不明确啊!
GOODlivelife 2010-06-07
  • 打赏
  • 举报
回复
320503 这个属于啥分类?

34,590

社区成员

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

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