一个查询

简介:
一个查询 3/4/2007 6:14:16 PM
今天坐在那里没太多事可做,然后就想到了上次说的那个输出格式,过去多半年了我也没找到一个思路。今天正好和三一块讨论一下。
具体要求是这样的:
有一张表。表里的某一个字段有重复值。现在要求的输出结果是如果某一个值已经出现了一次那么下面再出现时不再显示。
比如一张表:boyi55中的数据为
name    total
-----------------
boyi55      1
boyi55      2
boyi55      5
h11h99     2
h11h99     6
则要求的输出结果为:
name    total
-----------------
boyi55      1
                 2
                 5 
h11h99      2
                  6
这个在ASP里应该不难实现的。可是用T-SQL还真没写出来过。
下面看下我的实现过程:
原理:由于不能对原表直接update所以把原表里的数据排序后放到临时表里,然后对临时表进行update操作
用到两张临时表#boy 和#boyi
先把原表按name分组
select name,total into #boy from boyi55 group by name,total
再提出中间name按原值输出的列。即name相同行显示的第一列
select name,min(total) into #boyi from boyi55
下面对临时表#boy进行操作
update #boy
set name='' from #boy
inner join #boyi on #boyi.name=#boy.name
where #boy.total != #boyi.total
这样就可以得到要求的格式,但由于存在于临时表里,所以只能从临时表#boy中查询。
全部代码和结果如下:
--===========================
--按所要求格式输出内容
--按用户名显示信息,如果用户名称相同则从
--下一行开始用户名为空
--当用户名不同时继续输出用户名
--author:boyi55,date:2006-12-5
--===========================
set nocount on
if object_id('#boy') is not null
 drop table #boy
go
if object_id('#boyi') is not null
 drop table #boyi
go
if object_id('boyi55') is not null
 drop table boyi55
go
create table boyi55(name varchar(15),total int)
go
insert into boyi55 values('boyi55',8)
insert into boyi55 values('harry',10)
insert into boyi55 values('h11h99',15)
insert into boyi55 values('boyi55',50)
insert into boyi55 values('boyi55',30)
insert into boyi55 values('h11h99',11)
insert into boyi55 values('boyi55',15)
insert into boyi55 values('h11h99',16)
insert into boyi55 values('boyi55',16)
insert into boyi55 values('harry',1)
insert into boyi55 values('harry',15)
insert into boyi55 values('harry',50)
select name,total into #boy from boyi55 group by name,total
select name,min(total)as total into #boyi from boyi55 group by name
/*update #boy
set name='' from #boy,#boyi
where #boy.total not in(select total from #boyi where name=#boy.name)
*/
update #boy
set name='' from #boy
inner join #boyi on #boyi.name=#boy.name
where #boy.total != #boyi.total
select * from #boy
drop table #boy
drop table #boyi
drop table boyi55
set nocount off
 
输出结果:
name            total       
--------------- ----------- 
boyi55                    8 
                         15 
                         16 
                         30 
                         50 
h11h99                   11 
                         15 
                         16 
harry                     1 
                         10 
                         15 
                         50









本文转自 boyi55 51CTO博客,原文链接:http://blog.51cto.com/boyi55/27299,如需转载请自行联系原作者
目录
相关文章
|
6月前
|
SQL 存储 开发框架
2、SQL查询-简单查询
2、SQL查询-简单查询
33 0
|
6月前
|
SQL 关系型数据库 MySQL
3、SQL查询-复杂查询
3、SQL查询-复杂查询
80 0
|
8月前
|
NoSQL MongoDB 数据库
查询数据
查询数据
54 4
|
存储 缓存 数据库
提高Djang查询速度的9种方法
在Web应用程序中,数据库查询是一个关键的环节。优化数据库查询可以显著提高应用程序的性能和响应速度。Django作为一个高度可扩展的Web框架,提供了多种方式来优化数据库查询。本文将介绍一些常用的Django数据库查询优化技巧,从入门到精通,帮助您构建高效的应用程序。
166 1
|
9月前
|
SQL 存储
6. 复杂查询
6. 复杂查询
|
缓存 自然语言处理 网络架构
Elasticearch 查询详解 (二)
elk官网: [https://www.elastic.co/guide/en/enterprise-search-clients/index.html](https://www.elastic.co/guide/en/enterprise-search-clients/index.html) 作者主页:https://www.couragesteak.com/
Elasticearch 查询详解 (二)
|
C++
202009-1称检测点查询
202009-1称检测点查询
90 0
202009-1称检测点查询
小技巧 - 如何查询医保卡号?
小技巧 - 如何查询医保卡号?
1565 0
小技巧 - 如何查询医保卡号?
|
SQL 关系型数据库 MySQL
五、简单查询
五、简单查询
157 0
|
SQL
【查询】查询好像也可以很简单!
     还是要先说一下范围:以数据库为主的程序,b/s结构。        查询嘛,对于我来说就是SQL语句 where 后面(group、order by 前面的)的内容,把这搞定了,查询也就搞定了。
860 0

热门文章

最新文章