无法在SQL Server连接视图中对列进行求和

时间:2016-12-08 22:59:41

标签: oracle-apex

Moi伙计们,Matt在这里。我对相对复杂的观点有困难。我有一个零件和服务表,每个表都有给定零件/服务的唯一标识符。我试图将这些链接到服务发票表和后续视图作为M:N关系,因此我设置了中间关系表,包括发票号(发票主键)和部件/服务number(部件/服务主键)作为组合主键。这是我对整个关系和观点的代码:

CREATE TABLE service_invoice
(   servinv_Num VARCHAR2(10) CONSTRAINT serv_snum_PK PRIMARY KEY, 
    servinv_EmpID NUMBER(6) CONSTRAINT serv_empnum_FK REFERENCES employee(empID), 
    servinv_CustID NUMBER(6) CONSTRAINT serv_custid_FK REFERENCES customer(custID),
    servinv_VIN VARCHAR2(25) CONSTRAINT serv_VIN_FK REFERENCES vehicle(vehicle_vin),
    servinv_Terms VARCHAR2(6) CONSTRAINT serv_trms_NN NOT NULL,
    servinv_Date DATE );


CREATE TABLE Parts
(   PartID VARCHAR2(10) CONSTRAINT Part_PartID_PK PRIMARY KEY,
    PartDesc VARCHAR2(50) CONSTRAINT Part_PartDesc_NN NOT NULL,
    PartCharge NUMBER(4,2) CONSTRAINT Part_PartCharge_NN NOT NULL );


CREATE TABLE Service
(   ServiceID VARCHAR2(10) CONSTRAINT Serv_ServID_PK PRIMARY KEY,
    ServDesc VARCHAR2(50) CONSTRAINT Serv_ServName_NN NOT NULL,
    ServCharge NUMBER(4,2) CONSTRAINT Serv_ServCharge_NN NOT NULL );


CREATE TABLE Serv_SI_Rel
(   SI_num VARCHAR2(10) CONSTRAINT ServSI_SInum_FK REFERENCES service_invoice(servinv_Num),
    ServiceID VARCHAR2(10) CONSTRAINT ServSI_ServID_FK REFERENCES Service(ServiceID),
    CONSTRAINT ServSI_SInum_ServID_PK PRIMARY KEY(SI_num, ServiceID) );

CREATE TABLE Parts_SI_Rel
(   SI_num VARCHAR2(10) CONSTRAINT PartSI_SInum_FK REFERENCES service_invoice(servinv_Num),
    PartID VARCHAR2(10) CONSTRAINT  PartSI_PartID_FK REFERENCES Parts(PartID),
    CONSTRAINT PartSI_SInum_PartID_PK PRIMARY KEY(SI_num, PartID) );


CREATE OR REPLACE VIEW ServiceInvoiceDoc
AS
( 
 SELECT si.servinv_Num, si.servinv_Date, si.servinv_Terms,
    es.empName,
    sc.custName, sc.custHouse, sc.custCity, 
        sc.custState, sc.custZIP, sc.custPhone, sc.custEmail,
    sv.vehicle_VIN, sv.vehicle_mileage,
    srel.ServiceID,
    prel.PartID,
    s.ServDesc, s.ServCharge,
    p.PartDesc, p.PartCharge,
    SUM(s.ServCharge) TotalServCharges,
    SUM(p.PartCharge) TotalPartsCharges,
    ( SUM(s.ServCharge)+SUM(p.PartCharge) ) SubTotalCharges,
    ( SUM(s.ServCharge)+SUM(p.PartCharge) )*0.0825 Taxes,
    ( SUM(s.ServCharge)+SUM(p.PartCharge) )*1.0825 TotalCharges

   FROM service_invoice si
JOIN employee es
    ON (es.empID = si.servinv_EmpID)
JOIN customer sc
    ON (sc.custID = si.servinv_CustID)
JOIN vehicle sv
    ON (sv.vehicle_VIN = si.servinv_VIN)
LEFT OUTER JOIN Serv_SI_Rel srel
    ON (srel.SI_Num = si.servinv_Num)
LEFT OUTER JOIN Parts_SI_Rel prel
    ON (prel.SI_num = si.servinv_Num) 
JOIN Parts p
    ON (prel.PartID = p.PartID)
JOIN Service s
    ON (srel.ServiceID = s.ServiceID) );

我得到的错误与M:N关系中各个部分和服务费用的总和有关。这是运行中的错误代码:

ORA-00937: not a single-group group function

我已经尝试使用group by命令进行修复,但是分组标识符(服务发票)不包含在部件或服务表中,并且连接似乎不会将这些连接起来对于一个团体。例如我试过调用GROUP BY si.servinv_Num

可以解决这个问题还是完全错误?我可以选择将M:N关系作为1:M放弃,只是为每个部分/服务费单独制作发票,但我更愿意保持紧凑和专业。

非常感谢任何帮助。非常感谢你的时间!

1 个答案:

答案 0 :(得分:0)

a)错误的标签

b)我想你需要列出所有没有聚合的列的列,按照Oracle

...
group by si.servinv_Num, si.servinv_Date, si.servinv_Terms,
    es.empName,
    sc.custName, sc.custHouse, sc.custCity, 
        sc.custState, sc.custZIP, sc.custPhone, sc.custEmail,
    sv.vehicle_VIN, sv.vehicle_mileage,
    srel.ServiceID,
    prel.PartID,
    s.ServDesc, s.ServCharge,
    p.PartDesc, p.PartCharge