how to classify employers into three columns based on a condition?
I wish to classify employers who took the track into three different columns as below, based on condition of the no. of days they took in completing the courses, using the DB column lrn_complt
tells the no. of days taken :
no of emp who completed the track in
0-30d开发者_运维百科ays 30-60days 60-90days
1st column 2nd column 3rd column
Need Sql for this or if you can say logic too it may help ???
You'll need to post create table and insert statements for anyone to understand your problem correctly. Your input table, data and expected output and your Target RDBMS at the very least.
http://tkyte.blogspot.com/2005/06/how-to-ask-questions.html
Assuming you have two columns like this...
You can try inline queries like below...
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;
Basically, you need to make 3 subqueries inside one master query:
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
Looks like you need a 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;
精彩评论