2013-09-26 88 views
3

我懷疑這與我在SELECT語句中有case語句並且然後嘗試對此執行GROUP BY和ORDER BY語句的事實有關。SQL條件選擇大小寫邏輯

我有一個基於兩個表之間的連接的結果集。一個是ks2en,ks2ma和Ks2av列值爲2c,2b,2a,3c,3b等的學生表,第二個是每個學生的結果表,例如A +,A,A-,B +等。對ks2en,ks2ma和Ks2av列進行分組,並將結果相加,得出一個總數的網格,其中左下方的水平和上方的結果。然而,三種級別Ks2en,Ks2ma和Ks2av應該基於一個特定的主題,通過結果帶來:

Ks2en - English Result 
Ks2ma - Maths Result 
Ks2av - Any other subject Result 

哪裏有在ks2en,ks2ma或ks2av列沒有價值,我想小組通過向顯示爲'沒有KS2'。

我的代碼在底部對於Ks2en的英語非常出色,但我正在努力根據主題是英語,數學還是其他來改變結果的邏輯。

該主題通過名爲@SubjectName的變量進行填充。

下面是僞代碼,我想我的SQL做的事:

If @SubjectName = 'English' then 
    If ks2en ='' then 
     display 'No KS2' 
    else Ks2en 
    endif 
Else if @SubjectName = 'Mathematics' then 
    If ks2ma ='' then 
     display 'No KS2' 
    else Ks2ma 
    endif 
Else if @SubjectName <> 'English' and @SubjectName <> 'Mathematics' then 
    If ks2av ='' then 
     display 'No KS2' 
    else Ks2av 
    endif 

這是到目前爲止我的代碼是工作時@SubjectName = '英語':

DECLARE 
@DataCollection varchar(50) = '2013/14 - Autumn 1 - Targets', 
@StuYear VARCHAR(2) = '11', 
@SubjectName varchar(100) ='English', 
@TeachingGroup varchar(25) = 'Select All', 
@SubGroup varchar(10) = 'Select All' 
SELECT CASE WHEN Ks2en = '' and @SubjectName = 'English' THEN 'No KS2' ELSE ks2en END AS 'KS2', 
     nullif(count(CASE WHEN result = '' THEN 1 END),0) AS 'No Level/Grade', 
     nullif(count(CASE WHEN result IN ('U', '1a', '1b', '1c') THEN 1 END),0) AS '1/U', 
     nullif(count(CASE WHEN result IN ('U', '2a', '2b', '2c') THEN 1 END),0) AS '2/U', 
     nullif(count(CASE WHEN result IN ('G-','3c') THEN 1 END),0) AS '3c/G-', 
     nullif(count(CASE WHEN result IN ('G', '3b') THEN 1 END),0) AS '3b/G', 
     nullif(count(CASE WHEN result IN ('G+','3a') THEN 1 END),0) AS '3a/G+', 
     nullif(count(CASE WHEN result IN('F-','4c') THEN 1 END),0) AS '4c/F-', 
     nullif(count(CASE WHEN result IN('F', '4b') THEN 1 END),0) AS '4b/F', 
     nullif(count(CASE WHEN result IN('F+', '4a') THEN 1 END),0) AS '4a/F+', 
     nullif(count(CASE WHEN result IN('E-', '5c') THEN 1 END),0) AS '5c/E-', 
     nullif(count(CASE WHEN result IN('E', '5b') THEN 1 END),0) AS '5b/E', 
     nullif(count(CASE WHEN result IN('E+', '5a') THEN 1 END),0) AS '5a/E+', 
     nullif(count(CASE WHEN result IN('D-', '6c') THEN 1 END),0) AS '6c/D-', 
     nullif(count(CASE WHEN result IN('D', '6b') THEN 1 END),0) AS '6b/D', 
     nullif(count(CASE WHEN result IN('D+', '6a') THEN 1 END),0) AS '6a/D+',  
     nullif(count(CASE WHEN result IN('C-', '7c') THEN 1 END),0) AS '7c/C-',  
     nullif(count(CASE WHEN result IN('C', '7b') THEN 1 END),0) AS '7b/C',  
     nullif(count(CASE WHEN result IN('C+', '7a') THEN 1 END),0) AS '7a/C+',   
     nullif(count(CASE WHEN result IN('B-', '8c') THEN 1 END),0) AS '8c/B-', 
     nullif(count(CASE WHEN result IN('B', '8b') THEN 1 END),0) AS '8b/B', 
     nullif(count(CASE WHEN result IN('B+', '8a') THEN 1 END),0) AS '8a/B+', 
     nullif(count(CASE result WHEN 'A-' THEN 1 END),0) AS 'A-', 
     nullif(count(CASE result WHEN 'A' THEN 1 END),0) AS 'A', 
     nullif(count(CASE result WHEN 'A+' THEN 1 END),0) AS 'A+', 
     nullif(count(CASE result WHEN 'A*-' THEN 1 END),0) AS 'A*-', 
     nullif(count(CASE result WHEN 'A*' THEN 1 END),0) AS 'A*' 
    FROM student JOIN subject 
    ON subject.upn=student.upn 
