关于SQL查询一个简单的补集语句,求解

doudoucao 2007-02-10 08:57:08
表 data
列1 为 文本文字
列2 也为 文本文字
列3为 整数数字

想从该表里取出10个值,首先要满足,列1=A ,列2=B
如果值少于10个,那么就从列三里取出最大的值补上(补在后面). 全部以列3为倒序进行排列
SQL语句怎么写,先出20分吧,好了再加

我写的一个句子是这样的,但是不能从列三里取出值补上
...全文
913 15 打赏 收藏 转发到动态 举报
写回复
用AI写文章
15 条回复
切换为时间正序
请发表友善的回复…
发表回复
rocy520 2007-02-12
  • 打赏
  • 举报
回复
select *
wangdehao 2007-02-12
  • 打赏
  • 举报
回复
select * from (SELECT * FROM 影片 where 主演 = '刘德华' union all SELECT * FROM 影片 where 主演 = '周星驰' ) DERIVEDTBL
order by (case when 主演='刘德华' then 0 else 1 end), 观看次数 desc

w75251455 2007-02-12
  • 打赏
  • 举报
回复
--不知道你的第三列是不是唯一的

create table data(列1 char(1), 列2 char(1), 列3 int identity(1,1))
insert data select 'A', 'B'
union all select 'A', 'B'
union all select 'B', 'C'
union all select 'C', 'C'
union all select 'A', 'B'
union all select 'A', 'C'
union all select 'A', 'C'
union all select 'A', 'C'
union all select 'A', 'B'
union all select 'A', 'B'
union all select 'A', 'B'
union all select 'A', 'C'

select * from (
select top 10 * from data
order by case when 列1='A' and 列2='B'then (select count(*)from data) else 列3 end desc)a
order by 列3 desc
/*
列1 列2 列3
A C 12
A B 11
A B 10
A B 9
A C 8
A C 7
A C 6
A B 5
A B 2
A B 1
*/
dawugui 2007-02-11
  • 打赏
  • 举报
回复
还要反序

上面多了一个.

declare @i as int
set @i = 0
select @i = max(列3) from tb

select top 10 * from
(
select * from tb where 列1 = 'A' and 列2 = 'B'
union all
select 列1 = 'A' , 列2 = 'B' , 列3 = @i from tb
union all
select 列1 = 'A' , 列2 = 'B' , 列3 = @i from tb
union all
select 列1 = 'A' , 列2 = 'B' , 列3 = @i from tb
union all
select 列1 = 'A' , 列2 = 'B' , 列3 = @i from tb
union all
select 列1 = 'A' , 列2 = 'B' , 列3 = @i from tb
union all
select 列1 = 'A' , 列2 = 'B' , 列3 = @i from tb
union all
select 列1 = 'A' , 列2 = 'B' , 列3 = @i from tb
union all
select 列1 = 'A' , 列2 = 'B' , 列3 = @i from tb
union all
select 列1 = 'A' , 列2 = 'B' , 列3 = @i from tb
union all
select 列1 = 'A' , 列2 = 'B' , 列3 = @i from tb
) t
order by 列3 desc
dawugui 2007-02-11
  • 打赏
  • 举报
回复
上面多了一个.

declare @i as int
set @i = 0
select @i = max(列3) from tb

select top 10 * from
(
select * from tb where 列1 = 'A' and 列2 = 'B'
union all
select 列1 = 'A' , 列2 = 'B' , 列3 = @i from tb
union all
select 列1 = 'A' , 列2 = 'B' , 列3 = @i from tb
union all
select 列1 = 'A' , 列2 = 'B' , 列3 = @i from tb
union all
select 列1 = 'A' , 列2 = 'B' , 列3 = @i from tb
union all
select 列1 = 'A' , 列2 = 'B' , 列3 = @i from tb
union all
select 列1 = 'A' , 列2 = 'B' , 列3 = @i from tb
union all
select 列1 = 'A' , 列2 = 'B' , 列3 = @i from tb
union all
select 列1 = 'A' , 列2 = 'B' , 列3 = @i from tb
union all
select 列1 = 'A' , 列2 = 'B' , 列3 = @i from tb
union all
select 列1 = 'A' , 列2 = 'B' , 列3 = @i from tb
) t
dawugui 2007-02-11
  • 打赏
  • 举报
回复
declare @i as int
set @i = 0
select @i = max(列3) from tb

