SQL Plus SYSDATE用法?

时间:2012-02-13 23:58:09

标签: sql database oracle database-design sqlplus

我已经问过一个关于相同架构的先前问题,但我正在写出如何解决这个问题,我记得听过SYSDATE的一些事情,但我不确定它是什么以及如何正确使用它。继承了架构,问题和我的尝试。

问题:名为ProblematicMachine的视图列出了有关计算机的所有信息, 其中超过5张门票在上个月开放(即,如果 我们在10月份运行视图,然后列出了超过5张门票的门票 在9月提交,如果我们在11月份运行它,它列出的票数超过5 10月份提交的门票,等等)

我的解决方案没有SYSDATE

CREATE OR REPLACE VIEW ProblematicMachine AS 
SELECT machine_name, IP, network_port, MACADDR, location_id 
FROM Inventory, Tickets
WHERE    *this is where i was gonna use the condition of the 
 SYSDATE i guess SYSDATE - 30 (for days). 

SCHEMAS

TECH PERSONNEL (pplSoft, fname, lname, pittID, expertise, office phone) 
where fname is first name, and lname is last name.

USERS (pplSoft, fname, lname, pittID, office phone)

CATEGORIES (category id, category, description) where this table lists 
all possible categories of submitted tickets.

INVENTORY(machine name, IP, network port, MACADDR, location id)

LOCATIONS(location id, location, building, notes)

TICKETS (ticket number, owner pplSoft, date submitted, date closed, 
days worked on, category id, machine name, location, description)

ASSIGNMENT (ticket number, tech pplSoft, date assigned, status) 
where status held is an enumeration, could be: assigned, 
in progress, delegated, closed successful, or closed unsuccessful.

1 个答案:

答案 0 :(得分:3)

如果你问如何找出上个月开出的门票,你需要像

这样的东西
SELECT *
  FROM tickets
 WHERE trunc( date_opened, 'MM' ) = trunc( add_months(sysdate,-1), 'MM' );

trunc( date_openend, 'MM' )上的基于函数的索引从这种查询的性能角度来看会很有帮助(假设您最终会有数月的数据)。如果您想仅使用date_opened上的索引,则需要更像

的内容
SELECT *
  FROM tickets
 WHERE date_opened >= trunc( add_months(sysdate, -1), 'MM' )
   AND date_opened <  trunc( sysdate, 'MM' )

如果你问别的话,你必须更具体一点。

在您提议的视图中,您没有指定任何连接条件。您需要一些连接条件才能将两个表连接在一起。