将事实表转换为sql oracle中的模式

时间:2015-01-08 10:13:24

标签: sql oracle

我试图创建一个收集一组其他模式的模式,但也包含一个Sum。这就是我到目前为止所做的:

创建DDL -

CREATE TABLE ticket_sales 
  ( 
     theatreid     NUMBER(4) CONSTRAINT fk1 REFERENCES theatre, 
     trowid        NUMBER(4) CONSTRAINT fk2 REFERENCES theatre_row, 
     seasonid      NUMBER(4) CONSTRAINT fk3 REFERENCES season, 
     productionid  NUMBER(4) CONSTRAINT fk4 REFERENCES production, 
     clientid      NUMBER(4) CONSTRAINT fk5 REFERENCES client, 
     ticketrevenue NUMBER(7) NOT NULL, 
     CONSTRAINT pk PRIMARY KEY (theatreid, trowid, seasonid, productionid, 
     clientid) 
  ); 

插入DML -

INSERT INTO ticket_sales 
SELECT theatreid, 
       trowid, 
       seasonid, 
       productionid, 
       clientid, 
       ticketrevenue 
FROM   ( 
                SELECT   theatre.theatreid, 
                         theatre_row.trowid, 
                         season.seasonid, 
                         production.productionid, 
                         client.clientid, 
                         SUM(totalamount) ticketrevenue 
                FROM     notmydatabase.ticketpurchase 
                WHERE    extract(month FROM notmydatabase.preformance.pdate)=season.month 
                AND      to_number(to_char(notmydatabase.performance.pdate, 'WW'))=season.week
                AND      extract(day FROM notmydatabase.performance.pdate)=season.day 
                GROUP BY theatre.theatreid, 
                         theatre_row.trowid, 
                         season.seasonid, 
                         production.productionid, 
                         client.clientid);

当我运行插入部分时,它只是提出了表或视图不存在。我不知道为什么。有人可以帮忙吗?

提前致谢

2 个答案:

答案 0 :(得分:1)

以下可能是错误原因 -

  1. FROM not mydatabase.ticketpuchase
  2. 语法不正确。我宁愿期待ORA-00903: invalid table name错误。

    更多错误

    1. ticketpuchase应为ticketpurchase

    2. preformance.pdate应为performance.pdate

    3. FROM子句
    4. 中缺少以下表格

      -

      theatre 
      theatre_row
      season
      production
      client
      

答案 1 :(得分:0)

当前的问题是,您在r子句中唯一的表格名称中遗漏了fromnotmydatabase.ticketpuchase应为notmydatabase.ticketpurchase。确定将摆脱ORA-00942。

但那将揭示声明其余部分的问题。您指的是其他各种表,例如client.clientid,当这些表不是查询的一部分时 - client表中未指定from表。所有。您需要包含您所引用的所有表格以及加入条件。您似乎也在一个地方拼错了performance preformance

我根据你已经定义的键来猜测表结构和连接条件,但是这样的事情可能会给你一般的想法和一些工作:

INSERT INTO ticket_sales 
SELECT   theatre.theatreid, 
         theatre_row.trowid, 
         season.seasonid, 
         production.productionid, 
         client.clientid, 
         SUM(totalamount) ticketrevenue 
FROM     notmydatabase.ticketpurchase
JOIN     notmydatabase.performance
ON       notmydatabase.performance.performanceid = notmydatabase.ticketpurchase.performanceid
JOIN     theatre_row
ON       theatre_row.trowid = notmydatabase.ticketpurchase.trowid
JOIN     theatre
ON       theatre.theatreid = theatre_row.trowid
JOIN     production
ON       production.productionid = notmydatabase.performance.productionid
JOIN     client
ON       client.clientid = ticketpurchase.clientid
JOIN     season
ON       season.month = extract(month FROM notmydatabase.performance.pdate) 
AND      season.week = to_number(to_char(notmydatabase.performance.pdate, 'WW'))
AND      season.day = extract(day FROM notmydatabase.performance.pdate) 
GROUP BY theatre.theatreid, 
         theatre_row.trowid, 
         season.seasonid, 
         production.productionid, 
         client.clientid;

您也不需要额外级别的子查询。您可能希望查看表别名,这将使联接和其他引用更短,更容易遵循。您可能需要考虑使用视图来计算总和,因此您始终可以看到当前值而不是快照。我意识到这是一个练习,但是带插入的表可能是这里的要求。

但是,大多数情况下,您需要使用能够向您显示错误发生位置的客户端,以便您可以开始解决问题,尤其是拼写错误和基本语法错误。