WHERE 
    [StuYear] = @StuYear AND 
    [DataCollection] = @DataCollection AND 
    [Name] = @SubjectName AND (
     @TeachingGroup = 'Select All' OR 
     [TeachingGroup] = @TeachingGroup 
    ) AND (
     @SubGroup = 'Select All' OR 
     Gender = CASE 
       WHEN @SubGroup = 'GenF' THEN 'F' 
       WHEN @SubGroup = 'GenM' THEN 'M' 
      END 
    ) 
GROUP BY ks2en 
ORDER BY 
    CASE WHEN ks2en = 'W' THEN 0 ELSE 1 END, 
    LEFT(ks2en, 1), 
    RIGHT(ks2en, 1) DESC 

如果@ SubjectName ='Mathematics'然後該語句將有效地執行以下操作:

DECLARE 
@DataCollection varchar(50) = '2013/14 - Autumn 1 - Targets', 
@StuYear VARCHAR(2) = '11', 
@SubjectName varchar(100) ='Mathematics', 
@TeachingGroup varchar(25) = 'Select All', 
@SubGroup varchar(10) = 'Select All' 
SELECT CASE WHEN Ks2ma = '' and @SubjectName = 'Mathematics' THEN 'No KS2' ELSE ks2ma END AS 'KS2', 
     nullif(count(CASE WHEN result = '' THEN 1 END),0) AS 'No Level/Grade', 
**SNIP** 
     nullif(count(CASE result WHEN 'A*' THEN 1 END),0) AS 'A*' 
    FROM student JOIN subject 
    ON subject.upn=student.upn 
WHERE 
    [StuYear] = @StuYear AND 
    [DataCollection] = @DataCollection AND 
    [Name] = @SubjectName AND (
     @TeachingGroup = 'Select All' OR 
     [TeachingGroup] = @TeachingGroup 
    ) AND (
     @SubGroup = 'Select All' OR 
     Gender = CASE 
       WHEN @SubGroup = 'GenF' THEN 'F' 
       WHEN @SubGroup = 'GenM' THEN 'M' 
      END 
    ) 
GROUP BY ks2ma 
ORDER BY 
    CASE WHEN ks2ma = 'W' THEN 0 ELSE 1 END, 
    LEFT(ks2ma, 1), 
    RIGHT(ks2ma, 1) DESC 

如果@Subject名稱等於別的如科學,藝術或技術,則SQL會想以下幾點:

