很简单的问题,知道的帮忙啊!

hk207 2008-04-21 10:01:20
如何取一个表AID的相同的BID的值!
如:
AID BID
1 g
2 e
2 3
4 d
4 h
4 w
3 4
1 u

返回为:

AID BID
1 g
1 u

AID BID
2 e
2 3

AID BID
3 4

AID BID
4 d
4 h
4 w
...全文
414 61 打赏 收藏 转发到动态 举报
写回复
用AI写文章
61 条回复
切换为时间正序
请发表友善的回复…
发表回复
qdtianjinhua 2008-04-30
  • 打赏
  • 举报
回复
学习
UltraBejing 2008-04-30
  • 打赏
  • 举报
回复
好像没那么简单,呵呵.
cson_cson 2008-04-21
  • 打赏
  • 举报
回复
declare @t table(AID varchar(10),    BID  varchar(10))
insert @t select 1, 'g'
union all select 2 , 'e'
union all select 2 , '3'
union all select 4 , 'd'
union all select 4 , 'h'
union all select 4 , 'w'
union all select 3 , '4'
union all select 1, 'u'
select a.aid,a.bid,(select count(*) from @t where aid= a.aid) as 个数 from @t a order by aid
/*
aid bid 个数
---------- ---------- -----------
1 g 2
1 u 2
2 e 2
2 3 2
3 4 1
4 h 3
4 w 3
4 d 3
*/
hk207 2008-04-21
  • 打赏
  • 举报
回复
[Quote=引用 40 楼 laowan688 的回复:]
引用 39 楼 hk207 的回复:
引用 37 楼 fxwlei3245 的回复:
select * from tb group by aid
这样应该就可以了,哈哈
个人主页http://fxwlei.poco.cn 有问题过来共同讨论!


表tb还有n个字段呢?

他脑有毛病。
那集计函数有那么用的么?
你是想要那结果么?
这次好了没?
[/Quote]

运行结果一样:

服务器: 消息 206,级别 16,状态 2,行 7
操作数类型冲突: uniqueidentifier 与 int 不兼容
服务器: 消息 409,级别 16,状态 1,行 10
count-unique aggregate 运算不能以 uniqueidentifier 数据类型作为参数。
服务器: 消息 206,级别 16,状态 1,行 13
操作数类型冲突: uniqueidentifier 与 int 不兼容
laowan688 2008-04-21
  • 打赏
  • 举报
回复
[Quote=引用 39 楼 hk207 的回复:]
引用 37 楼 fxwlei3245 的回复:
select * from tb group by aid
这样应该就可以了,哈哈
个人主页http://fxwlei.poco.cn 有问题过来共同讨论!


表tb还有n个字段呢?
[/Quote]
他脑有毛病。
那集计函数有那么用的么?
你是想要那结果么?
这次好了没?
hk207 2008-04-21
  • 打赏
  • 举报
回复
[Quote=引用 37 楼 fxwlei3245 的回复:]
select * from tb group by aid
这样应该就可以了,哈哈
个人主页http://fxwlei.poco.cn 有问题过来共同讨论!
[/Quote]

表tb还有n个字段呢?
laowan688 2008-04-21
  • 打赏
  • 举报
回复
declare @table2 table
(
ProdID int
)
declare @n int

insert @table2 select distinct(ProdID) from Prod order by ProdID
set @n=1

while @n-1 < (select count(distinct ProdID) from Prod)
begin
set @n= @n+1
select ProdID,ProdType from Prod where ProdID=(select top 1 ProdID from @table2)
delete @table2 where ProdID = (select top 1 ProdID from @table2)
end
fxwlei3245 2008-04-21
  • 打赏
  • 举报
回复
select * from tb group by aid
这样应该就可以了,哈哈
个人主页http://fxwlei.poco.cn 有问题过来共同讨论!
hk207 2008-04-21
  • 打赏
  • 举报
回复
[Quote=引用 32 楼 laowan688 的回复:]
SQL code
declare @table2 table
(
AID int
)
declare @n int

