2011-11-16 93 views
0

這裏是我當前的查詢:添加了一列的列在查詢

SELECT sac.cred, s.status, (SELECT NVL (csl.census_dates, tl.census_dates) 
             FROM schema.sections cs, schema.sections_ls csl, schema.terms tl 
             WHERE cs.course_sections_id = csl.course_sections_id(+)AND csl.pos(+) = 1 AND cs.term = tl.terms_id 
              AND tl.pos = 1 AND cs.course_sections_id = cs2.course_sections_id AND ROWNUM = 1)AS censusDate, 
             (SELECT NVL (p.ssn, 'xxx-xx-xxxx') FROM schema.person p 
             WHERE p.id = sac.person_id) AS ssn, 
             //schema.person_name(sac.person_id, 'FML') as fml, 
             //schema.person_name(sac.person_id, 'LF') as lf 
           FROM schema.student_acad_cred sac JOIN schema.statuses s 
            ON s.student_acad_cred_id = sac.student_acad_cred_id 
           JOIN schema.terms tl ON sac.term = tl.terms_id 
           JOIN schema.student_course_sec scs ON sac.student_course_sec = scs.student_course_sec_id 
           JOIN schema.course_sections cs2 ON scs.course_section = cs2.course_sections_id 
           JOIN schema.terms t ON tl.terms_id = t.terms_id 
           WHERE sac.person_id = '1111111111' 
            AND (s.status IN ('A', 'N') OR (s.status = 'D' AND final_grade IS NOT NULL)) 
            AND s.pos = '1'AND tl.pos = '1' AND tl.terms_id = 'spring'; 

而且這裏的結果:

cred  status currentDate censusDate  ssn 
====  ====== =========== ==========  === 
    3   N  11/16/2011 12/15/2011  xxx-xx-xxxx 
    4   N  11/16/2011 12/15/2011  xxx-xx-xxxx 
    3   N  11/16/2011 12/15/2011  xxx-xx-xxxx 
    4   N  11/16/2011 12/15/2011  xxx-xx-xxxx 
    1   N  11/16/2011 12/15/2011  xxx-xx-xxxx 

好吧,我所要做的是使用總和()(或其他函數)合計所有拉出的積分時間。因此在這種情況下,所有的信用小時數的總和將是'15'。有沒有辦法在查詢中做到這一點?理想情況下,我想要這樣的事情:

cred  status currentDate censusDate  ssn 
====  ====== =========== ==========  === 
15   N  11/16/2011 12/15/2011  xxx-xx-xxxx 
+0

爲什麼要顯示單個記錄的SUM信用信息?另外,當你根據一個不變的'person_id'獲得'ssn'時,爲什麼'ssn'對於同一個人會有所不同呢?事實上,爲什麼不只是''加入'schema.person'而不是做一個子選擇? – ean5533

+0

我真的需要一個理由嗎?大聲笑,信用時間的總和,毫無疑問,需要。 reults完全組成,我只是在打字。加入人員需要3次以上的連接(我沒有設置表格),這使得查詢執行時間大約需要兩倍。 – Dan

+0

還有3個連接?它只需要一個連接:'JOIN schema.person p ON sac.person_id = p.person_id'。你本質上已經在做,只是以一種非常奇怪的方式。我問的原因是因爲你的輸出沒有任何意義 - 「ssn」永遠不會有所不同,它永遠都是一樣的。在我們確定了這個事實之後,看到你只是輸出同一行5次這很愚蠢......這很愚蠢。相反,你應該做一個「GROUP BY」並且只產生一行,這是「SUM」有意義的地方。 – ean5533

回答

1

要做到這一點的方法是GROUP BY所有其他列。由於您使用的是can't group by aliased columns directly(在Oracle和大多數RDMBSes中),因此您必須將整個事件包裝在另一個查詢中並在那裏進行分組。

SELECT SUM(cred), status, censusDate, ssn 
FROM 
    (SELECT sac.cred, s.status, 
      (SELECT NVL (csl.census_dates, tl.census_dates) 
      FROM schema.sections cs, schema.sections_ls csl, schema.terms tl 
      WHERE cs.course_sections_id = csl.course_sections_id(+)AND csl.pos(+) = 1 AND cs.term = tl.terms_id 
       AND tl.pos = 1 AND cs.course_sections_id = cs2.course_sections_id AND ROWNUM = 1)AS censusDate, 
      (SELECT NVL (p.ssn, 'xxx-xx-xxxx') FROM schema.person p 
      WHERE p.id = sac.person_id) AS ssn, 
      //schema.person_name(sac.person_id, 'FML') as fml, 
      //schema.person_name(sac.person_id, 'LF') as lf 
    FROM schema.student_acad_cred sac JOIN schema.statuses s 
     ON s.student_acad_cred_id = sac.student_acad_cred_id 
    JOIN schema.terms tl ON sac.term = tl.terms_id 
    JOIN schema.student_course_sec scs ON sac.student_course_sec = scs.student_course_sec_id 
    JOIN schema.course_sections cs2 ON scs.course_section = cs2.course_sections_id 
    JOIN schema.terms t ON tl.terms_id = t.terms_id 
    WHERE sac.person_id = '1111111111' 
     AND (s.status IN ('A', 'N') OR (s.status = 'D' AND final_grade IS NOT NULL)) 
     AND s.pos = '1'AND tl.pos = '1' AND tl.terms_id = 'spring') 
GROUP BY status, censusDate, ssn; 

這看起來很醜,但實際上並沒有可怕的性能影響。

0
select groupcol, sum(cred) 
from table1 
group by rollup(groupcol)