帮助按顺序排序和显示顺序

时间:2012-04-08 09:09:49

标签: sql sqlite

以下是我为Sqlite写的bash脚本。

 #!/bin/bash
 sqlite3 file.db "CREATE TABLE table1(name text,rolln integer PRIMARY KEY,examm integer CHECK(examm >=0 and examm <= 50));"
 sqlite3 file.db "insert into table1(name,rolln,examm)values('Aldose Paul ',1234,'alpj@gmail.com',49); "
 sqlite3 file.db "insert into table1(name,rolln,examm)values('Thomas Paul',1023,'tpaul@gmail.com',45); "

 sqlite3 file.db "CREATE TABLE project(title text PRIMARY KEY,marks integer CHECK(marks >= 0 and marks <= 20)) ;"
 sqlite3 file.db "insert into project(title,marks)values('A',16);"
 sqlite3 file.db "insert into project(title,marks)values('B',14);"


 sqlite3  file.db "select rolln AS 'Roll Number' ,(examm) AS 'Total Marks'from table1 order by (examms ) ;"

1 个答案:

答案 0 :(得分:0)

作为首发,这里有一种方法可以根据成绩获得排名:

.headers on

create table foo (name text, grade int);

insert into foo values ('Joe', 45);
insert into foo values ('Anna', 98);
insert into foo values ('Julie', 78);

select name, 
       grade, 
       (select count(*) from foo t1 where t1.grade>=t2.grade) as rank  
from foo t2;

select name, 
       grade, 
       (select count(*) from foo t1 where t1.grade>=t2.grade) as rank  
from foo t2
order by rank;

将此保存为foo.sql后,我得到了这个:

[someone@somewhere tmp]$ sqlite3 < foo.sql 
name|grade|rank
Joe|45|3
Anna|98|1
Julie|78|2
name|grade|rank
Anna|98|1
Julie|78|2
Joe|45|3