2017-08-17 145 views
2

有没有人知道如何在fastreport中做出不同的计数? 例 我报告:fastreport中的不同计数

Name sex 
João m 
João m 
Maria f 

在正常计数,结果将是3,但我想要一个只需要不重复的字段名称的行数。 在这种情况下,结果将是2. 任何人都可以帮助我吗?这只是一个例子。 我不能在SQL中做一个组,因为我有几个字段。

+0

你有SQL Server? – Sami

+0

不... Firebird ... – sounobre

回答

1

我不熟练使用FastReport,但我在FastReport的官方论坛上找到this page

我认为你可以通过适应你的场景来改变例子(注意,语法可能需要一些调整)。

波段:

GroupHeader1 <Sex> 
MasterData1 [Name, Sex, ...] 
GroupFooter1 [GetDistinctCount] 

脚本(仅正在与数据集由所述字段排序来算):

var 
    LastValue : string; 
    DistinctCount : integer; 

//create this event by double-clicking the event from the Object Inspector 
procedure OnGroupHeader1.OnBeforePrint; 
begin 
    if LastValue <> (<Datasetname."Sex">) then 
    Inc(DinstinctCount); 

    LastValue := <Datasetname."Sex"> 
end; 

function GetDistinctCount: string; 
begin 
    Result := IntToStr(DistinctCount); 
end; 

基部构思是,DistinctCount变量每次递增字段值更改。

脚本(应工作也与未分选的数据集):

var 
    FoundValues : array of string; 


(* !!IMPORTANT!! 
You need to initialize FoundValues array before to start counting: *) 
SetLength(FoundValues, 0); 


function IndexOf(AArray : array of string; const AValue : string) : integer; 
begin 
    Result := 0; 
    while(Result < Length(AArray)) do 
    begin 
    if(AArray[Result] = AValue) then 
     Exit; 
    Inc(Result); 
    end; 
    Result := -1; 
end; 

//create this event by double-clicking the event from the Object Inspector 
procedure OnGroupHeader1.OnBeforePrint; 
begin 
    if(IndexOf(FoundValues, <Datasetname."Sex">) = -1) then 
    begin 
    SetLength(FoundValues, Length(FoundValues) + 1); 
    FoundValues[Length(FoundValues) - 1] := <Datasetname."Sex">; 
    end; 
end; 

function GetDistinctCount: string; 
begin 
    Result := IntToStr(Length(FoundValues)); 
end; 

基部想法是,发现每个不同的值被添加到FoundValues阵列。

+0

这样,它的工作正常......但只有当它是由我想要的项目排序。如果是杂乱的,它不断的到来,以错误的方式来了... 例: 姓名性别 若昂·米 若昂米 玛丽亚˚F 计数的结果= 2是正确的 姓名性别 若昂米 玛丽亚˚F 若昂·米 结果是3 ...不正确 – sounobre

+0

@sounobre:我加入这应该也可以与一个未排序的数据集 – ExDev

+0

SetLength方法更新了答案(FoundValues,0); 在设置长度后显示错误 ':'预计 – sounobre

0

你能做到在无火鸟为GROUP BY

DECLARE @T TABLE (ID INT IDENTITY (1,1), Name NVARCHAR(25) , Sex CHAR(1)); 

INSERT INTO @T VALUES 
('Sami','M'), 
('Sami','M'), 
('Maria','F'); 

SELECT DISTINCT Name , Sex FROM @T 

您还可以创建一个View,然后在您的报告中使用它。

如果您确实需要在FastReport中那样做,那么您必须使用GroupHeaderGroupFooter来做到这一点。

How ?

您必须在OnBeforePrint事件中编写脚本。

procedure OnGroupHeader1.OnBeforePrint; 

通过双击对象检查器中的事件创建此项。

+0

这将是如何在fastreport?我迷路了... – sounobre

+0

如果你必须编写一个脚本,你不必使用'GroupHeader',而是任何可以显示值的控件。但是,这似乎是矫枉过正。我敢打赌,有一个简单的方法。 – Victoria

+0

这个脚本会是什么样子? – sounobre