`
csstome
  • 浏览: 1543280 次
  • 性别: Icon_minigender_1
  • 来自: 北京
文章分类
社区版块
存档分类
最新评论

重复信息中case when exists 的应用...

阅读更多

重复信息中case when exists 的应用<?xml:namespace prefix = o ns = "urn:schemas-microsoft-com:office:office" />

create table #a(Fname varchar(30),fqun decimal(18,2),fclass int )
create table #b ( fclass int,name varchar(30))
insert into #a
select 'a',20,1
union all
select 'a',20,2
union all
select 'a',20,3
union all
select 'B',10,1
union all
select 'B',10,2

insert into #b
select 1,'A'
union
select 2,'B'
union
select 3,'C'

select a.*,b.name from #a a inner join #b b on a.fclass = b.fclass
要求输出格式为
Fname fqun fclass name
------------------------------ -------------------- ----------- ------------------------------
a 20.00 1 A
2 B
3 C
B 10.00 1 A
2 B

-解决-

create table #a(Fname varchar(30),fqun decimal(18,2),fclass int )

create table #b ( fclass int,name varchar(30))

insert into #a

select 'a',20,1

union all

select 'a',20,2

union all

select 'a',20,3

union all

select 'B',10,1

union all

select 'B',10,2

insert into #b

select 1,'A'

union

select 2,'B'

union

select 3,'C'

---select a.*,b.name from #a a inner join #b b on a.fclass = b.fclass

select Fnane=(case when exists (select 1 from #a where a.fname = fname and fclass<a.fclass) then '' else a.fname end)

,fqun=(case when exists (select 1 from #a where a.fqun = fqun and fclass<a.fclass) then '' else ltrim(a.fqun) end)

,a.fclass

,b.name

from #a a

inner join #b b

on a.fclass =b.fclass

order by a.Fname,a.fclass

drop table #a,#b

--------------------------

a 20.00 1 A

2 B

3 C

B 10.00 1 A

2 B

这里需要注意的就是case when exists(select 1 from #a …..

这里的是一个嵌套查询,需要处理的字段里层与外层sql 相等,然后再加一条件,不处理的条件做大小比较。根据 大于 小于 可以控制为相同记录的第一条记录,或最后一条记录

如果外层 小于 里层,就是第一条,反之,就是最后一条

例子:

declare @t table(F1 varchar(8),F2 varchar(8))

insert into @t values('01','a ')

insert into @t values('01','aa ')

insert into @t values('02','b ')

insert into @t values('02','bb ')

insert into @t values('02','bbb')

--select * from @t

select F1=(Case when exists (select 1 from @t where F1=a.F1 and F2<a.F2) then '' else F1 end)

,a.F2

from @t a

order by a..F1,a.F2

--Result--

------------

01 a

aa

02 b

bb

bbb

分享到:
评论

相关推荐

    sql case when exists not exists in not in

    在SQL查询中,`CASE WHEN`、`EXISTS`、`NOT EXISTS`以及`IN`和`NOT IN`是常用的操作符,它们用于处理复杂的条件判断和数据筛选。这些概念对于理解和编写高效的SQL语句至关重要,尤其是在数据分析和数据库管理中。 `...

    用于生成数据字典的SQL语句

    /*SQL Server数据库字典(查询所有的表结构)*/ SELECT TOP 100 PERCENT --a.id, CASE WHEN a.colorder = 1 THEN d.name ELSE '' END AS 表名, CASE WHEN a.colorder = 1 THEN isnull(f....CASE WHEN EXISTS (SELECT 1

    sql2000查看数据库表结构.sql

    主键 = case when exists(SELECT 1 FROM sysobjects where xtype='PK' and parent_obj=a.id and name in ( SELECT name FROM sysindexes WHERE indid in( SELECT indid FROM sysindexkeys WHERE id = a.id AND ...

    得到SQL数据库中所有表字段及字段中文描述 (2).pdf

    is_primary=(case when exists( select 1 from sys.indexes i,sys.index_columns ic,sys.objects o where o.type='PK' and o.name=i.name and i.index_id=ic.index_id and ic.object_id=ic.object_id and ic....

    得到SQL数据库中所有表字段及字段中文描述 (2).docx

    is_primary=CASE WHEN EXISTS( SELECT 1 FROM sys.indexes i,sys.index_columns ic,sys.objects o WHERE o.type='PK' AND o.name=i.name AND i.index_id=ic.index_id AND i.object_id=ic.object_id AND ic.column...

    SqlServer2000&2005数据库字典及表结构

    CASE WHEN EXISTS ( SELECT 1 FROM dbo.sysindexes si INNER JOIN dbo.sysindexkeys sik ON si.id = sik.id AND si.indid = sik.indid INNER JOIN dbo.syscolumns sc ON sc.id = sik.id AND sc.colid = sik....

    查询表字段信息

    通过查询SQL Server 2005数据库中的表结构信息,可以获取关于表中各字段的详细属性,这对于数据库的设计、维护以及应用程序的开发都有非常大的帮助。 #### SQL查询语句解析 以下是对给定SQL查询语句的逐行分析: ...

    SQL数据字典查询

    CASE WHEN EXISTS (SELECT 1 FROM dbo.sysindexes si INNER JOIN dbo.sysindexkeys sik ON si.id = sik.id AND si.indid = sik.indid INNER JOIN dbo.syscolumns sc ON sc.id = sik.id AND sc.colid = sik....

    获取MSSQL 表结构中字段的备注、主键等信息的sql

    1、MSSQL2000 代码如下: SELECT 表名 = case when a.colorder=1 then d.name ...case when COLUMNPROPERTY( a.id,a.name,’IsIdentity’)=1 then ‘√’else ” end, 主键 = case when exists(SELECT 1 FROM sysobjects

    Sql for mysql

    5.8 The Case Expression . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 101 5.9 The Scalar Expression Between Brackets . . . . . . . . . . . . . . . . 106 viii Contents 5.10 The Scalar...

    存储过程生成器所需Sql语言

    在SQL语言中,存储过程是一种预编译的SQL语句集合,它允许数据库开发者封装一组操作,以便在需要时重复调用。存储过程是数据库管理中的一个重要工具,它可以提高性能,减少网络流量,并提供更好的安全性。本文将详细...

    数据库中经常用到的操作和管理数据库的语句总结第1/2页

    主键 = case when exists(SELECT 1 FROM sysobjects where xtype='PK' and parent_obj=a.id and name in ( SELECT name FROM sysindexes WHERE indid in( SELECT indid FROM sysindexkeys WHERE id = a.id AND ...

    SQL Server vs Oracle 存储过程语法转换1.2.docx

    - SQL Server的`CASE WHEN...THEN...END;` 8. **游标**: - Oracle中定义游标使用`CURSOR`关键字,打开、关闭和循环使用`OPEN`, `FETCH`, `CLOSE`,如: ```sql DECLARE CurA CURSOR FOR SELECT a FROM tab; ...

    如何利用Microsoft Query查询数据库表结构_8952.doc

    "主键"=case when exists(SELECT 1 FROM sysobjects where xtype='PK' and name in (SELECT name FROM sysindexes WHERE indid in(SELECT indid FROM sysindexkeys WHERE id = a.id AND colid=a.colid))) then '√'...

    常用的SQL语句~~

    1. **CASE WHEN结构**: 这种结构可以用来在SQL查询中进行条件判断。当指定的条件为真时,返回一个值;否则返回另一个值或空值。例如,在提供的代码片段中: ```sql SELECT =case when a.colorder = 1 then d.name...

    获取SqlServer2005表结构

    在SQL Server 2005中,获取表结构是数据库管理员和开发人员进行数据管理和应用开发时常见的需求。以下是一些用于获取SQL Server 2005表结构的关键知识点,包括字段、主键、外键、递增字段以及列的描述信息。 1. **...

    经典T-SQL.doc

    在SQL语言中,T-SQL(Transact-SQL)是一种扩展的SQL,主要用于Microsoft SQL Server。以下是对经典T-SQL文档中...在实际应用中,还需要结合索引、事务、视图、存储过程等高级概念,以实现更高效和安全的数据库操作。

    查看SQLSERVER 表结构

    主键=Case When exists(Select 1 From sysobjects where Xtype='PK' And Name In ( Select Name From sysindexes Where indid In( Select indid From sysindexkeys Where ID = A.ID AND colid=A.colid ))) Then '√...

    Sql 导出表结构和表定义

    CASE WHEN EXISTS ( SELECT 1 FROM sys.objects WHERE xtype = 'PK' AND parent_obj = a.id AND name IN ( SELECT name FROM sys.indexes WHERE indid IN ( SELECT indid FROM sys.index_columns ...

    SQL 语句存储过程

    主键=case when exists(SELECT 1 FROM sysobjects where xtype='PK' and name in (SELECT name FROM sysindexes WHERE indid in(SELECT indid FROM sysindexkeys WHERE id = a.id AND colid=a.colid))) then '√' ...

Global site tag (gtag.js) - Google Analytics