惯性聚合 高效追踪和阅读你感兴趣的博客、新闻、科技资讯
阅读原文 在惯性聚合中打开

推荐订阅源

博客园 - 三生石上(FineUI控件)
G
Google Developers Blog
Vercel News
Vercel News
Cyber Security Advisories - MS-ISAC
Cyber Security Advisories - MS-ISAC
N
Netflix TechBlog - Medium
奇客Solidot–传递最新科技情报
奇客Solidot–传递最新科技情报
钛媒体:引领未来商业与生活新知
钛媒体:引领未来商业与生活新知
Engineering at Meta
Engineering at Meta
B
Blog
博客园_首页
量子位
博客园 - 叶小钗
L
LangChain Blog
T
The Blog of Author Tim Ferriss
云风的 BLOG
云风的 BLOG
Blog — PlanetScale
Blog — PlanetScale
F
Fortinet All Blogs
S
SegmentFault 最新的问题
宝玉的分享
宝玉的分享
D
DataBreaches.Net
雷峰网
雷峰网
The Cloudflare Blog
freeCodeCamp Programming Tutorials: Python, JavaScript, Git & More
Threat Intelligence Blog | Flashpoint
Threat Intelligence Blog | Flashpoint
Last Week in AI
Last Week in AI
P
Proofpoint News Feed
TaoSecurity Blog
TaoSecurity Blog
罗磊的独立博客
MongoDB | Blog
MongoDB | Blog
The GitHub Blog
The GitHub Blog
I
Intezer
H
Help Net Security
The Hacker News
The Hacker News
The Register - Security
The Register - Security
cs.CL updates on arXiv.org
cs.CL updates on arXiv.org
AWS News Blog
AWS News Blog
V
V2EX
Microsoft Security Blog
Microsoft Security Blog
T
Tenable Blog
Spread Privacy
Spread Privacy
A
Arctic Wolf
P
Proofpoint News Feed
T
Threat Research - Cisco Blogs
Schneier on Security
Schneier on Security
C
CERT Recently Published Vulnerability Notes
让小产品的独立变现更简单 - ezindie.com
让小产品的独立变现更简单 - ezindie.com
The Last Watchdog
The Last Watchdog
Latest news
Latest news
T
Troy Hunt's Blog
L
LINUX DO - 热门话题

博客园 - lrary

JavaScript经典技巧 - lrary - 博客园 动态添加ASP.NET控件并绑定处理事件一例 - lrary - 博客园 脚本收藏 - lrary - 博客园 一些sql语句的详细解释[转] 特殊的DataGrid的绑定 - lrary - 博客园 纯脚本搞掂DataGrid表表头不动,表身滚动。(转摘) - lrary - 博客园 检验密码强度的JS类 - lrary - 博客园 数据库设计规范 V2.0 DataGrid 多行 DataGrid怎么产生一个分类的题头 AJAX 在.net的应用 sql怎么使用外连接 Asp.NET程序中常用的三十三种代码 检测含有中文字符串的实际长度 根据区位得到汉字拼音首字母(c#) 认识ASP.NET配置文件Web.config Asp.net(C#)实现验证码功能 给Repeater、Datalist和Datagrid增加自动编号列 查找和统计页面上控件
SQL语句特---殊统计(1)
lrary · 2006-05-13 · via 博客园 - lrary

表Table1:  
  项目 金额  
  X1 100  
  X2 120  
  Y1   Y3 180  
  Y2 90  
  Z1 140  
  A1   A2   B1                   300  
  B2   Z2   C1                   150  
  要得到以下结果:  
  项目 金额  
  A 200  
  B 150  
  C 50  
  X 220  
  Y 270  
  Z 190  
  注:即头字母相同的累加,一个项目中如果有多于一个值则等分(如A1   A2   B1:300则A   200,B   100)。

解决方法:

create   table   Table1(项目   varchar(20),金额   int)   
  
insert   Table1   select   'X1'                         ,100   
  
union     all         select   'X2'                         ,120   
  
union     all         select   'Y1       Y3'               ,180   
  
union     all         select   'Y2'                         ,90   
  
union     all         select   'Z1'                         ,140   
  
union     all         select   'A1     A2       B1',300   
  
union     all         select   'B2       Z2   C1'         ,150   
  
go   
  
select * from table1
  
--查询处理   
    
  
--处理临时表   
  declare   @i   int   
  
select   @i=max(len(项目))   from   Table1   
  
set   rowcount   @i   
  
select   id=identity(int)   into   #t   from   syscolumns   a,syscolumns   b   
  
set   rowcount   0   
    
  
--统计出结果   
  select   项目,项目1=substring(a.项目,b.id,1),金额   
  
into   #t1   from   Table1   a,#t   b   
  
where   len(a.项目)>=b.id   and   patindex('   [^   ]%',substring('   '+a.项目,b.id,8000))=1   
    
  
select   项目=a.项目1,金额=sum(a.金额/b.cnt)     
  
from   #t1   a,(select   项目,cnt=count(*)   from   #t1   group   by   项目)b   
  
where   a.项目=b.项目   
  
group   by   a.项目1   
    
  
drop   table   #t,#t1   
  
go   
    
  
--删除测试   
  drop   table   Table1