select top 10 * from
(
select * from tb where 列1 = 'A' and 列2 = 'B'
union all
select 列1 = 'A' , 列2 = 'B' , 列3 = @i from tb
union all
select 列1 = 'A' , 列2 = 'B' , 列3 = @i from tb
union all
select 列1 = 'A' , 列2 = 'B' , 列3 = @i from tb
union all
select 列1 = 'A' , 列2 = 'B' , 列3 = @i from tb
union all
select 列1 = 'A' , 列2 = 'B' , 列3 = @i from tb
union all
select 列1 = 'A' , 列2 = 'B' , 列3 = @i from tb
union all
select 列1 = 'A' , 列2 = 'B' , 列3 = @i from tb
union all
select 列1 = 'A' , 列2 = 'B' , 列3 = @i from tb
union all
select 列1 = 'A' , 列2 = 'B' , 列3 = @i from tb
union all
select 列1 = 'A' , 列2 = 'B' , 列3 = @i from tb
union all
select 列1 = 'A' , 列2 = 'B' , 列3 = @i from tb
) t
marco08 2007-02-11
  • 打赏
  • 举报
回复

create table 影片(主演 varchar(10), 观看次数 int)
insert 影片 select '刘德华', 200
insert 影片 select '周星驰', 640
insert 影片 select '李明', 100

insert 影片 select '李明', 600
insert 影片 select '刘德华', 500
insert 影片 select '周星驰', 1088

insert 影片 select '刘德华', 205
insert 影片 select '周星驰', 300
go

select *, sort=1 from 影片 where 主演='刘德华'
union all
select *, sort=2 from 影片 where 主演='周星驰'
order by sort, 观看次数 desc

--result
主演 观看次数 sort
---------- ----------- -----------
刘德华 500 1
刘德华 205 1
刘德华 200 1
周星驰 1088 2
周星驰 640 2
周星驰 300 2

(6 row(s) affected)
doudoucao 2007-02-10
  • 打赏
  • 举报
回复
但是目前的问题
select * from (SELECT * FROM 影片 where 主演 = '刘德华' union all SELECT * FROM 影片 where 主演 = '周星驰' ) DERIVEDTBL order by 观看次数 desc "

这样是 order by 观看次数 desc 是对于整个纪录集了,郁闷.
doudoucao 2007-02-10
  • 打赏
  • 举报
回复
在各位的帮助下已经搞定一部分,现在出现新问题


select * from (SELECT * FROM 影片 where 主演 = '刘德华' union all SELECT * FROM 影片 where 主演 = '周星驰' ) DERIVEDTBL order by 观看次数 desc"

现在,我想先显示 '刘德化' 的影片,再现实 '周星驰' 的影片,并且两者都按照 '观看次数' 排序.谢谢
marco08 2007-02-10
  • 打赏
  • 举报
回复
--这样行吗?

create table T(列1 char(1), 列2 char(1), 列3 int)
insert T select 'A', 'B', 10
union all select 'A', 'B', 11
union all select 'B', 'B', 12
union all select 'C', 'B', 13
union all select 'A', 'B', 14
union all select 'A', 'C', 15
go

set rowcount 4

select *, sort=1 from T
where 列1='A' and 列2='B'
union all
select *, sort=2 from T
where 列1<>'A' or 列2<>'B'
order by sort, 列3 desc

set rowcount 0

--result
列1 列2 列3 sort
---- ---- ----------- -----------
A B 14 1
A B 11 1
A B 10 1
A C 15 2

(4 row(s) affected)
marco08 2007-02-10
  • 打赏
  • 举报
回复
--try

set rowcount 10
select * from T
where 列1='A' and 列2='B'
union all
select * from T
where 列1<>'A' or 列2<>'B'
set rowcount 0
doudoucao 2007-02-10
  • 打赏
  • 举报
回复
不能一句处理掉也可以,只要能实现..
doudoucao 2007-02-10
  • 打赏
  • 举报
回复
我再把问题描述一便

列1 列2 列3
aa xy 10
bb jk 11
cc sd 12
dd as 13
... ... ...

想从该表里取出10个值,满足 :列1=A ,列2=B

但如果取出值少于10个,那么就以列三的倒序里取出值补上满足10个为止, 补上的一定要排在前2个条件后面
请大家帮忙想想办法,可否用一句sql处理掉,谢谢
doudoucao 2007-02-10
  • 打赏
  • 举报
回复
列1 列2 列3
aa xy 10
bb jk 11
cc sd 12
dd as 13
... ... ...

想从该表里取出10个值,满足 :列1=A ,列2=B

但如果取出值少于10个,那么就以列三的倒序里取出值补上满足10个.

刚才没说清楚,不好意思
chuifengde 2007-02-10
  • 打赏
  • 举报
回复
不懂

27,580

社区成员

发帖
与我相关
我的任务
社区描述
MS-SQL Server 应用实例
社区管理员
  • 应用实例社区
加入社区
  • 近7日
  • 近30日
  • 至今
社区公告
暂无公告

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