为同一组的第一个添加新行
Add new row for first of same group
我是 Firebird 的新手,需要您的帮助。我知道 Firebird 的任务不是做一个漂亮的打印。
我有一个具有以下减少输出的存储过程:
Drink
Name
Cola
Marcus
Cola
Siegfried
Fanta
Jochen
Beer
Anna
Beer
Manuel
我想在每组 Drink 的第一个值之前添加新的行和列,其中包含 Drink 的名称。
所以输出应该是:
Drink
Drinks for print
Name
Cola
Cola
Cola
Marcus
Cola
Siegfried
Fanta
Fanta
Fanta
Jochen
Beer
Beer
Beer
Anna
Beer
Manuel
我正在使用 Firebird 2.5.8
假设您不想修改现有的存储过程,而是想 'decorate' 存储过程的输出,您需要执行如下操作:
execute block
returns (
drink varchar(50),
"Drink for print" varchar(50),
name varchar(50))
as
declare drink_intermediate varchar(50);
declare name_intermediate varchar(50);
begin
for select drink, name
from drinks
order by drink
into drink_intermediate, name_intermediate
do
begin
if (drink is distinct from drink_intermediate) then
begin
drink = drink_intermediate;
"Drink for print" = drink_intermediate;
name = null;
suspend;
"Drink for print" = null;
end
name = name_intermediate;
suspend;
end
end
也就是每更换一次饮品,就需要多加一行
您也可以在 DSQL 中执行类似的操作,但是由于 distinct
:
,当您有很多行时,这可能会更昂贵
select distinct
drink,
case a.type when 1 then drink end as "Drink for print",
case a.type when 2 then name end as name
from drinks
cross join (
select 1 as type from RDB$DATABASE
union all
select 2 as type from RDB$DATABASE
) a
order by drink, type
也就是说,对于存储过程的每一行,我们添加一个额外的行,其中填充了 "Drinks for print"
列并且未填充 name
列,然后我们采用不同的(以删除重复的行),并排序以获得一致的输出。
我是 Firebird 的新手,需要您的帮助。我知道 Firebird 的任务不是做一个漂亮的打印。
我有一个具有以下减少输出的存储过程:
Drink | Name |
---|---|
Cola | Marcus |
Cola | Siegfried |
Fanta | Jochen |
Beer | Anna |
Beer | Manuel |
我想在每组 Drink 的第一个值之前添加新的行和列,其中包含 Drink 的名称。
所以输出应该是:
Drink | Drinks for print | Name |
---|---|---|
Cola | Cola | |
Cola | Marcus | |
Cola | Siegfried | |
Fanta | Fanta | |
Fanta | Jochen | |
Beer | Beer | |
Beer | Anna | |
Beer | Manuel |
我正在使用 Firebird 2.5.8
假设您不想修改现有的存储过程,而是想 'decorate' 存储过程的输出,您需要执行如下操作:
execute block
returns (
drink varchar(50),
"Drink for print" varchar(50),
name varchar(50))
as
declare drink_intermediate varchar(50);
declare name_intermediate varchar(50);
begin
for select drink, name
from drinks
order by drink
into drink_intermediate, name_intermediate
do
begin
if (drink is distinct from drink_intermediate) then
begin
drink = drink_intermediate;
"Drink for print" = drink_intermediate;
name = null;
suspend;
"Drink for print" = null;
end
name = name_intermediate;
suspend;
end
end
也就是每更换一次饮品,就需要多加一行
您也可以在 DSQL 中执行类似的操作,但是由于 distinct
:
select distinct
drink,
case a.type when 1 then drink end as "Drink for print",
case a.type when 2 then name end as name
from drinks
cross join (
select 1 as type from RDB$DATABASE
union all
select 2 as type from RDB$DATABASE
) a
order by drink, type
也就是说,对于存储过程的每一行,我们添加一个额外的行,其中填充了 "Drinks for print"
列并且未填充 name
列,然后我们采用不同的(以删除重复的行),并排序以获得一致的输出。