insert @table2 select distinct(ProdID) from Prod order by ProdID
set @n=1

while @n-1< (select count(distinct ProdID) from Prod)
begin
set @n= @n+1
select ProdID,ProdType from Prod where ProdID=(select top 1 ProdID from @table2)
delete @table2 where ProdID = (select top 1 ProdID from @table2)
end
[/Quote]

运行结果:

服务器: 消息 206,级别 16,状态 2,行 7
操作数类型冲突: uniqueidentifier 与 int 不兼容
服务器: 消息 409,级别 16,状态 1,行 10
count-unique aggregate 运算不能以 uniqueidentifier 数据类型作为参数。
服务器: 消息 206,级别 16,状态 1,行 13
操作数类型冲突: uniqueidentifier 与 int 不兼容
laowan688 2008-04-21
  • 打赏
  • 举报
回复


declare @table2 table
(
ProdID int
)
declare @n int


--select * from @table
insert @table2 select distinct(ProdID) from Prod order by ProdID
set @n=1

while @n-1< (select count(distinct ProdID) from Prod)
begin
set @n= @n+1
select ProdID,ProdType from Prod where ProdID=(select top 1 ProdID from @table2)
delete @table2 where ProdID = (select top 1 ProdID from @table2)
end


laowan688 2008-04-21
  • 打赏
  • 举报
回复
[Quote=引用 33 楼 kakajya 的回复:]
直接order by aid
[/Quote]
他要的是不同的结果集

不是一个结果集
kakajya 2008-04-21
  • 打赏
  • 举报
回复
直接order by aid
laowan688 2008-04-21
  • 打赏
  • 举报
回复

declare @table2 table
(
AID int
)
declare @n int

insert @table2 select distinct(ProdID) from Prod order by ProdID
set @n=1

while @n-1< (select count(distinct ProdID) from Prod)
begin
set @n= @n+1
select ProdID,ProdType from Prod where ProdID=(select top 1 ProdID from @table2)
delete @table2 where ProdID = (select top 1 ProdID from @table2)
end

hk207 2008-04-21
  • 打赏
  • 举报
回复
[Quote=引用 29 楼 laowan688 的回复:]
结果集是你想要的么?
你把你的表名字发过来,还有字段名/
我帮你写好。
[/Quote]


你把你的表名字发过来,还有字段名——

表名字Prod,字段名ProdID、ProdType
laowan688 2008-04-21
  • 打赏
  • 举报
回复
1
laowan688 2008-04-21
  • 打赏
  • 举报
回复
结果集是你想要的么?
你把你的表名字发过来,还有字段名/
我帮你写好。
hk207 2008-04-21
  • 打赏
  • 举报
回复
[Quote=引用 18 楼 laowan688 的回复:]
SQL code


declare @table table
(
AID int,
BID nvarchar(40)
)
declare @table2 table
(
AID int
)
declare @n int

insert @table select 1 , ' g' union select 2 , ' e' union select 1, 'h'

--select * from @table
insert @table2 select distinct(AID) from @table order by AID
set @n=1

while @n-1< (select count(distinct AID) from @table)
begin
set @n= @n+1
select AID,BID …
[/Quote]

这个看不懂!运行了一下,结果有!但是不明白道理!
hk207 2008-04-21
  • 打赏
  • 举报
回复
[Quote=引用 23 楼 cson_cson 的回复:]
引用 22 楼 hk207 的回复:
引用 21 楼 cson_cson 的回复:
SQL codeselect a.aid,a.bid,(select count(*) from tb where aid= a.aid) as 个数 from tb a order by aid


个数是?

你说呢?
[/Quote]

[Quote=引用 25 楼 hk207 的回复:]
举个例呗……
[/Quote]
laowan688 2008-04-21
  • 打赏
  • 举报
回复
华夏风。
我说你能把话说明白点不?
hk207 2008-04-21
  • 打赏
  • 举报
回复
举个例呗……
加载更多回复(41)

34,590

社区成员

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

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