抢分题,大家看看,谢谢啦!

intellectual2 2008-09-02 12:42:53
create table test( id int identity(1,1),[水果]varchar(20),[商店]varchar(20),[售货员]varchar(20),[区域]varchar(20))
insert into test([水果],[商店],[售货员],[区域])
select

'香蕉' ,'超市','张', '上海'
union all select
'香蕉', '超市', '王','上海'
union all select
'香蕉', '超市', '赵', '上海'
union all select
'香蕉','超市' ,'黄', '上海'
union all select
'苹果' ,'批发站', '孙', '上海'
union all select
'苹果','批发站' ,'潘', '上海'
union all select
'苹果' ,'批发站' ,'刘' ,'上海'
union all select
'苹果' ,'批发站', '顾', '上海'
union all select
'西瓜' ,'零售小贩', '夏', '北京'
union all select
'葡萄' ,'零售小贩' ,'夏' ,'北京'
union all select
'葡萄' ,'零售小贩' ,'朱' ,'北京'
union all select
'黄桃','零售小贩', '朱' ,'北京'
union all select
'葡萄' ,'零售小贩' ,'钱' ,'天津'
union all select
'黄桃' ,'零售小贩', '钱', '天津'
union all select
'西瓜','零售小贩' ,'钱', '天津'

/*需要的查询结果
1 香蕉 超市 张 上海
2 香蕉 超市 王 上海
3 香蕉 超市 赵 上海
4 香蕉 超市 黄 上海
5 苹果 批发站 孙 上海
6 苹果 批发站 潘 上海
7 苹果 批发站 刘 上海
8 苹果 批发站 顾 上海
13 葡萄 零售小贩 钱 天津
14 黄桃 零售小贩 钱 天津
15 西瓜 零售小贩 钱 天津
说明:
1.第9-12行记录地区是北京,北京所对应的是零售小贩,零售小贩一共包含西瓜,葡萄,黄桃.
夏,朱两个售货员没有卖过全部这三种水果.应此这些记录是不符合要求的.去除.
2.第13-15行记录中地区是北京,北京所对应的是零售小贩,零售小贩一共包含西瓜,葡萄,黄桃.
钱三种水果都卖,所以记录符合要求.
3.第1-8行也是这种规则,都符合要求所以返回在结果中
4.查询中不能直接引用苹果,批发站,潘,上海等数据.



*/



...全文
137 18 打赏 收藏 转发到动态 举报
写回复
用AI写文章
18 条回复
切换为时间正序
请发表友善的回复…
发表回复
-晴天 2008-09-02
  • 打赏
  • 举报
回复
表述不清楚.
intellectual2 2008-09-02
  • 打赏
  • 举报
回复
原表数据
id 水果 商店 售货员 区域
1 香蕉 超市 张 上海
2 香蕉 超市 王 上海
3 香蕉 超市 赵 上海
4 香蕉 超市 黄 上海
5 苹果 批发站 孙 上海
6 苹果 批发站 潘 上海
7 苹果 批发站 刘 上海
8 苹果 批发站 顾 上海
9 西瓜 零售小贩 夏 北京
10 葡萄 零售小贩 夏 北京
13 葡萄 零售小贩 朱 北京
14 黄桃 零售小贩 朱 北京
15 葡萄 零售小贩 钱 天津
16 黄桃 零售小贩 钱 天津
17 西瓜 零售小贩 钱 天津
xiangzi21cn 2008-09-02
  • 打赏
  • 举报
回复
select t.*
from test as t
inner join
(select a.售货员,a.区域,a.商店
from
(select 售货员,区域,商店,count(水果)as m
from test
group by 售货员,区域,商店) as a
inner join
(select 区域,商店,count(水果) as n
from #a
group by 区域,商店)as b
on a.m=b.n and
a.区域=b.区域 and
a.商店=b.商店) as v
on t.售货员=v.售货员
=----------------------------------------------
id 水果 商店 售货员 区域
----------- -------------------- -------------------- -------------------- --------------------
1 香蕉 超市 张 上海
2 香蕉 超市 王 上海
3 香蕉 超市 赵 上海
4 香蕉 超市 黄 上海
5 苹果 批发站 孙 上海
6 苹果 批发站 潘 上海
7 苹果 批发站 刘 上海
8 苹果 批发站 顾 上海
13 葡萄 零售小贩 钱 天津
14 黄桃 零售小贩 钱 天津
15 西瓜 零售小贩 钱 天津

