复杂的 SQL 语句(200分)

  • 主题发起人 主题发起人 Boat
  • 开始时间 开始时间
B

Boat

Unregistered / Unconfirmed
GUEST, unregistred user!
数据表共有七个字段,依次为:one、two、three、four、five、six。每个字段的值为一位数(0--9中的任一位数)。
现准备使用 Query 构件,生成结果表,如下图:
字段one,字段two ,字段three, 字段four, 字段five, 字段six
0
1
2
3
4
5
6
7
8
9
表格中对应为某字段(横坐标)的值等于左边的数字(纵坐标)的所有记录数。

SQL 语句中的 Select count……如何使用?是否还可以:
Label1.Caption:=IntToStr(Query.Value); 或
Label1.Caption:=IntToStr(SQL.Value);

要实现由原表到结果表,应该如何写 SQL 语句?

我的算法:

{从第一字段到第六字段;外层循环}
for iRow:=1 to 6 do

{数字从 0 到 9 ,内层循环}
for jCol:=0 to 9 do

[ 字段[iRow].Value:=j;
aResult[i,j]:=Query.RecordCount;
{aResult[i,j]为一整型数组,存贮 Query 的结果}
]

按这个思路的 SQL 语句中使用动态参数传值,如何写对应的语句?
若不用动态参数,又如何解决?


 
用联合查询可实现你的结果:
SELECT COUNT(one) AS one,COUNT(two) AS two,COUNT(three) AS three,
COUNT(four) AS four,COUNT(five) AS five,COUNT(six) AS six FROM table
GROUP BY 0 UNION
SELECT COUNT(one) AS one,COUNT(two) AS two,COUNT(three) AS three,
COUNT(four) AS four,COUNT(five) AS five,COUNT(six) AS six FROM table
GROUP BY 1 UNION
SELECT COUNT(one) AS one,COUNT(two) AS two,COUNT(three) AS three,
COUNT(four) AS four,COUNT(five) AS five,COUNT(six) AS six FROM table
GROUP BY 2 UNION
...
SELECT COUNT(one) AS one,COUNT(two) AS two,COUNT(three) AS three,
COUNT(four) AS four,COUNT(five) AS five,COUNT(six) AS six FROM table
GROUP BY 9 UNION
看起来很长.改以下数据结构,两个字段:

digit(0..9) bit(1..6)
用一个cross table 查询可实现同样的结果.



 
是不是oracle的数据库?
如果是,可以用decode函数来模拟cross table query.
 
gxg8816 is right
为什么不结束?
 
可惜被人抢先了.
 
接受答案了.
 
后退
顶部