SQL 語法一問

2007-06-16 7:48 pm
我有一個 table 入面有三個 attributes.

Id , Marks , Grade

同埋有一千個 records.

Id 同 Marks 係入左野, 依家就要用 SQL 根據 marks 既高低黎入 Grade.

如果 order by marks DESC 之後:
第 1-10 個 records, 就比 'A'
第 11-100 個 records, 就比 'B'
第 101-300 個 records, 就比 'C'
第 301-600 個 records, 就比 'D'
第 601-1000 個 records, 就比 'E'

但係有個好麻煩既 condition, 就係如果第 10 同 11 個係同 marks, 咁佢地都係入 'A', 12-100 先入'B'.

有無可能一條 SQL 做曬呢?

Database 係 MySQL 5.0
Table TYPE 係 INNODB

謝謝回答
更新1:

假設 10-100 都係同分, 咁 1-100 都係 'A', 101 已經係 'B'. 另外關於 datatype 既問題: id, marks 係 INTEGER(8), grade 係 VARCHAR(1).

更新2:

如果一句不行的話, 用五句 SQL 也可以.

更新3:

Sorry 打錯字 ~.~ 假設 10-100 都係同分, 咁 1-100 都係 'A', 101 已經係 'C'. 係 'C' 先對, 即係一個 'B' 都無.

更新4:

Sorry 打錯字 ~.~ 假設 10-100 都係同分, 咁 1-100 都係 'A', 101 已經係 'C'. 係 C 先對, 即係一個 B 都無.

回答 (1)

2007-06-17 8:02 pm
✔ 最佳答案
For proper display, single quotes around grade letter have been removed.

update mytable set grade = null

update mytable set grade=A where id in (select top 10 id from mytable order by marks desc)
update mytable set grade=A where marks = (select min(marks) from mytable where grade = A)

update mytable set grade=B where id in (select top 100-(select count(id) from mytable where grade = A) id from mytable where grade is null order by marks desc)
update mytable set grade=B where marks = (select min(marks) from mytable where grade = B)

update mytable set grade=C where id in (select top 300-(select count(id) from mytable where grade in (A,B)) id from mytable where grade is null order by marks desc)
update mytable set grade=C where marks = (select min(marks) from mytable where grade = C)

update mytable set grade=D where id in (select top 600-(select count(id) from mytable where grade in (A,B,C)) id from mytable where grade is null order by marks desc)
update mytable set grade=D where marks = (select min(marks) from mytable where grade = D)

update mytable set grade=E where id in (select top 1000-(select count(id) from mytable where grade in (A,B,C,D)) id from mytable where grade is null order by marks desc)
update mytable set grade=E where marks = (select min(marks) from mytable where grade = E)

2007-06-17 15:43:27 補充:
回雁妮, your sql do not achieve the desired result. I explain later.OK 5 sql statements will do.找出第10個最高分數,高於這分數或相同的就是Aupdate mytable set grade=A where marks >= (select marks from mytable order by marks desc limit 9,1)

2007-06-17 15:47:52 補充:
找出第100個最高分數,高於這分數或相同的就是B,A除外update mytable set grade=B where marks >= (select marks from mytable order by marks desc limit 99,1) and grade is null

2007-06-17 15:48:59 補充:
找出第300個最高分數,高於這分數或相同的就是C,A,B除外update mytable set grade=C where marks >= (select marks from mytable order by marks desc limit 299,1) and grade is null

2007-06-17 15:52:06 補充:
找出第600個最高分數,高於這分數或相同的就是D,A,B,C除外update mytable set grade=D where marks >= (select marks from mytable order by marks desc limit 599,1) and grade is nullupdate mytable set grade=E where grade is null

2007-06-17 16:02:03 補充:
To 雁妮select min(marks) from mytable order by marks desc limit 600I have not tested it, but my intuition tells me that this sql returns the lowest mark rather the 600th highest mark.

2007-06-17 16:19:08 補充:
[ 假設 10-100 都係同分, 咁 1-100 都係 'A', 101 已經係 'B' ]如果這情況出現,你我的sql都做不到

2007-06-18 09:26:01 補充:
[ 假設 10-100 都係同分, 咁 1-100 都係 A, 101 已經係 C. 係 C 先對, 即係一個 B 都無. ]Do it backward, from E first. This is similar to 雁妮's method.update mytable set grade=E

2007-06-18 09:27:00 補充:
找出第600個最高分數,高於這分數或相同的就是Dupdate mytable set grade=D where marks >= (select marks from mytable order by marks desc limit 599,1)找出第300個最高分數,高於這分數或相同的就是Cupdate mytable set grade=C where marks >= (select marks from mytable order by marks desc limit 299,1)

2007-06-18 09:27:43 補充:
找出第100個最高分數,高於這分數或相同的就是Bupdate mytable set grade=B where marks >= (select marks from mytable order by marks desc limit 99,1)找出第10個最高分數,高於這分數或相同的就是Aupdate mytable set grade=A where marks >= (select marks from mytable order by marks desc limit 9,1)


收錄日期: 2021-04-25 20:33:14
原文連結 [永久失效]:
https://hk.answers.yahoo.com/question/index?qid=20070616000051KK01411

檢視 Wayback Machine 備份