提供Oracle SQL上的视图

时间:2012-11-23 11:24:35

标签: sql oracle

A)每个仪表读数器在当月读取的仪表数量(此视图应该可以随时使用,而无需更改以适应日期)。

阅读表

    Name            Null     Type         
--------------- -------- ------------ 
READINGID       NOT NULL VARCHAR2(4)  
READINGDATA              NUMBER(10)   
READINGDATE              DATE         
METERSERIALNO            VARCHAR2(10) 
EMPLOYEEID               VARCHAR2(10) 
LASTREADINGDATA          NUMBER(10)   

员工表

    Name                 Null     Type         
-------------------- -------- ------------ 
EMPLOYEEID           NOT NULL VARCHAR2(10) 
FIRSTNAME                     VARCHAR2(20) 
LASTNAME                      VARCHAR2(20) 
NUMBEROF_METERS_READ          NUMBER(10) 

我的回答

create or replace view ViewA as
  select E.EmployeeID as Employee, E.FirstName, E.Numberof_Meters_Read, R.ReadingDate
   from   Employee E
   join
   Readings   R
   on (R.EmployeeID = E.EmlpoyeeID);

Oracle正在显示“无效标识符”。需要帮助!!

2 个答案:

答案 0 :(得分:1)

尝试将最后一行更改为:

 on (R.EmployeeID = E.EmployeeID)

(即。E.EmployeeID,而非E.EmlpoyeeID

答案 1 :(得分:0)

除了错字之外,你的意思是员工有NUMBEROF_METERS_READ吗?因为这可能只是一个总计,而不是每月总计(除非你有其他一些过程每个月重置它。

如果你想通过查看读数来做到这一点..然后创建你的视图:

select e.employeeid as employee, e.firstname, count(r.readingdate) numberof_meters_read
   from employee e left outer join readings r
                                on (r.employeeid = e.employeeid)
                               and r.readingdate >= trunc(sysdate, 'mm') 
                               and r.readingdate < add_months(trunc(sysdate, 'mm'), 1)
 group by e.employeeid, e.firstname;

或(如果您希望未完成任何员工的员工显示为“0”):

 select e.employeeid as employee, e.firstname, count(*) numberof_meters_read
   from employee e join readings r
                     on (r.employeeid = e.employeeid)
 where r.readingdate >= trunc(sysdate, 'mm')
   and r.readingdate < add_months(trunc(sysdate, 'mm'), 1)
 group by e.employeeid, e.firstname;