查询数据的问题

yuyangyangde 2011-08-11 06:25:30
有这么一张表,数据类似如下:
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列中的数据不能重复,请问如何查询
...全文
103 12 打赏 收藏 转发到动态 举报
写回复
用AI写文章
12 条回复
切换为时间正序
请发表友善的回复…
发表回复
gogodiy 2011-08-12
  • 打赏
  • 举报
回复
给出你想要的结果。
q465897859 2011-08-12
  • 打赏
  • 举报
回复

select col1,
MAX(case when RIGHT(col1,1)<>RIGHT(col2,1) then col2 else '' end) as col2,
MAX(case when RIGHT(col2,1)<>RIGHT(col3,1) then col3 else '' end) as col3
from tb
group by col1
cxmcxm 2011-08-11
  • 打赏
  • 举报
回复
declare  @tb table([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
(select distinct col1,col2,col3 from @tb) a
where not exists(select * from @tb where col1=a.col1 and
(col2>=a.col2 and col3>a.col3 or col2>a.col2 and col3>=a.col3))
cxmcxm 2011-08-11
  • 打赏
  • 举报
回复
select * from 
(select distinct col1,col2,col3 from t) a
where not exists(select * from t where col1.a.col1 and not (col2>=a.col2 or col3>=a.col3))
--小F-- 2011-08-11
  • 打赏
  • 举报
回复
----------------------------------------------------------------
-- 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 行受影响)
*/
AcHerat 元老 2011-08-11
  • 打赏
  • 举报
回复

select distinct *
from tb t
where not exists (select 1 from tb where col1=t.col1 and (col2>t.col2 or (col2=t.col2 and col3>t.col3)))

--or

select col1,col2,col3
from(
select *,rid=row_number() over (partition by col1 order by getdate())
from tb
)t
where rid = 1
yuyangyangde 2011-08-11
  • 打赏
  • 举报
回复
这张表所有列的数据我都要查询出来
NBDBA 2011-08-11
  • 打赏
  • 举报
回复
select distinct *
from tab a
where not exists (
select 1 from tab
where col1=a.col1 and ( col2 >col2
or col2 = a.col2 and col3>a.col3)
)


NBDBA 2011-08-11
  • 打赏
  • 举报
回复
or

not exists
+
distinct
中国风 2011-08-11
  • 打赏
  • 举报
回复
or Cte
select *
from
(select Row=row_number()over(partition by Col1 order by Col1),* from T1)t
where Row=1
NBDBA 2011-08-11
  • 打赏
  • 举报
回复
not exists
+
group by
中国风 2011-08-11
  • 打赏
  • 举报
回复
用 group by Col1
版本:presto-server-0.214.tar软件版本 presto-cli-0.214-executableCentOS71、presto的起因 hadoop ---hdfs----MR(java)-----hivehive底层原理用MR,速度比较慢,公司hadoop集群主要集中于晚上到凌晨,平日工作时间负载不是很高。但在工作时间内,公司业务人员有实时查询的需求,现在主要借助于hive提供业务人员的查询。hive是基于MR类的SQL查询工具,他会输入的查询SQL解析为MapReduce,能极大的降低使用大数据门槛,让一般的业务人员可以直接准对大数据进行查询,但是有一个利弊,它的查询基于MR,会让人等待比较着急,等待的时间可能是几个小时或者一天。 spark基于内存提高改良的hive,sql,现在factbook在hive上面开发一套利器,准对hive可以通过sql语句快速查询,presto。2、Facebook为何开发Presto  Facebook的2011的数据仓库存储在少量大型hadoopfs集群,Hive是FaceBook在几年前专门为Hadoop打造的一款数据仓库工具,在以前,facebook的科学家和分析师一直靠hive进行数据分析.但hive使用MR作为底层计算框架,是专为批处理设计的,但是随着数据的不断增多,使用hive进行一个简单的数据查询可能要花费分钟或者几个小时,显然不能满足查询需求,FaceBooke也调研了其他比hive更快的工具,但是他们需要在功能有限的条件下做简单操作,以至于无法操作Facebook庞大的数据要求。2012年开始研究自己的框架--presto,每日可以超过1pb查询,而且速度比较快,faceBook声称Presto的性能比hive要好上10倍或者100倍,presto和hive都是facebook开发的 Presto是一个开源的分布式SQL查询引擎,适用于交互式查询数据量支持GB到PB字节。Presto的设计和编写完全是为了解决Facebook这样规模的商业数据仓库交互式分析和处理速度的问题Presto可以做什么 Presto支持在线数据查询,包括Hive kafka Cassandra关系数据库以及专门数据存储,一条Presto查询可以将多个数据源进行合并,可以跨越整个组织进行分析。Presto以分析师的需求作为目标,他们期望相应速度小于1秒到几分钟,Presto要么在使用速度的快的昂贵的商业方案,提高内存,要么是消耗大量的硬件进行快速查询。(128G 64G)本套课程教给如何在企业环境中使用Presto技术。

34,587

社区成员

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

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