create table 表头(fid int)
insert into 表头 values(1)
create table 表体(fid int,物料 varchar(10),采购员 varchar(10))
insert into 表体 values(1 ,'a', 'a')
insert into 表体 values(1 ,'b', 'b')
insert into 表体 values(1 ,'c', 'a')
goselect fid = (select count(distinct 采购员) from 表体 where fid = t.fid and 采购员 < t.采购员) + 1 , 物料,采购员 from 表体 t order by fiddrop table 表头 , 表体这是wugui大侠给写的一段sql,哪位给详细解释下,包括sql执行的原理等等,理解不上去,谢谢啦
insert into 表头 values(1)
create table 表体(fid int,物料 varchar(10),采购员 varchar(10))
insert into 表体 values(1 ,'a', 'a')
insert into 表体 values(1 ,'b', 'b')
insert into 表体 values(1 ,'c', 'a')
goselect fid = (select count(distinct 采购员) from 表体 where fid = t.fid and 采购员 < t.采购员) + 1 , 物料,采购员 from 表体 t order by fiddrop table 表头 , 表体这是wugui大侠给写的一段sql,哪位给详细解释下,包括sql执行的原理等等,理解不上去,谢谢啦
表jh03有下列数据:
name score
aa 99
bb 56
cc 56
dd 77
ee 78
ff 76
gg 78
ff 501. 名次生成方式1,Score重复时合并名次
SELECT * , Place=(SELECT COUNT(DISTINCT Score) FROM jh03 WHERE Score >= a.Score)
FROM jh03 a
ORDER BY Place
结果
Name Score Place
---------------- ----------------- -----------
aa 99.00 1
ee 78.00 2
gg 78.00 2
dd 77.00 3
ff 76.00 4
bb 56.00 5
cc 56.00 5
ff 50.00 62. 名次生成方式2 , Score重复时保留名次空缺
SELECT * , Place=(SELECT COUNT(Score) FROM jh03 WHERE Score > a.Score) + 1
FROM jh03 a
ORDER BY Place
结果
Name Score Place
--------------- ----------------- -----------
aa 99.00 1
ee 78.00 2
gg 78.00 2
dd 77.00 4
ff 76.00 5
bb 56.00 6
cc 56.00 6
ff 50.00 8
create table 表头(fid int)
insert into 表头 values(1)
create table 表体(fid int,物料 varchar(10),采购员 varchar(10))
insert into 表体 values(1 ,'a', 'a')
insert into 表体 values(1 ,'b', 'b')
insert into 表体 values(1 ,'c', 'a')
goselect fid = (select count(distinct 采购员) from 表体 where 采购员 <= t.采购员) , 物料,采购员 from 表体 t order by fid
/*
fid 物料 采购员
----------- ---------- ----------
1 a a
1 c a
2 b b(所影响的行数为 3 行)
*/select fid = (select count(distinct 采购员) from 表体 where 采购员 < t.采购员) + 1, 物料,采购员 from 表体 t order by fid
/*
fid 物料 采购员
----------- ---------- ----------
1 a a
1 c a
2 b b(所影响的行数为 3 行)
*/
drop table 表头 , 表体