SQL如何将一个表里的不同条件查询结果拼接显示

Posted

tags:

篇首语:本文由小常识网(cha138.com)小编为大家整理,主要介绍了SQL如何将一个表里的不同条件查询结果拼接显示相关的知识,希望对你有一定的参考价值。

如下所示:
数据库图如下:

我要根据她的销售价格,来得出如下的表格

请问通过SQL语句 可以实现么?

关系型数据库是以一行来表示一条数据的,而不是一列。你要得出的那个表格,一行没有任何意义。

代码仅作参考:

declare @count0 int;
declare @count1 int;
declare @count2 int;
declare @count3 int;

if(object_id('TableA') is not null) drop table TableA
select getdate() as createTime, 产品名称 into TableA from [表名] where 销售金额 >=200 and 销售金额 <= 599;

if(object_id('TableB') is not null) drop table TableB
select getdate() as createTime, 产品名称 into TableB from [表名] where 销售金额 >=600 and 销售金额 <= 899;

if(object_id('TableC') is not null) drop table TableC
select getdate() as createTime, 产品名称 into TableC from [表名] where 销售金额 >=900 ;

select @count1 = count(1) from TableA;
select @count2= count(1) from TableB;
select @count3= count(1) from TableC;

set @count0 = @count1;
if @count0<@count2
begin
set @count0 = @count2;
end

if @count0<@count3
begin
set @count0 = @count3;
end

declare @i int;
set @i = 0;
while(@count1+@i)<@count0
begin
    insert into TableA values(getdate(),'');
    set @i = @i+1;
end

SET @i=0;
while(@count2+@i)<@count0
begin
    insert into TableB values(getdate(),'');
    set @i = @i+1;
end

SET @i=0;
while(@count3+@i)<@count0
begin
    insert into TableC values(getdate(),'');
    set @i = @i+1;
end

select a.产品名称 as '200到599',b.产品名称 as '600到899',c.产品名称 as '900以上'
from (select row_number() over(order by createTime) as rownum, 产品名称 from TableA) a
left join
(select row_number() over(order by createTime) as rownum, 产品名称 from TableB) b on a.rownum = b.rownum
left join
(select row_number() over(order by createTime) as rownum, 产品名称 from TableC) c on c.rownum = b.rownum

参考技术A select max(case when 销售金额 between 200 and 599 then 产品名称 else null end) 价格200到599,
max(case when 销售金额 between 600 and 899 then 产品名称 else null end) 价格600到899,
max(case when 销售金额 > 900 then 产品名称 else null end) 价格900以上
from 表名 ;
参考技术B 用 and 和 or 一起实现

将oracle 查询结果列拼接为字符串

表中的原始数据为
id name
1 a
1 b
1 c
2 a
2 b
3 a
3 c
.......
现在想查询显示的结果为
id name
1 a,b,c
2 a,b
3 a,c
.......
sql语句咋写 在线等谢谢!急!!!!!

参考技术A create table test (id int, name varchar(10) )
insert into test values (1,'a')
insert into test values (1,'b')
insert into test values (1,'c')
insert into test values (2,'a')
insert into test values (2,'b')
insert into test values (3,'a')
insert into test values (3,'c')

select id,sys_connect_by_path(name,',') from (
select id,name, row_number() over(partition by id order by name)rn,
count(*) over(partition by id ) cnt from test
) a where level=cnt
start with rn=1 connect by prior id=id and prior rn=rn-1

测试后 可用。

一楼的回答其实最容易理解了。你把它修改成动态sql 就可以了。可以不受限制了。本回答被提问者采纳
参考技术B 个数有限,可以实现
select id, max(case when rk = 1 then name else '' end)
||','||max(case when rk = 2 then name else '' end)
||','||max(case when rk = 3 then name else '' end)
from (SELECT id, name,row_number() over(partition by id order by name) rk from tab) t
group by id追问

个数是不无限的,具体的值不确定,是根据关联关系出来的

追答

那自己写存储过程吧,sql不是万能的。

参考技术C select t.id, WMSYS.WM_CONCAT(t.Name) name From table_name t GROUP BY t.id追问

中文会出现乱码,咋解决

以上是关于SQL如何将一个表里的不同条件查询结果拼接显示的主要内容,如果未能解决你的问题,请参考以下文章

mysql如何查询一个表里,同一字段不同条件数据数量?

SQL一次性查询一个字段不同条件下的统计结果

mysql:只用一条sql语句,如何查出一个表里,不同条件对应的数据条数

将多条查询结果作为like查询条件

sql从同一表里查询多条不同条件的数据

SQL一次性查询一个字段不同条件下的统计结果(另一张表的统计数量)