DECLARE 
@DataCollection varchar(50) = '2013/14 - Autumn 1 - Targets', 
@StuYear VARCHAR(2) = '11', 
@SubjectName varchar(100) ='Science', 
@TeachingGroup varchar(25) = 'Select All', 
@SubGroup varchar(10) = 'Select All' 
SELECT CASE WHEN Ks2av = '' and @SubjectName <> 'Mathematics' and @SubjectName <> 'English' THEN 'No KS2' ELSE ks2av END AS 'KS2', 
     nullif(count(CASE WHEN result = '' THEN 1 END),0) AS 'No Level/Grade', 
**SNIP** 
     nullif(count(CASE result WHEN 'A*' THEN 1 END),0) AS 'A*' 
    FROM student JOIN subject 
    ON subject.upn=student.upn 
WHERE 
    [StuYear] = @StuYear AND 
    [DataCollection] = @DataCollection AND 
    [Name] = @SubjectName AND (
     @TeachingGroup = 'Select All' OR 
     [TeachingGroup] = @TeachingGroup 
    ) AND (
     @SubGroup = 'Select All' OR 
     Gender = CASE 
       WHEN @SubGroup = 'GenF' THEN 'F' 
       WHEN @SubGroup = 'GenM' THEN 'M' 
      END 
    ) 
GROUP BY ks2av 
ORDER BY 
    CASE WHEN ks2av = 'W' THEN 0 ELSE 1 END, 
    LEFT(ks2av, 1), 
    RIGHT(ks2av, 1) DESC 

我的SQL生成網格類似於以下。結果集將看看SMAE但後臺數據將基於上述不同的標準,並會在每個小區產生不同的總計:

KS2 No Result 1/U  2/U  3c/G- 3b/G 3a/G+ 4c/F- 4b/F **snip** 
No KS2   1  NULL NULL NULL NULL NULL NULL **snip** 
2a NULL  NULL NULL NULL NULL NULL 2  1  **snip** 
3c 1   NULL NULL NULL NULL NULL NULL NULL **snip** 
3b NULL  NULL NULL NULL NULL NULL 1  NULL **snip** 
3a 1   NULL NULL NULL NULL NULL NULL NULL **snip** 
4c NULL  1  1  NULL NULL NULL NULL NULL **snip** 
4b NULL  NULL NULL NULL NULL NULL NULL NULL **snip** 
4a NULL  1  1  NULL NULL NULL NULL NULL **snip** 
5c NULL  NULL NULL NULL NULL NULL NULL NULL **snip** 
5b NULL  NULL NULL NULL NULL NULL NULL NULL **snip** 
+0

「*** g^- ***」 ???哇,你有一些艱難的評分。 – RBarryYoung

回答

1

如果我深知,你其實可以處理所有任務你自己除了過濾。我的意思是,除了用「英語/數學/其他」分支/調整結果外,你的問題與事實是幾乎相同的,除了它們來自不同的列並且你想要統一地對它們進行線程化。

對不起,如果這不是你的實際問題,但我讀/理解它的方式。

我目前不能給你一個確切的完整答案,由於幾個原因(即我不知道如何等級F + G-等與3a6b值),但我至少可以給你一些幫助/啓動你如何解決這類問題。

您想要的效果:

 If @SubjectName = 'English' then If ks2en ='' then .... 
else If @SubjectName = 'Maths' then If ks2ma ='' then .... 
else          If ks2av ='' then .... 

它很容易被一些初步的重新安排你的輸入表來完成。簡單地說,有趣的部分只是過濾成塊,並添加一些常量,將作爲有關塊一個方便的元數據:

  select "English"  as topic, ks2en as ks2 
      from YourTable 
      where ks2en <> '' 
union all 
      select "Mathematics" as topic, ks2ma as ks2 
      from YourTable 
      where ks2ma <> '' 
union all 
      select "Other"  as topic, ks2av as ks2 
      from YourTable 
      where ks2av <> '' 

注意,我增加了額外的計算鍵值名爲topic,但是這些數據現在被重新安排分成2列。您現在不能區分ks2enks2ma,但是您有一個ks2,您可以通過常量topic列檢測結果。

