SQL Plus SYSDATE 用法?

发布于 2025-01-05 01:15:45 字数 1346 浏览 0 评论 0原文

我之前问过一个关于相同模式的问题,但是我写出了如何解决这个问题,我记得听到过一些关于 SYSDATE 的内容,但我有点不确定它是什么以及如何正确使用它。这是架构、问题和我的尝试。

问题:名为 ProblematicMachine 的视图列出了有关机器的所有信息, 上个月打开的门票超过 5 张(即,如果 我们在 10 月份运行视图,然后它列出了超过 5 张门票的门票 9 月份提交,如果我们在 11 月份运行,它会列出超过 5 个的票证 十月份提交的票证,依此类推)

我的解决方案没有 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.

i have asked a previous question about the same schema however im writing out how to solve this problem and i remember hearing something about SYSDATE but i am kinda unsure about what it is and how to use it properly... heres the schema, the problem and my attempt.

Problem: A view named ProblematicMachine that lists all information about the machines,
for which more than 5 tickets where opened during the previous month (i.e., if
we run the view in Oct, then it lists tickets for which more than 5 tickets where
submitted in Sep, and if we run it in Nov, it list tickets for which more than 5
tickets where submitted in Oct, and so on)

My solution without the 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.

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(1

疾风者 2025-01-12 01:15:45

如果您询问如何查明上个月打开了哪些票证,那么

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' )

如果您问其他问题,则必须更具体”。

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

If you are asking how to find out what tickets were opened last month, you'd want something like

SELECT *
  FROM tickets
 WHERE trunc( date_opened, 'MM' ) = trunc( add_months(sysdate,-1), 'MM' );

A function-based index on trunc( date_openend, 'MM' ) would be helpful from a performance standpoint for this sort of query (assuming you are eventually going to have many months worth of data). If you want to utilize an index on just date_opened, you'd need something more like

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

If you are asking something else, you'll have to be a bit more specific.

In your proposed view, you're not specifying any join conditions. You'll need some join conditions in order to join two tables together.

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文