2015-07-01 377 views
1

我有一張學生證,major1,major2和minor的表格。我想確定那些爲同一專業註冊兩次的學生的記錄。我需要一個函數來選擇只有那些像Major1和Major2列中的「FIN」一樣的專業人士。我至今:在兩個不同的列中標識具有兩個相同值的記錄?

CREATE VIEW A5T5 AS 
SELECT (firstname || ' ' || lastname)"FullName", StudentID AS "StudentID", Major1 as "DoubleDipping" 
FROM A5 
Group by Major1, Major2 ????? 
Having count ????? 
ORDER BY Major,LastName,FirstName; 

回答

1

我認爲你正在做這個難度比它是。如果我正確理解你的問題下面的查詢應該給你你在找什麼:

SELECT (firstname || ' ' || lastname) AS "FullName", 
     StudentID AS "StudentID", 
     Major1 as "DoubleDipping" 
    FROM A5 
    WHERE MAJOR1 = MAJOR2 

如果你真的需要這是一個函數,通常的方式返回結果集將返回開呼叫者然後將負責關閉的SYS_REFCURSOR。例如:

DECLARE 
    csr  SYS_REFCURSOR; 
    strNAME VARCHAR2(2000); 
    nID  A5.STUDENTID%TYPE; 
    strMAJOR A5.MAJOR1%TYPE; 
BEGIN 
    csr := DOUBLE_DIPPING_STUDENTS; 

    LOOP 
    FETCH csr 
     INTO NAME, ID, MAJOR; 

    WHEN csr%NOTFOUND THEN EXIT; 

    DBMS_OUTPUT.PUT_LINE(strNAME || ' ' || nID || ' ' || strMAJOR); 
    END LOOP; 

    CLOSE csr; 
END; 
+1

OMG感謝你:

CREATE OR REPLACE FUNCTION DOUBLE_DIPPING_STUDENTS RETURN SYS_REFCURSOR IS csr SYS_REFCURSOR; BEGIN OPEN csr FOR SELECT (firstname || ' ' || lastname) AS "FullName", StudentID AS "StudentID", Major1 as "DoubleDipping" FROM A5 WHERE MAJOR1 = MAJOR2; RETURN csr; END DOUBLE_DIPPING_STUDENTS; 

上述功能可以從PL/SQL塊作爲被稱爲!這是我第二週學習sql,對不起,這比我想象的要簡單得多。 好的,那麼如果我必須在三列(Major1,Major2,Minor)中的至少兩列宣佈同一個紀律時如何處理這個問題? SELECT(姓名|| '' ||姓氏)AS 「全名」, StudentID AS 「StudentID」, Major1爲 「DoubleDipping」 FROM AS WHERE MAJOR1 = MAJOR2 OR MAJOR1 =輕微或MAJOR2 = MINOR 上午我糾正? @ john3136 –

+1

請問,作爲一個單獨的問題。謝謝。 –

1

爲什麼不是那樣簡單:

SELECT名字,姓氏FROM A5 WHERE Major1 = Major2

相關問題