加个自增字段后用内联!!!
也就是Balaschen方法需要改进!
因为XH的值没有确定性,所以数据多而且乱(就是NAME千奇百怪)的话XH可能导致很多地区值都一样
这样优先按XH排序导致
A 河北 10
B 河北 10
。。。
C 北京 20
这样结果。
所以需要增加一个自增INT字段ID(基本所有数据库都有这种类型,没有任何问题),
SQL改为(这样就可以保证XH的值对于每行值都是确定且不同)
Select Name,Local,Age,
XH=(Select Count(*) from List A where A.Local=B.Local and A.ID<B.ID)
from List B order by XH,Local
SQL> Select Name,Local,Age,
2 XH=(Select Count(*) from List A where A.Local=B.Local and A.Name<B.Name)
3 from List B order by XH,Local
SQL> run
1 Select Name,Local,Age,
2 XH=(Select Count(*) from List A where A.Local=B.Local and A.Name<B.Name)
3* from List B order by XH,Local
XH=(Select Count(*) from List A where A.Local=B.Local and A.Name<B.Name)
*
ERROR 位于第 2 行:
ORA-00923: 未找到预期 FROM 关键字
------------------------
看来是XH=(Select Count(*) from List A where A.Local=B.Local and A.Name<B.Name)当中
的 from list A 不认识啊!
用标准的写法试试:
Select Name,Local,Age,
(Select Count(*) from List As A where A.Local=B.Local and A.Name<B.Name) As XH
from List As B order by XH,Local
实在不行这么写:
Select Name,Local,Age from
(Select Name,Local,Age,
(Select Count(*) from List As A where A.Local=B.Local and A.Name<B.Name) As XH
from List As B) As List order by XH,Local
Sybase11.5,DB2都能支持
我没有用过ORACLE,平时我写SQL只参照SQL 92标准
每个地方加AS.这个是SQL 92标准的写法。我想ORACLE不会不支持吧
Select Name,Local,Age,
XH=(Select Count(*) from List AS A where A.Local=B.Local and A.ID<B.ID)
from List AS B order by XH,Local
//其实括号里的LIST 不用AS
Select Name,Local,Age,
XH=(Select Count(*) from List where Local=B.Local and ID<B.ID)
from List AS B order by XH,Local
//还有,XH还是用自增字段才不会错,用NAME求COUNT有问题
》Select Name,Local,Age,
》XH=(Select Count(*) from List A where A.Local=B.Local and A.Name<B.Name)
》from List B order by XH,Local
-------------------------------------
我又颠颠的跑到了SQL SERVER 2k 中,在以前的一个库中加了这张表list,
执行如下,结果运行良好!
也实现了要求!
procedure TForm1.Button1Click(Sender: TObject);
var str:string;
begin
str:='Select Name,Local,Age, '+
' XH=(Select Count(*) from List A where A.Local=B.Local and A.Name<B.Name)'+
' from List B order by XH,Local ';
query1.Close;
query1.SQL.Clear;
query1.SQL.Add(str);
query1.open;
end;
----------------------
看来在SQL_SERVER中可以,但在oracle中不行,怎么办?
我没有遇到过这种情况! 请大家继续关注!
越来越看不懂了! 发晕!
to balaschen: 我体力不支了,您的代码我没有修改,直接copy的!
2个都提示了信息~
麻烦您再看看! 3Q
---------------------------
SQL> Select Name,Local,Age,
2 (Select Count(*) from List As A where A.Local=B.Local and A.Name<B.Name) As XH
3 from List As B order by XH,Local;
(Select Count(*) from List As A where A.Local=B.Local and A.Name<B.Name) As XH
*
ERROR 位于第 2 行:
ORA-00907: 缺少右括号
---------------------------------
SQL> Select Name,Local,Age from
2 (Select Name,Local,Age,
3 (Select Count(*) from List As A where A.Local=B.Local and A.Name<B.Name) As XH
4 from List As B) As List order by XH,Local
5 ;
(Select Count(*) from List As A where A.Local=B.Local and A.Name<B.Name) As XH
*
ERROR 位于第 3 行:
ORA-00907: 缺少右括号
=============================
to onedot:刚才机子重起了一次,现在才看到您的留言!
谢谢!
我现在就建另外一张表,试试您的说的!
3Q!
绝对可用,只是执行效率不高
select a.name,a.local,a.age from list a,list b
where a.local=b.local and a.name>=b.name
group by a.name,a.local,a.age
order by count(*) ,local