从一个表中选择列,但从另一个表中选择WHERE子句?

时间:2018-10-16 00:47:38

标签: sql oracle oracle11g

所以我有3个表,并被告知要输入这3个特定列作为参数。我的问题是,我是否可以仅凭投资组合说明找到投资者的名字和姓氏,还是也必须加入表格?

(第一个select语句引用我没有列出的表)

Investor 
InvestorNum   FirstName    LastName     Address
-----------    --------     -------     ------   

Portfolio
Port num     Investor Num     Port Description    InvestorNum
---------   -------------     ----------------    ----------

Stock 
Stock Code     Stock Name
----------     ----------


FUNCTION PKG_FN_Q4
(P_Portfolio_Description varchar2, P_Investor_Number number, P_Stock_Name varchar2) 
RETURN varchar2
AS
V_Average_Cost varchar2(50); 
V_Output varchar2(500);
V_Investor_First_Name varchar2(25);
V_Investor_Last_Name varchar2(30);
V_Stock_Name varchar2(50);
V_Portfolio_Desc varchar2(50);

BEGIN 
SELECT TO_CHAR(sum(Quantity*Price_Per_Share)/sum(Quantity))
INTO V_Average_Cost
From Transaction 
Where Portfolio_Description = P_Portfolio_Description AND
  Investor_Number = P_Investor_Number AND
      Stock_Name = P_Stock_Name AND Buy_Sell = 'B'; 

SELECT First_Name, Last_Name
INTO V_Investor_First_Name, V_Investor_Last_Name
From Investor 
Where Portfolio_Description = P_Portfolio_Description 
  AND Portfolio.Investor_Number = Investor.Investor_Number;  

SELECT Stock_Name 
INTO V_Stock_Name
From Stock
Where Stock_Name = P_Stock_Name; 

SELECT Portfolio_Description 
INTO V_Portfolio_Desc
From Portfolio
Where Portfolio_Description = P_Portfolio_Description;

RETURN V_Output = V_Investor_First_Name || V_Investor_Last_Name || 'paid, on average' || 
                    V_Average_Cost || 'for the stock' || V_Stock_Name || 'in the portfolio' || V_Portfolio_Desc;

1 个答案:

答案 0 :(得分:1)

假设Investor_Number是主键,仅使用P_Investor_Number应该可以工作。

SELECT First_Name, Last_Name
INTO V_Investor_First_Name, V_Investor_Last_Name
From Investor 
Where Investor.Investor_Number = P_Investor_Number;