2011-05-09 73 views
-1

我希望根據第一條的規定,將按照下面三條不同欄目劃分的僱主分類。他們在完成課程的日子裏,使用數據庫列lrn_complt告訴沒有。天的拍攝:如何根據條件將僱主分爲三列?

沒有EMP是誰完成了

0-30days    30-60days   60-90days 
1st column   2nd column   3rd column 

軌道不需要對SQL本或者你可以說邏輯也可能會有所幫助???

+0

您正在使用哪個數據庫? – 2011-05-09 20:52:50

+0

nexus teratom,並與SQL一起工作 – Mohammad 2011-05-09 21:08:42

回答

2

您需要發佈創建表和插入語句,以便任何人正確理解您的問題。您的輸入表,數據和預期輸出以及您的目標RDBMS至少。

http://tkyte.blogspot.com/2005/06/how-to-ask-questions.html

假設你有兩列這樣的...

您可以嘗試像下面嵌入式查詢...

Select id, 
     (select count(*) from courses where days between 0 and 30) 0_to_30_days, 
     (select count(*) from courses where days between 31 and 60) 0_to_30_days 
     (select count(*) from courses where days between 61 and 90) 0_to_30_days 
from courses; 
0

基本上,你需要做一個內部的子查詢3主查詢:

SELECT 
(SELECT COUNT(*) FROM EMPLOYER WHERE LRN_COMPLT BETWEEN 0 AND 30) AS COLUMN1, 
(SELECT COUNT(*) FROM EMPLOYER WHERE LRN_COMPLT BETWEEN 31 AND 60) AS COLUMN2, 
(SELECT COUNT(*) FROM EMPLOYER WHERE LRN_COMPLT BETWEEN 61 AND 90) AS COLUMN3 
FROM DUAL 
0

看起來像是需要一個PIVOT

Select id, 
     COUNT(CASE WHEN lrn_complt between 0 and 30 THEN 1 END) Group1, 
     COUNT(CASE WHEN lrn_complt between 31 and 60 THEN 1 END) Group2, 
     COUNT(CASE WHEN lrn_complt between 61 and 90 THEN 1 END) Group3 
from courses;