(所影响的行数为 11 行)
anovice 2008-09-02
  • 打赏
  • 举报
回复

/*create table test( id int identity(1,1),[水果]varchar(20),[商店]varchar(20),[售货员]varchar(20),[区域]varchar(20))
insert into test([水果],[商店],[售货员],[区域])
select

'香蕉' ,'超市','张', '上海'
union all select
'香蕉', '超市', '王','上海'
union all select
'香蕉', '超市', '赵', '上海'
union all select
'香蕉','超市' ,'黄', '上海'
union all select
'苹果' ,'批发站', '孙', '上海'
union all select
'苹果','批发站' ,'潘', '上海'
union all select
'苹果' ,'批发站' ,'刘' ,'上海'
union all select
'苹果' ,'批发站', '顾', '上海'
union all select
'西瓜' ,'零售小贩', '夏', '北京'
union all select
'葡萄' ,'零售小贩' ,'夏' ,'北京'
union all select
'葡萄' ,'零售小贩' ,'朱' ,'北京'
union all select
'黄桃','零售小贩', '朱' ,'北京'
union all select
'葡萄' ,'零售小贩' ,'钱' ,'天津'
union all select
'黄桃' ,'零售小贩', '钱', '天津'
union all select
'西瓜','零售小贩' ,'钱', '天津'
*/
/*需要的查询结果
1 香蕉 超市 张 上海
2 香蕉 超市 王 上海
3 香蕉 超市 赵 上海
4 香蕉 超市 黄 上海
5 苹果 批发站 孙 上海
6 苹果 批发站 潘 上海
7 苹果 批发站 刘 上海
8 苹果 批发站 顾 上海
13 葡萄 零售小贩 钱 天津
14 黄桃 零售小贩 钱 天津
15 西瓜 零售小贩 钱 天津
说明:
1.第9-12行记录地区是北京,北京所对应的是零售小贩,零售小贩一共包含西瓜,葡萄,黄桃.
夏,朱两个售货员没有卖过全部这三种水果.应此这些记录是不符合要求的.去除.
2.第13-15行记录中地区是北京,北京所对应的是零售小贩,零售小贩一共包含西瓜,葡萄,黄桃.
钱三种水果都卖,所以记录符合要求.
3.第1-8行也是这种规则,都符合要求所以返回在结果中
4.查询中不能直接引用苹果,批发站,潘,上海等数据.


*/
--1 查询每个区域销售水果的数量
--select count(distinct [水果]) as cnt ,[商店],[区域] from test group by [商店],[区域]

--2 查询每个售货员销售水果数量
--select count([水果]) as cnt,[商店],[售货员],[区域] from test group by [商店],[售货员],[区域]

--3
select c.*
from (select count(distinct [水果]) as cnt ,[商店],[区域] from test group by [商店],[区域]) a,
(select count([水果]) as cnt,[商店],[售货员],[区域] from test group by [商店],[售货员],[区域]) b,
test c
where a.cnt = b.cnt and a.[区域]= b.[区域] and b.[区域]= c.[区域] and a.[商店]= b.[商店] and b.[商店]=c.[商店] and b.[售货员] = c.[售货员]
/*
(所影响的行数为 11 行)

id 水果 商店 售货员 区域
----------- -------------------- -------------------- -------------------- --------------------
1 香蕉 超市 张 上海
2 香蕉 超市 王 上海
3 香蕉 超市 赵 上海
4 香蕉 超市 黄 上海
5 苹果 批发站 孙 上海
6 苹果 批发站 潘 上海
7 苹果 批发站 刘 上海
8 苹果 批发站 顾 上海
13 葡萄 零售小贩 钱 天津
14 黄桃 零售小贩 钱 天津
15 西瓜 零售小贩 钱 天津

(所影响的行数为 11 行)*/
anovice 2008-09-02
  • 打赏
  • 举报
