2012-02-02 122 views
2
 SELECT 
    COUNT(id), AgeRange 
    FROM 
    (
    select 
     id, 
     case 
     when age < 0 then 'less than 0' 
     when age >= 0 and age <=30 then '0-30' 
     when age >= 31 and age <=60 then '31-60' 
     when age >= 61 and age <=90 then '61-90' 
     when age >= 91 then '91+' 
     when age = null then 'NO INFORMATION' 
     else 'no catagory' 
    end AS AgeRange 
from queue 
where DATE between '01-Apr-2011' and '05-May-2011' 
) T 
GROUP BY 
    AgeRange; 

我希望這些ageRanges(0-30,31-60,61-90)是動態的。這意味着這些值應該來自一個表格(因爲這些是由用戶設置的)。用戶可以根據需要設置多個值來獲取結果。我怎麼能做到這一點..其迫切的,有人可以plz幫助...動態情況下

+1

請勿使用'CASE'。加入表格。 – 2012-02-03 02:20:50

回答

4

假設你有一個第二個表,如:

ASSIST_TABLE 
FromAge|ToAge|Text 
0|30|'0-30' 

的,你可以做這樣的事情。

SELECT 
COUNT(id), 
FROM 
(
select 
    id, 
    ISNULL(select text from ASSIST_TABLE 
    where Age between FromAge andToAge),'NO CATEGORY') AS AGERANGE 
from queue 
where DATE between '01-Apr-2011' and '05-May-2011' 
) T 
GROUP BY 
AgeRange; 

如果你想堅持你的case-statement,你將不得不聲明動態。這意味着首先生成查詢,將其存儲到變量中並最終執行。這是更多的工作!

+0

thanx的迴應...但是我想堅持案件陳述,因爲我想要一個結果像foll .. – Pramod 2012-02-02 13:05:31

+0

thanx的迴應...但是我想堅持案件陳述,因爲我想要一個結果像foll .. 計數範圍 --------------- 10 31-60 115 61-90 – Pramod 2012-02-02 13:07:10

+0

好的Pramod。爲此,您需要定義一個遊標來循環遍歷第二個表。 1.將第一部分寫入var,2.循環遍歷第二個表並寫入case-clause 3.將語句的結尾寫入var。比超越它。 – chris 2012-02-02 13:10:27

1

這是另一個嘗試:動態SQL,它不是最終的答案,但你會明白這一點。

CREATE PROCEDURE sp_generate_valid_choices (IN p_request_id Bigint) 
BEGIN 
DECLARE num_rows INT DEFAULT 0; 
DECLARE no_more_rows BINARY; 
DECLARE no_more_subrows BINARY; 
DECLARE loop_cntr INT DEFAULT 0; 
DECLARE var_choice_group BIGINT DEFAULT 0; 

-- Declare Cursor for the loop through the constraint_groups 
DECLARE cur_constraint_group CURSOR FOR 
SELECT distinct choice_constraint_group FROM aip_choice_constraint 
    WHERE choice_id_rule_parameter IN (SELECT choice_id FROM aip_request_detail 
             where request_id = p_request_id); 
-- DECLARE 'handlers' for exceptions 
DECLARE CONTINUE HANDLER FOR NOT FOUND 
SET no_more_rows := TRUE; 

-- OPEN CURSOR AN PROCESS CONSTRAINT_GROUPS 
OPEN cur_constraint_group; 
SELECT FOUND_ROWS() INTO num_rows; 

choice_group_loop: LOOP 
    FETCH cur_constraint_group 
    INTO var_choice_group; 

IF no_more_rows THEN 
    CLOSE cur_constraint_group; 
    LEAVE choice_group_loop; 
END IF; 
-- PAYLOAD 
-- INSERT THE VALID CHOCIES INTO tmp_aip_valid_choices 
SELECT @var_sql_query := CONCAT('INSERT INTO tmp_aip_valid_choices ','SELECT ',p_request_id,' as request_id, `aip_choice_constraint`.`choice_constraint_id`,`aip_choice_constraint`.`choice_constraint_group` 
    ,AVG(IF (`aip_request_detail`.`choice_varchar_value`', `aip_choice_constraint`.`choice_constraint_operator`, '\'',`aip_choice_constraint`.`choice_constraint_value`, '\'',',1,0)) AS VALID 
FROM `aip_choice_constraint` 
LEFT JOIN `aip_request_detail` ON `aip_request_detail`.`choice_id` = `aip_choice_constraint`.`choice_id_rule_parameter` 
WHERE `aip_choice_constraint`.choice_constraint_group =' , var_choice_group, 
' GROUP BY `aip_choice_constraint`.choice_constraint_group') 
FROM `aip_choice_constraint` WHERE `aip_choice_constraint`.choice_constraint_group = var_choice_group; 
PREPARE SQL_STATEMENT FROM @var_sql_query; 
EXECUTE SQL_STATEMENT; 
-- INCREMENT THE COUNTER 
SET loop_cntr = loop_cntr + 1; 

END LOOP choice_group_loop; 

INSERT INTO tmp_aip_valid_choices_for_request 
(request_id, choice_id) 
SELECT DISTINCT p_request_id, choice_id FROM aip_choice ac 
-- RULE 1 ALL CHOICES WITHOUT CONSTRAINTS 
WHERE ac.choice_id NOT IN (SELECT choice_id_rule_target FROM aip_choice_constraint) 
-- RULE 2 ALL CHOICES WITH CONSTRAINTS, THAT ARE NOT YET ANSWERED 
OR ac.choice_id NOT IN (SELECT choice_id_rule_target FROM aip_choice_constraint 
WHERE choice_id_rule_parameter IN (SELECT choice_id FROM aip_request_detail WHERE request_id = p_request_id)) 
-- RULE 3 ALL CHOICES WITH CONSTRAINTS, THAT ARE TRUE 
OR ac.choice_id IN (SELECT choice_id_rule_target FROM aip_choice_constraint 
WHERE choice_constraint_group IN (SELECT choice_constraint_group FROM tmp_aip_valid_choices WHERE request_id = p_request_id AND VALID = 1)); 

END // 
Delimiter ; 

該代碼是MySQL的,所以你不能複製它。但這個想法是一樣的。準備好您的語句作爲字符串,然後執行它。