oracle sql how to write?

wyjam 2006-06-11 11:26:41
要求:
三张表,DATA,CRO,DIR.
create table DATA (
OID NUMBER not null,
RED NUMBER default 0,
CROID NUMBER default 0,
DIRID NUMBER default 0,
YDATE DATE default SYSDATE, //这个为记录对应的时间
constraint PK_DATA primary key (OID)
)
/
CREATE TABLE CRO(
OID NUMBER NOT NULL,
NAME VARCHAR(20) ,
NO NUMBER DEFAULT 0,
IP VARCHAR(20),
constraint PK_CRO primary key (OID)
)
CREATE TABLE DIR(
OID NUMBER NOT NULL,
NAME VARCHAR(20) ,
NO NUMBER DEFAULT 0,
IP VARCHAR(20),
constraint PK_DIR primary key (OID)
)

SELECT COUNT(*) AS SUM,B.NO,
B.NAME||' '||C.DIR AS NAME,
B.IP FROM DATA A,CRO B,DIR C WHERE A.CROID = B.OID AND A.DIRID = C.OID GROUP BY
B.NO,B.NAME||' '||C.DIR,B.IP
上面的这个语句可以搞出对应的分组的记录。
现在想加入一个一天中24小时对应的一个小时的记录条数。

结果如下:
NO NAME IP 2006-06-10 01:00:00(时间段的SUM)=1HOUR 2006-06-10 02:00:00(时间段的SUM)=2HOUR...
1 家 192.168.0.1 3 12 ...


...全文
232 5 打赏 收藏 转发到动态 举报
AI 作业
写回复
用AI写文章
5 条回复
切换为时间正序
请发表友善的回复…
发表回复
yqwd911 2006-06-13
  • 打赏
  • 举报
回复

可以用case語句,
你的例子太多表了,關係複雜,我擧個簡單的例子你體會一下方法。

table a
id time
1 2006/06/12 12:30
1 2006/06/12 12:50
2 2006/06/12 12:30
2 2006/06/12 12:50

select
id,
sum(case when (to_char(time,'hh24:mi:ss')>='00:00:00' and to_char(time,'hh24:mi:ss')<'01:00:00') then 1 else 0 end) H1,
sum(case when (to_char(time,'hh24:mi:ss')>='01:00:00' and to_char(time,'hh24:mi:ss')<'02:00:00') then 1 else 0 end) H2,
...
sum(case when (to_char(time,'hh24:mi:ss')>='23:00:00') then 1 else 0 end) H24
from a
where time >= to_date('2006/06/12','yyyy/mm/dd') and time <to_date('2006/06/12','yyyy/mm/dd')+1
group by id
sql語句中每個sum(case when ... then ... else ...end)的寫法是求每個時間段的訪問次數。而where 條件中的時間可以根據你的需求變更,如果是每天固定執行一次可以更換成sysdate
yqwd911 2006-06-12
  • 打赏
  • 举报
回复
數據庫版本是多少?是9i不?
wyjam 2006-06-12
  • 打赏
  • 举报
回复
今天一天在外面, 现在才有空上网,
对数据库是9i没错。
yqwd911 2006-06-12
  • 打赏
  • 举报
回复
有循環語句:
1)無限循環滿足條件退出
loop
sql statement;
exit when condition statement;
end loop;
2)黨滿足條件則進行循環
while condittion statement;
sql statement;
end loop;
3)For循環(可以用游標)
for i in 1..12 loop
sql statement;
end loop;

建議常用3)寫法比較簡單,應用比較靈活。

但是作爲你的統計,要實現及時,盡量做一個觸發器這樣比較好。
個人建議供參考。
wyjam 2006-06-11
  • 打赏
  • 举报
回复
自己UP一下, 顺便问一下,SQL中的存储过程 有没有循环语句?
Pro Oracle SQL unlocks the power of SQL in the Oracle Database—one of the most potent SQL implementations on the market today. To master it requires a three-pronged approach: learn the language features, learn the supporting features that Oracle provides to help use the language effectively, and learn to think and work in sets., , Karen Morton and her team help you master powerful aspects of Oracle SQL not found in competing databases. You’ll learn analytic functions, the MODEL clause, and advanced grouping syntax—techniques that will help in creating good queries for reporting and business intelligence applications. Pro Oracle SQL also helps you minimize parsing overhead, read execution plans, test for correct results, and exert control over SQL execution in your database. You’ll learn when to create indexes, how to verify that they make a difference, how to use SQL Profiles to optimize SQL in packaged applications, and much more. You’ll also understand how SQL is optimized for working in sets, and that the key to getting accurate results lies in making sure that queries ask clear and precise questions., , What’s the bottom-line? Pro Oracle SQL helps you work at a truly professional level in Oracle dialect of SQL. You’ll master the language, the tools to work effectively with the language, and the right way to think about a problem in SQL. Pro Oracle SQL helps you rise above the crowd to provide stellar service in your chosen profession. Endorsed by the OakTable Network, a group of Oracle technologists well-known for their rigorous and scientific approach to Oracle Database performance Comprehensive – goes beyond the language with a focus on what you need to know to write successful queries and data manipulation statements. What you'll learn Master powerful SQL features implemented only in Oracle Database Read and interpret SQL execution plans Quickly diagnose and fix badly performing SQL Control execution plans through hints, profiles, and plan baselines Optimize queries within packaged applications without touching the code Recognize when not to waste time on SQL that is performing optimally Who this book is for Pro Oracle SQL is aimed at developers and database administrators who submit SQL for execution by an Oracle database. Readers should already know the basic four SQL statements, and be ready to learn deeply about Oracle’s specific implementation of the language, including Oracle-specific features and syntax. Readers should also want to learn about Oracle Database features such as analytic queries, the MODEL clause, and subquery refactoring that are designed to help developers and DBAs exert control over their SQL environment and its execution.

3,494

社区成员

发帖
与我相关
我的任务
社区描述
Oracle 高级技术相关讨论专区
社区管理员
  • 高级技术社区
加入社区
  • 近7日
  • 近30日
  • 至今
社区公告
暂无公告

试试用AI创作助手写篇文章吧