求一句SQL语句,请各位大哥帮忙想想。。

cnhgj 2004-03-18 09:05:07
有一四个列的表,如果四列值全都一样,那么显示一行
如果行里有两个列一样,那么将会显示三行
如果有三个列都一样,那么显示二行

如果有表:
A B C D
1 2 1 2
1 2 3 4
其中ABCD是四个列名要求并成一列,并得到如下查询结果:

列名
1
2
1
2
3
4
...全文
42 18 打赏 收藏 转发到动态 举报
写回复
用AI写文章
18 条回复
切换为时间正序
请发表友善的回复…
发表回复
cnhgj 2004-03-18
  • 打赏
  • 举报
回复
好,谢谢两位。。
zjcxc 元老 2004-03-18
  • 打赏
  • 举报
回复
这样也没问题,效果一样.
cnhgj 2004-03-18
  • 打赏
  • 举报
回复
谢谢,请问这样的话,有什么问题吗?

declare @Tmp table(a int,b int,c int,d int,bh int IDENTITY (1, 1))
insert @Tmp select 5, 6, 5, 6
union all select 5, 6, 7, 8
union all select 5, 2, 4, 2
select distinct bh,a from (select bh,a from @tmp union all
select bh,b from @tmp union all
select bh,c from @tmp union all
select bh,d from @tmp) abc
zjcxc 元老 2004-03-18
  • 打赏
  • 举报
回复
没有理解错嘛,你的目的就是要去重复,将union all改成union就可以了
zjcxc 元老 2004-03-18
  • 打赏
  • 举报
回复
declare @Tmp table(a int,b int,c int,d int,e int,bh int IDENTITY (1, 1))
insert @tmp select 1,2,1,2,1
insert @tmp select 1,2,3,4,5

select bh,a from @tmp union
select bh,b from @tmp union
select bh,c from @tmp union
select bh,d from @tmp union
select bh,e from @tmp order by bh

/*--测试结果

bh a
----------- -----------
1 1
1 2
2 1
2 2
2 3
2 4
2 5

(所影响的行数为 7 行)
--*/
j9988 2004-03-18
  • 打赏
  • 举报
回复
表设计要改,不改最终会成问题。
j9988 2004-03-18
  • 打赏
  • 举报
回复
如果记录只有三五条或几十条(不考虑效率):


declare @a table(id int identity,A int,B int,C int,D int)
insert @a select 1, 2, 1, 2
union all select 1, 2, 3, 4

select id,v from (
select id,1 as f,A as V from @a
union all
select id,2 as f,b as V from @a
union all
select id,3 as f,C as V from @a
union all
select id,4 as f,d as V from @a ) T1
where not exists (select 1 from (
select id,1 as f,A as V from @a
union all
select id,2 as f,b as V from @a
union all
select id,3 as f,c as V from @a
union all
select id,4 as f,d as V from @a ) T2
where id=T1.id and F<T1.F and V=T1.V )
order by id,F
j9988 2004-03-18
  • 打赏
  • 举报
回复
先从表设计开始吧,要这么设计:
PK(ID,F)
ID F V
1 A 1
1 B 2
1 C 1
1 D 2
2 A 1
2 B 2
2 C 3
2 D 4
cnhgj 2004-03-18
  • 打赏
  • 举报
回复
declare @Tmp table(a int,b int,c int,d int,e int,bh int IDENTITY (1, 1))
insert @tmp select 1,2,1,2,1
insert @tmp select 1,2,3,4,5
select bh,a from @tmp union all
select bh,b from @tmp union all
select bh,c from @tmp union all
select bh,d from @tmp union all
select bh,e from @tmp order by bh

结果是
bh a
1 1
1 2
1 1
1 2
1 1
2 5
2 4
2 3
2 2

表的内容是
a b c d e bh
1 2 1 2 1 1
1 2 3 4 5 2

但我想要的结果是
bh a
1 1
1 2
2 1
2 2
2 3
2 4
2 5
zjcxc 元老 2004-03-18
  • 打赏
  • 举报
回复
每行都判断?
cnhgj 2004-03-18
  • 打赏
  • 举报
回复
但是不只一行啊
zjcxc 元老 2004-03-18
  • 打赏
  • 举报
回复
是上面这种意思吗?
zjcxc 元老 2004-03-18
  • 打赏
  • 举报
回复
--测试

--测试数据
declare @表 table(A int,B int,C int,D int)
insert @表 select 1,2,3,4

--查询
select a from @表
union
select b from @表
union
select c from @表
union
select d from @表

/*--测试结果

a
-----------
1
2
3
4

(所影响的行数为 4 行)

--*/
zjcxc 元老 2004-03-18
  • 打赏
  • 举报
回复

a
-----------
1
2
3
4

(所影响的行数为 4 行)
zjcxc 元老 2004-03-18
  • 打赏
  • 举报
回复
--测试

--测试数据
declare @表 table(A int,B int,C int,D int)
insert @表 select 1,2,1,2

--查询
select a from @表
union
select b from @表
union
select c from @表
union
select d from @表

/*--测试结果
a
-----------
1
2

(所影响的行数为 2 行)
--*/
zjcxc 元老 2004-03-18
  • 打赏
  • 举报
回复
--你是想返回不重复的数据吧?
cnhgj 2004-03-18
  • 打赏
  • 举报
回复
就是把四个列的内容进行判断
如果当四个列的内容都一样的话,那么只返回一行
如果四个列中有三个一样的话,那么返回两行
如果有两个一样的话,那么返回三行
如果全都不一样的话,那么返回四行

但是表中其实只有一行数据
zjcxc 元老 2004-03-18
  • 打赏
  • 举报
回复
看不明白

34,590

社区成员

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

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