現在:

select 
    case when subq.ks2 = '' then 'No KS2' 
    else subq.ks2 
    end 
from 
(
      select "English"  as topic, ks2en as ks2 from YourTable where ks2en <> '' 
union all select "Mathematics" as topic, ks2ma as ks2 from YourTable where ks2ma <> '' 
union all select "Other"  as topic, ks2av as ks2 from YourTable where ks2av <> '' 
) subq 
where subq.topic = @SubjectName 

(我已經壓實選擇/工會一部分整體可讀性)

將返回KS2的實際SubjectName你想要的。除了Other部分顯然不匹配任何東西,因爲「物理」不符合「其他」。您可以通過檢查SubjectName中的已知值並將其設置爲「Other」來輕鬆解決問題。或者別的什麼。

關鍵是,由於初步的過濾器描述和粘貼它可以以任何你想要的方式重塑數據,然後很容易地將它組合/投影到你想要的東西上。

編輯:我不知道它是否存在於sql2008級別,但我認爲你也可以檢查PIVOT/UNPIVOT操作符,可以轉置列< - >行。他們使用起來很棘手,有一些限制,使他們遠不如他們可以得到的方便,但值得檢查。當然,假設我很好地理解了你的問題的核心。對不起,對不起。

+0

感謝您花時間回答。我發現很難儘可能簡單地表達我的問題而不會漏掉任何東西。我會再看看我的問題,並嘗試使它更清晰一些。我會嘗試使用一些示例數據。 – Matt

1

如果我理解您的問題,您希望您的KS2列顯示相關主題的相關KS2__數據。嘗試:

CASE 
    WHEN (Ks2en = '' and @SubjectName = 'English') 
     OR (Ks2ma = '' and @SubjectName = 'Mathematics') 
     OR (Ks2av = '' and @SubjectName <> 'Mathematics' and @SubjectName <> 'English') 
    THEN 'No KS2' 
    ELSE CASE @SubjectName 
       WHEN 'English' THEN Ks2en 
       WHEN 'Mathematics' THEN Ks2ma 
       ELSE Ks2av 
     END 
END AS KS2 

你會使用這個條款(少AS KS2)在GROUP BY和類似的條款在您的ORDER BY

ORDER BY 
CASE WHEN 
    CASE 
      WHEN (Ks2en = '' and @SubjectName = 'English') 
       OR (Ks2ma = '' and @SubjectName = 'Mathematics') 
       OR (Ks2av = '' and @SubjectName <> 'Mathematics' and @SubjectName <> 'English') 
      THEN 'No KS2' 
      ELSE CASE @SubjectName 
         WHEN 'English' THEN Ks2en 
         WHEN 'Mathematics' THEN Ks2ma 
         ELSE Ks2av 
       END 
     END = 'W' THEN 0 ELSE 1 END, 
    LEFT(CASE 
      WHEN (Ks2en = '' and @SubjectName = 'English') 
       OR (Ks2ma = '' and @SubjectName = 'Mathematics') 
       OR (Ks2av = '' and @SubjectName <> 'Mathematics' and @SubjectName <> 'English') 
      THEN 'No KS2' 
      ELSE CASE @SubjectName 
         WHEN 'English' THEN Ks2en 
         WHEN 'Mathematics' THEN Ks2ma 
         ELSE Ks2av 
       END 
     END, 1), 
    RIGHT(CASE 
      WHEN (Ks2en = '' and @SubjectName = 'English') 
       OR (Ks2ma = '' and @SubjectName = 'Mathematics') 
       OR (Ks2av = '' and @SubjectName <> 'Mathematics' and @SubjectName <> 'English') 
      THEN 'No KS2' 
      ELSE CASE @SubjectName 
         WHEN 'English' THEN Ks2en 
         WHEN 'Mathematics' THEN Ks2ma 
         ELSE Ks2av 
       END 
     END, 1) DESC