回复
select c.*
from (select count(distinct [水果]) as cnt ,[商店],[区域] from test group by [商店],[区域]) a,
(select count([水果]) as cnt,[商店],[售货员],[区域] from test group by [商店],[售货员],[区域]) b,
test c
where a.cnt = b.cnt and a.[区域]= b.[区域] and b.[区域]= c.[区域] and a.[商店]= b.[商店] and b.[商店]=c.[商店] and b.[售货员] = c.[售货员]
Herb2 2008-09-02
  • 打赏
  • 举报
回复
[Quote=引用 14 楼 intellectual2 的回复:]
13楼能够空手写出来,厉害!
但是结果不对.
哪个售货员卖了他所对应的商店中包含的所有水果?
[/Quote]
这是每个售货员买的水果数:
(select 售货员,区域,商店,ss=count(distinct 水果) from tb group by 售货员,区域,商店) b,
这个每个商店卖的水果数:
(select 区域,商店,ss=count(distinct 水果) from tb group by 售货员,区域,商店) c

只要他们相等就行了:
b.区域= c.区域 and b.商店 = c.商店 and b.ss = c.ss

有什么问题吗?
intellectual2 2008-09-02
  • 打赏
  • 举报
回复
13楼能够空手写出来,厉害!
但是结果不对.
哪个售货员卖了他所对应的商店中包含的所有水果?
Herb2 2008-09-02
  • 打赏
  • 举报
回复
id 水果 商店 售货员 区域


select * from tb a,
(select 售货员,区域,商店,ss=count(distinct 水果) from tb group by 售货员,区域,商店) b,
(select 区域,商店,ss=count(distinct 水果) from tb group by 售货员,区域,商店) c
where a.售货员=b.售货员 and a.区域=b.区域 and a.商店 = b.商店
and b.区域= c.区域 and b.商店 = c.商店 and b.ss = c.ss
intellectual2 2008-09-02
  • 打赏
  • 举报
回复
区域不用看了,太搞了.
只要回答一个问题就解决了我的问题
哪个售货员卖了他所对应的商店中包含的所有水果?
intellectual2 2008-09-02
  • 打赏
  • 举报
回复
1 香蕉' ,'超市','张', '上海'
张先生所对应的,[商店] 是超市超市中只有香蕉' 一种水果.张先生卖了这种水果.所以第一条记录符合要求.
神龟,你看看有办法吗?
dawugui 2008-09-02
  • 打赏
  • 举报
回复
[Quote=引用 9 楼 intellectual2 的回复:]
每个售货员 是否包含了 对应的每组[区域] 所对应的每组[商店] 所包含的全部 水果 就可以了
如果售货员包含了这些水果就保留,如果不包含就去除.
[/Quote]
1-8怎么也没看出来能符合你的要求.
intellectual2 2008-09-02
  • 打赏
  • 举报
回复
每个售货员 是否包含了 对应的每组[区域] 所对应的每组[商店] 所包含的全部 水果 就可以了
如果售货员包含了这些水果就保留,如果不包含就去除.
-晴天 2008-09-02
  • 打赏
  • 举报
回复
或者:
create table test( id int identity(1,1),[水果]varchar(20),[商店]varchar(20),[售货员]varchar(20),[区域]varchar(20)) 
insert into test([水果],[商店],[售货员],[区域])
select
'香蕉' ,'超市','张', '上海'
union all select
'香蕉', '超市', '王','上海'
union all select
'香蕉', '超市', '赵', '上海'
union all select
'香蕉','超市' ,'黄', '上海'
union all select
'苹果' ,'批发站', '孙', '上海'
union all select
'苹果','批发站' ,'潘', '上海'
union all select
'苹果' ,'批发站' ,'刘' ,'上海'
union all select
'苹果' ,'批发站', '顾', '上海'
union all select
'西瓜' ,'零售小贩', '夏', '北京'
union all select
'葡萄' ,'零售小贩' ,'夏' ,'北京'
union all select
'葡萄' ,'零售小贩' ,'朱' ,'北京'
union all select
'黄桃','零售小贩', '朱' ,'北京'
union all select
'葡萄' ,'零售小贩' ,'钱' ,'天津'
union all select
'黄桃' ,'零售小贩', '钱', '天津'
union all select
'西瓜','零售小贩' ,'钱', '天津'
go
select * from test where 售货员 in(
select 售货员 from test where 商店='零售小贩' group by 售货员 having count(水果)>2
)
or 商店<>'零售小贩'
--select * from test
go
drop table test
/*
id 水果 商店 售货员 区域
----------- -------------------- -------------------- -------------------- --------------------
1 香蕉 超市 张 上海
2 香蕉 超市 王 上海
3 香蕉 超市 赵 上海
4 香蕉 超市 黄 上海
5 苹果 批发站 孙 上海
6 苹果 批发站 潘 上海
7 苹果 批发站 刘 上海
8 苹果 批发站 顾 上海
13 葡萄 零售小贩 钱 天津
14 黄桃 零售小贩 钱 天津
15 西瓜 零售小贩 钱 天津

(11 行受影响)
*/
intellectual2 2008-09-02
  • 打赏
  • 举报
