不好意思。上面的表結構 scores | integer | not null 應該是times | integer | not null 我現在想先統計出學習了多少次times,然後再去考慮排名,現我用 select user_id,sum(case when section_id=4 then ceil(times/2) else times end) as studycount from record group by user_id order by user_id結果集為:user_id:1387498 studycount:139 user_id:1387499 studycount:1 user_id:1387502 studycount:1 user_id:1387503 studycount:1 user_id:1387513 studycount:0 user_id:1387527 studycount:44 ///////////////////////////按道理 user_id:1387513 studycount:0 它的studycount應該1才對啊,怎麼會是零呢?
ceiling也試過了,還是零,真是郁悶了
你 select ceiling(1/2); 测试一下,难道是bug?
to:foolbirdflyfirst 我試過了select ceiling(1/2); 返回的是0,我查了下PostgreSQL的資料, 如果要返回1的話,select ceiling(1.1/2); 需要這樣的數才行。另外我請教你。我現在忽略這個問題,我現在已通過: select user_id,sum(case when section_id=4 then ceil(times/2) else times end) as studycount from record group by user_id order by user_id 得到我需要的結果集,但現要想每條記錄加上一個排行,通過studycount可以得到這個用戶的成績排在哪一位,請問這語句要怎麼寫啊?
select user_id,sum(case when section_id=4 then ceiling(trunc(times,1)/2) else times end) as studycount from record group by user_id order by user_id我用上面的辦法就可以了。先用trunc函數將times補上.0一個小數,如果不能整除2的數就會+1。現在麻煩大大幫幫想下怎樣可以得到每個用戶根據studycount取得到他的成績排名。謝謝!
scores | integer | not null
應該是times | integer | not null
我現在想先統計出學習了多少次times,然後再去考慮排名,現我用
select user_id,sum(case when section_id=4 then ceil(times/2) else times end) as studycount from record group by user_id order by user_id結果集為:user_id:1387498
studycount:139
user_id:1387499
studycount:1
user_id:1387502
studycount:1
user_id:1387503
studycount:1
user_id:1387513
studycount:0
user_id:1387527
studycount:44
///////////////////////////按道理
user_id:1387513
studycount:0
它的studycount應該1才對啊,怎麼會是零呢?
我試過了select ceiling(1/2); 返回的是0,我查了下PostgreSQL的資料,
如果要返回1的話,select ceiling(1.1/2); 需要這樣的數才行。另外我請教你。我現在忽略這個問題,我現在已通過:
select user_id,sum(case when section_id=4 then ceil(times/2) else times end) as studycount from record group by user_id order by user_id
得到我需要的結果集,但現要想每條記錄加上一個排行,通過studycount可以得到這個用戶的成績排在哪一位,請問這語句要怎麼寫啊?
或者你去sql版问问,加上排名列,一条不嵌套查询的语句估计够呛。:)