如何实现这样的查询(interbase)(150分)

O

OnlyD4

Unregistered / Unconfirmed
GUEST, unregistred user!
我有以下table

Table
NO SID Quantity
------------------------------
001 01 10
001 02 20
002 01 30
002 03 25
002 04 20

现在要实现这样的查询:
查找某NO的所有SID的Quantity:并只返回一条纪录

例如: 002 30 25 20

有点类似cross table查询,不知道在interbase中如何实现

 
必须用sql实现吗?

通过编程倒可以实现
var
i : integer;
s1, s2, s3: string;
begin
i := 1;
s1 := '';
s2 := '';
s3 := '';
with query1 do
begin
sql.text := 'select * from table where no = "002"';
open;
first;
while not eof do
begin
s1 := s1 + ',Quantity' + inttostr(i) + ' short';
s2 := s2 + ',Quantity' + inttostr(i);
s3 := s3 + ','+fieldbyname('Quantity').asstring;
inc(i);
next;
end;
close;
sql.text := 'Create table temptable ( NO char(3)' + s1 + ')';
executeSQL;
sql.text := 'Insert into temptable (No' + s2 + ') values ("002"' + s3;
executeSQL;
sql.text := 'Select * from temptable';
open; // 返回表结构为 NO QUANTITY1 QUANTITY2 ....
end;
.... // some code to show the values
end;

这里未做任何检查与错误处理, 实际使用中得加上.
 
是否可以这样实现:
Var
founded: Boolean;
...
IdxNo = 给出要检索的编号;
Founded := True;
With Table Do
Begin
Open;
First;
While Not Eof and Founded Do
Begin
if FieldValues['No']=IdxNo Then
Begin
SIDStr := FieldValues['Quantity'];
Founded := False;
End;
Next;
End; {*end of index of Database*}

First;
FndStr := IdxNo+ ' '+'SidStr'+' ';
While Not Eof Do
Begin
if (FieldValues['No']=IdxNo) and (FieldValues['SID']=SidStr) Then
FndStr := FndStr + FieldValues['Quantity'];

Next;
End; {*end of index of Database*}
close;
ShowMessage(FndStr);
end;
...

其中的变量你自己定义。
 
to another_eyes:我不想用temp table,否则应该搞定了

to all:补充一点:我的SID字段位于table sub(sID, SNote)中
希望得到的结果可以是:

01 02 03 04 ......
---------------------------------
002 30 25 20 ......

即002在sid为02的时候为空
 
不用temptable恐怕不好解决
 
我看修改你的库结构可能是较好的解决办法。
 
写纯的SQL吧!
定义Cursor,声明变量,然后一次返回结果!
效率高!

具体的不写了,xixi..
(不好意思,有一个关键字忘了!自己查一下好吗?)
 
等于没说
 
代码量少的一个办法:

1.用prcedure,主要试用其带输入输出参数功能,
2.用Decision Cube中DecisionQuery
写法如下:
Select ID, sum(),count(ID)..from ProcedureName
3.再用Decision Cube中DecisionCube.DataSet := DecisionQuery
4.再用Decision Cube中Decisiondatasource和DecisionGrid,
会看到你想要的结果的.
 
Oracle 有一个Decode函数,就像一条内联的If...Then..Else语句
可以通过返回值来横向察看数据
Interbase有没有这样的函数
 

王淳霞,这个procedure怎么写?
 
大虾们,帮帮我啊
 
用存储过程可以实现.
把库组织好,利用子查询,返回应得值,没有办不到的事.


 
多人接受答案了。
 
我想是这样的:
select NO, SID,Quantity from table group by no distinct.
可能有点错,但是大致是这样的。:)
 
顶部