回复
可以从集合和列名中取得啊,看看每个售货员 是否包含了要 每组[区域] 对应的每组[商店] 所包含的全部 水果 就可以了
如果包含就保留,如果不包含就去除.
-晴天 2008-09-02
  • 打赏
  • 举报
回复
create table test( id int identity(1,1),[水果]varchar(20),[商店]varchar(20),[售货员]varchar(20),[区域]varchar(20)) 
insert into test([水果],[商店],[售货员],[区域])
select
'香蕉' ,'超市','张', '上海'
union all select
'香蕉', '超市', '王','上海'
union all select
'香蕉', '超市', '赵', '上海'
union all select
'香蕉','超市' ,'黄', '上海'
union all select
'苹果' ,'批发站', '孙', '上海'
union all select
'苹果','批发站' ,'潘', '上海'
union all select
'苹果' ,'批发站' ,'刘' ,'上海'
union all select
'苹果' ,'批发站', '顾', '上海'
union all select
'西瓜' ,'零售小贩', '夏', '北京'
union all select
'葡萄' ,'零售小贩' ,'夏' ,'北京'
union all select
'葡萄' ,'零售小贩' ,'朱' ,'北京'
union all select
'黄桃','零售小贩', '朱' ,'北京'
union all select
'葡萄' ,'零售小贩' ,'钱' ,'天津'
union all select
'黄桃' ,'零售小贩', '钱', '天津'
union all select
'西瓜','零售小贩' ,'钱', '天津'
go
delete from test where 售货员 in(
select 售货员 from test where 商店='零售小贩' group by 售货员 having count(水果)<3
)
select * from test
go
drop table test
/*
id 水果 商店 售货员 区域
----------- -------------------- -------------------- -------------------- --------------------
1 香蕉 超市 张 上海
2 香蕉 超市 王 上海
3 香蕉 超市 赵 上海
4 香蕉 超市 黄 上海
5 苹果 批发站 孙 上海
6 苹果 批发站 潘 上海
7 苹果 批发站 刘 上海
8 苹果 批发站 顾 上海
13 葡萄 零售小贩 钱 天津
14 黄桃 零售小贩 钱 天津
15 西瓜 零售小贩 钱 天津

(11 行受影响)
*/
KermitYue 2008-09-02
  • 打赏
  • 举报
回复
select * from test a
where 商店='零售小贩'
and exists(select 1 from test b where a.商店=b.商店 and a.售货员=b.售货员 and b.水果='葡萄')
and exists(select 1 from test b where a.商店=b.商店 and a.售货员=b.售货员 and b.水果='黄桃')
and exists(select 1 from test b where a.商店=b.商店 and a.售货员=b.售货员 and b.水果='西瓜')
or 商店<>'零售小贩'

说明:
查询条件描述不是太清楚。

数据结构设计不太合理。
intellectual2 2008-09-02
  • 打赏
  • 举报
回复
原表和结果都有了,规则也说了.数据比较搞.还请耐心看.我想会有人看得懂的.
有问题我在线回答
-晴天 2008-09-02
  • 打赏
  • 举报
回复
查询中不能直接引用苹果,批发站,潘,上海等数据.
苹果,第一列,批发站,第二列,潘,第三列,上海,第四列.
这四列数据都不能引用,我拿什么去判断该去掉哪几行?

22,207

社区成员

发帖
与我相关
我的任务
社区描述
MS-SQL Server 疑难问题
社区管理员
  • 疑难问题社区
  • 尘觉
加入社区
  • 近7日
  • 近30日
  • 至今
社区公告
暂无公告

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