通过执行下列SQL排序后:
select t.bk "板块",
t.company "公司",
t.code "科目编码",
t.name "科目名称"
from tbl_test t
where t.bk='xx板块'
order by t.code,t.company 得到的结果这样的:
xx板块 A公司 1001 库存现金
xx板块 B公司 1001 库存现金
xx板块 C公司 1001 库存现金
xx板块 A公司 100101 人民币现金
xx板块 C公司 100101 人民币
xx板块 A公司 100102 美元现金
xx板块 C公司 100102 美元
xx板块 C公司 100103 欧元
xx板块 C公司 100104 英镑
xx板块 A公司 1002 银行存款
xx板块 B公司 1002 银行存款
xx板块 C公司 1002 银行存款
xx板块 A公司 100201 人民币活期存款
xx板块 B公司 100201 中信富华支行
xx板块 C公司 100201 活期存款
而我想要的结果是这样的: xx板块 B公司 1001 库存现金
xx板块 A公司 1001 库存现金
xx板块 A公司 100101 人民币现金
xx板块 A公司 100102 美元现金
xx板块 C公司 1001 库存现金
xx板块 C公司 100101 人民币
xx板块 C公司 100102 美元
xx板块 C公司 100103 欧元
xx板块 C公司 100104 英镑
xx板块 B公司 1002 银行存款
xx板块 B公司 100201 中信富华支行
xx板块 A公司 1002 银行存款
xx板块 A公司 100201 人民币活期存款请问各位高手有没有什么好办法?谢谢
select t.bk "板块",
t.company "公司",
t.code "科目编码",
t.name "科目名称"
from tbl_test t
where t.bk='xx板块'
order by t.code,t.company 得到的结果这样的:
xx板块 A公司 1001 库存现金
xx板块 B公司 1001 库存现金
xx板块 C公司 1001 库存现金
xx板块 A公司 100101 人民币现金
xx板块 C公司 100101 人民币
xx板块 A公司 100102 美元现金
xx板块 C公司 100102 美元
xx板块 C公司 100103 欧元
xx板块 C公司 100104 英镑
xx板块 A公司 1002 银行存款
xx板块 B公司 1002 银行存款
xx板块 C公司 1002 银行存款
xx板块 A公司 100201 人民币活期存款
xx板块 B公司 100201 中信富华支行
xx板块 C公司 100201 活期存款
而我想要的结果是这样的: xx板块 B公司 1001 库存现金
xx板块 A公司 1001 库存现金
xx板块 A公司 100101 人民币现金
xx板块 A公司 100102 美元现金
xx板块 C公司 1001 库存现金
xx板块 C公司 100101 人民币
xx板块 C公司 100102 美元
xx板块 C公司 100103 欧元
xx板块 C公司 100104 英镑
xx板块 B公司 1002 银行存款
xx板块 B公司 100201 中信富华支行
xx板块 A公司 1002 银行存款
xx板块 A公司 100201 人民币活期存款请问各位高手有没有什么好办法?谢谢
order by t.bk,t.company,t.code,t.name
这里的t.code要按编号顺序走,t.name是按拼音,笔划排吗?
适当的在order by 加decode(t.name,.....)可以实现你的要求。
t.bk "板块",
t.company "公司",
t.code "科目编码",
t.name "科目名称"
from
tbl_test t
where
t.bk='xx板块'
order by
substr(t.code,4),t.company,t.company,t.code [/code]
t.bk "板块",
t.company "公司",
t.code "科目编码",
t.name "科目名称"
from
tbl_test t
where
t.bk='xx板块'
order by
substr(t.code,4),t.company,t.company,t.code [/code]
declare @t table(code1 varchar(16),code2 varchar(16),code3 varchar(16),code4 varchar(16))insert into @t select 'xx板块','A公司','1001' ,'库存现金'
insert into @t select 'xx板块','B公司','1001' ,'库存现金'
insert into @t select 'xx板块','C公司','1001' ,'库存现金'
insert into @t select 'xx板块','A公司','100101','人民币现金'
insert into @t select 'xx板块','C公司','100101','人民币'
insert into @t select 'xx板块','A公司','100102','美元现金'
insert into @t select 'xx板块','C公司','100102','美元'
insert into @t select 'xx板块','C公司','100103','欧元'
insert into @t select 'xx板块','C公司','100104','英镑'
insert into @t select 'xx板块','A公司','1002' ,'银行存款'
insert into @t select 'xx板块','B公司','1002' ,'银行存款'
insert into @t select 'xx板块','C公司','1002' ,'银行存款'
insert into @t select 'xx板块','A公司','100201','人民币活期存款'
insert into @t select 'xx板块','B公司','100201','中信富华支行'
insert into @t select 'xx板块','C公司','100201','活期存款' select * from @t order by left(code3,4),code2,code3,code4/*
code1 code2 code3 code4
---------------- ---------------- ---------------- ----------------
xx板块 A公司 1001 库存现金
xx板块 A公司 100101 人民币现金
xx板块 A公司 100102 美元现金
xx板块 B公司 1001 库存现金
xx板块 C公司 1001 库存现金
xx板块 C公司 100101 人民币
xx板块 C公司 100102 美元
xx板块 C公司 100103 欧元
xx板块 C公司 100104 英镑
xx板块 A公司 1002 银行存款
xx板块 A公司 100201 人民币活期存款
xx板块 B公司 1002 银行存款
xx板块 B公司 100201 中信富华支行
xx板块 C公司 1002 银行存款
xx板块 C公司 100201 活期存款
*/
t.company "公司",
t.code "科目编码",
t.name "科目名称"
from tbl_test t
where t.bk='xx板块'
order by t.bk,substr(t.code,1,4),t.company,t.code;
B公司 1001
A公司 1001
A公司 100101
A公司 100102因为100101、100102都是1001的下级,所以它们要归在一起,而不能是
A公司 1001
B公司 1001
A公司 100101
A公司 100102不知道各位能否理解我的意思?谢谢!
t.company "公司",
t.code "科目编码",
t.name "科目名称"
from tbl_test t
where t.bk='xx板块'
order by substr(t.code,1,4),t.company,t.code;
我在3楼的那个SQL也不能得出楼主的预期?
t.bk "板块",
t.company "公司",
t.code "科目编码",
t.name "科目名称"
from
tbl_test t
where
t.bk='xx板块'
order by
substr(t.code,4),t.company,t.code
t.company "公司",
t.code "科目编码",
t.name "科目名称"
from tbl_test t
where t.bk='xx板块'
order by substr(t.code,4),t.company
t.company "公司",
t.code "科目编码",
t.name "科目名称"
from tbl_test t
where t.bk='xx板块'
order by substr(t.code,4),t.code,t.company
substr(t.code,1,4),t.company,t.code 就实现了,不过 libin_ftsafe 能给解释一下原理吗?谢谢