日期:2014-05-17  浏览次数:20578 次

查询数据的问题
有这么一张表,数据类似如下:
col1 col2 col3
a1 b1 c1
a1 b1 c1
a1 b2 c3
a2 b2 c3
a2 b2 c4
a2 b3 c5
a3 b4 c6


现在有如下要求:
以col1为主,查询一张表,表中只要求col1列中的数据不能重复,请问如何查询


------解决方案--------------------
SQL code
----------------------------
-- Author  :fredrickhu(小F,向高手学习)
-- Date    :2011-08-11 21:16:57
-- Verstion:
--      Microsoft SQL Server 2008 (RTM) - 10.0.1600.22 (Intel X86) 
--    Jul  9 2008 14:43:34 
--    Copyright (c) 1988-2008 Microsoft Corporation
--    Enterprise Evaluation Edition on Windows NT 5.1 <X86> (Build 2600: Service Pack 3)
--
----------------------------
--> 测试数据:[tb]
if object_id('[tb]') is not null drop table [tb]
go 
create table [tb]([col1] varchar(2),[col2] varchar(2),[col3] varchar(2))
insert [tb]
select 'a1','b1','c1' union all
select 'a1','b1','c1' union all
select 'a1','b2','c3' union all
select 'a2','b2','c3' union all
select 'a2','b2','c4' union all
select 'a2','b3','c5' union all
select 'a3','b4','c6'
--------------开始查询--------------------------
select
 * 
 from
  tb t 
 where
  not exists(select 1 from tb where (col1=t.col1 and col2>t.col2) or (col1=t.col1  and col3>t.col3))
----------------结果----------------------------
/* col1 col2 col3
---- ---- ----
a1   b2   c3
a2   b3   c5
a3   b4   c6

(3 行受影响)
*/