用于填充表

时间:2018-04-16 14:54:13

标签: oracle plsql sqlplus

我的任务是使用PL / SQL块填充一个名为 Sales_Facts 的表,但我返回0结果。该过程执行时没有错误,我运行我的脚本来填充我的表,但我的SELECT COUNT脚本没有返回任何内容。我看不出我做错了什么。

这就是我所拥有的:

CREATE TABLE Sales (
    sale_ID VARCHAR2(10) NOT NULL,
    salesperson_ID VARCHAR2(10) NOT NULL,
    cust_ID VARCHAR2(10) NOT NULL,
    sale_date DATE,
    VIN VARCHAR2(20) NOT NULL,
    mileage INT,
    vehicle_status VARCHAR2(15),
    gross_sale_price NUMBER(8,2) NOT NULL,
    PRIMARY KEY (sale_ID),
    CONSTRAINT FK_Customer_ID FOREIGN KEY (cust_ID) REFERENCES Customers(cust_ID),
    CONSTRAINT FK_VIN_ID FOREIGN KEY (VIN) REFERENCES Sale_Vehicles(VIN));


CREATE TABLE Times (
    sale_day DATE NOT NULL, --populated from Sales sale_date
    day_type VARCHAR2(50) NOT NULL, 
    PRIMARY KEY (sale_day));

CREATE TABLE Vehicles (
    vehicle_Code VARCHAR2(10),
    description VARCHAR2(100),
    PRIMARY KEY (vehicle_Code));

车辆填充了这个:

CREATE SEQUENCE veh_code_seq
    MINVALUE 1
    START WITH 1
    INCREMENT BY 1
    CACHE 20;
COMMIT;

--PL/SQL Block

SET SERVEROUTPUT ON
DECLARE
vehType VARCHAR2(50);
v_make OLTP_Vehicles.make%type;
v_model OLTP_Vehicles.model%type;

CURSOR v_type IS SELECT DISTINCT make, model FROM OLTP_Vehicles;
BEGIN
    OPEN v_type;
    LOOP
        FETCH v_type INTO v_make, v_model;
        vehType := v_make || ', ' || v_model;
        INSERT INTO Vehicles (vehicle_Code, description)
        VALUES (veh_code_seq.NEXTVAL, vehType);
        EXIT WHEN v_type%notfound;
    END LOOP;
    CLOSE v_type;
END;
/

CREATE TABLE Financing_Plans (
    plan_ID VARCHAR2(10) NOT NULL,
    institution VARCHAR2(25) NOT NULL,
    loan_type VARCHAR2(15) NOT NULL,
    percentage DECIMAL(4,2) NOT NULL,
    min_down NUMBER(8,2) NOT NULL,
    max_loan_amt NUMBER(8,2) NOT NULL,
    max_term INT NOT NULL,
    PRIMARY KEY (plan_ID));

CREATE TABLE Dealerships (
    dealer_ID VARCHAR2(5) NOT NULL,
    location VARCHAR(30) NULL,
    region_ID VARCHAR(5) NULL,
    street_address VARCHAR2(100) NOT NULL,
    city VARCHAR2(25) NOT NULL,
    state VARCHAR2(15) NOT NULL,
    zip VARCHAR2(5) NOT NULL,
    phone VARCHAR2(10) NOT NULL,
    sqft NUMERIC(8,2) NULL,
    opened_date DATE,
    manager VARCHAR2(100) NULL,
    district_ID VARCHAR2(5) NOT NULL,
    PRIMARY KEY (dealer_ID),
    CONSTRAINT UC_Dealership UNIQUE (dealer_ID,district_ID));

CREATE TABLE Sales_Facts (
    sale_day DATE NOT NULL,
    vehicle_Code VARCHAR2(10) NOT NULL, 
    plan_ID VARCHAR2(10) NOT NULL,
    dealer_ID VARCHAR2(5) NOT NULL,
    vehicles_sold NUMBER(8,2) NOT NULL,
    gross_sales_amt NUMBER(8,2) NOT NULL,
    CONSTRAINT PK_Sales_Facts PRIMARY KEY (sale_day, vehicle_Code, plan_ID, dealer_ID),
    CONSTRAINT FK_Sale_Day FOREIGN KEY(sale_day) References Times(sale_day),
    CONSTRAINT FK_Vehicle_Code FOREIGN KEY(vehicle_Code) References Vehicles(vehicle_Code),
    CONSTRAINT FK_Fin_Plan_ID FOREIGN KEY(plan_ID) References Financing_Plans(plan_ID),
    CONSTRAINT FK_Dealer_ID FOREIGN KEY(dealer_ID) References Dealerships(dealer_ID));

这是我的程序没有返回任何结果:

CREATE OR REPLACE PROCEDURE Populate_Sales_Facts
AS
    l_sale_day DATE;
    l_vehicle_Code VARCHAR2(10);
    l_plan_ID VARCHAR2(10);
    l_dealer_ID VARCHAR2(5);
    l_vehicles_sold NUMBER(8,2);
    l_gross_sales_amt NUMBER(8,2); 

    CURSOR c1 IS SELECT sale_day,vehicle_Code,fp.plan_ID,d.dealer_ID,
        COUNT (*) AS vehicles_sold,
        SUM (s.gross_sale_price) AS gross_sales_amt
    FROM Times t, Sales s, Financing_Plans fp, Dealerships d, Vehicles v
    WHERE t.sale_day = s.sale_date
    GROUP BY sale_day, vehicle_Code, fp.plan_ID, d.dealer_ID;

BEGIN
    OPEN c1;
    LOOP
      FETCH c1 INTO l_sale_day, l_vehicle_Code, l_plan_ID, l_dealer_ID, l_vehicles_sold, l_gross_sales_amt;
      EXIT WHEN c1%NOTFOUND;
      IF l_vehicles_sold <> 0 THEN
        INSERT INTO SALES_FACTS (sale_day,vehicle_Code,plan_ID,dealer_ID,vehicles_sold, gross_sales_amt)
        VALUES (l_sale_day,l_vehicle_Code,l_plan_ID,l_dealer_ID,l_vehicles_sold,l_gross_sales_amt);
    END IF;
    END LOOP;
CLOSE c1; 
END;
/

BEGIN
    Populate_Sales_Facts;
END;
/

我获得了填充Sales表的字段,但无法更改  根据我的要求,但我确实修复了我的WHERE语句,从Times表中拉出sale_day,其等于Sales表中的sale_date,因为这些是唯一链接的字段。所以我能够填充表格,但现在不是不超过200行,而是获得61065行数据。以下是我的要求:获取维度表的主键的每种可能组合,然后获得每种组合的总销售量和总销售额。如果组合的Total_Vehicles_Sold和Gross_Sales_Amount的这些值为零,则不要将行插入SALES_FACT表。仅插入四个外键列组合的行 有些车辆已售出。也许我只是误解了这项任务,但我觉得现在我的行​​数太多了。

0 个答案:

没有答案