database.sarang.net
UserID
Passwd
Database
DBMS
MySQL
PostgreSQL
Firebird
Oracle
Informix
Sybase
ㆍMS-SQL
DB2
Cache
CUBRID
LDAP
ALTIBASE
Tibero
DB 문서들
스터디
Community
공지사항
자유게시판
구인|구직
DSN 갤러리
도움주신분들
Admin
운영게시판
최근게시물
MS-SQL Q&A 5857 게시물 읽기
No. 5857
[질문]SQL구문 바꾸기?
작성자
완전초보
작성일
2011-03-17 07:45ⓒ
2011-03-31 01:06ⓜ
조회수
7,203

안녕하세요. 저는 SQL 에 대해서는 아무것도 모르는 완전 초짜입니다.

네크워크 쪽에 일하고 있고 Solarwins라는 프로그램을 사용해서 네트워크 모니터를 합니다.

다음의 SQL 구문은 달(Month)에 쓰여지는 네트워크 사용량을 알아내는 다섯개중의 SQL Function중에 

시간을 정하는 두개의 Function 구문입니다. 이 구문들은 달(Month) 로는 잘 돌아가고 있지만,

어떻게 하면 이 구문들을 주로(Week)로 바꿀수 있는지요???

 

/* Month Begin */

CREATE FUNCTION dbo.monthBegin( @monthPast integer ) RETURNS DateTime
AS
BEGIN
    declare @ret datetime
    set @ret = GetDate();
    set @ret = DateAdd( hour,
        -(datepart( hour, @ret ) ),
         @ret )
    set @ret = DateAdd( minute,
        -(datepart( minute, @ret ) ),
         @ret )
    set @ret = DateAdd( second,
        -(datepart( second, @ret ) ),
         @ret )
    set @ret = DateAdd( millisecond,
        -(datepart( millisecond, @ret ) ),
         @ret )
    set @ret = DateAdd( day,
        -(datepart( day, @ret ) -1 ),
         @ret )
    set @ret = DateAdd( month,
        -@monthPast,
         @ret )

    return @ret
END

 

/* Month End */

CREATE FUNCTION dbo.monthEnd( @monthPast integer ) RETURNS DateTime
AS
BEGIN
    declare @ret datetime
    set @ret = dbo.monthBegin( @monthPast );
    set @ret = DateAdd( month, 1, @ret );
    set @ret = DateAdd( day, -1, @ret );
    set @ret = DateAdd( hour, 23, @ret );
    set @ret = DateAdd( minute, 59, @ret );
    set @ret = DateAdd( second, 59, @ret );
    set @ret = DateAdd( millisecond, 998, @ret );
    return @ret
END

 

/*Business Hour*/

CREATE FUNCTION dbo.isBusinessHours(@time DATETIME, @offset INTEGER)
RETURNS INTEGER
AS
BEGIN
    DECLARE @theDay INTEGER
    DECLARE @theHour INTEGER
    DECLARE @adjDate DATETIME
   
    -- Adjust the date with the GMT offset.
    SET @adjDate = DATEADD(hour, @offset, @time)

    -- Get the day and make sure it's not Saturday or Sunday.
    SET @theDay = DATEPART(day, @adjDate)
   
    IF (@theDay = '1')
        RETURN 0
    IF (@theDay = '7')
        RETURN 0
   
    -- Get the hour and make sure it's between 8AM and 6PM.
    SET @theHour = DATEPART(hour, @adjDate)
   
    IF (@theHour < 8)
        RETURN 0

    IF (@theHour > 17)
        RETURN 0

    RETURN 1

END

 

/*

 

/*Inbound 95th*/

CREATE FUNCTION dbo.in95th (@node_name VARCHAR(30), @cir FLOAT, @start_time DATETIME, @end_time DATETIME)
RETURNS FLOAT
AS
BEGIN

    DECLARE @value FLOAT
    SELECT @value = MAX(In_Averagebps)
    FROM (
        SELECT TOP 95 PERCENT In_Averagebps
        FROM
        Nodes INNER JOIN Interfaces ON Nodes.NodeID = Interfaces.NodeID
        INNER JOIN InterfaceTraffic ON Interfaces.InterfaceID = InterfaceTraffic.InterfaceID
        WHERE Nodes.Caption = @node_name
        AND DateTime BETWEEN @start_time AND @end_time
        AND dbo.isBusinessHours(DateTime, Interfaces.gmtOffset) = '1'
        AND Interfaces.isOnUtilRept = '1'
        AND Interfaces.CIR = @cir
        ORDER BY InterfaceTraffic.In_Averagebps ASC
    ) AS AA
    RETURN @value

END

 

/*Outbound 95th*/

CREATE FUNCTION dbo.out95th (@node_name VARCHAR(30), @cir FLOAT, @start_time DATETIME, @end_time DATETIME)
RETURNS FLOAT
AS
BEGIN

    DECLARE @value FLOAT
    SELECT @value = MAX(Out_Averagebps)
    FROM (
        SELECT TOP 95 PERCENT Out_Averagebps
        FROM
        Nodes INNER JOIN Interfaces ON Nodes.NodeID = Interfaces.NodeID
        INNER JOIN InterfaceTraffic ON Interfaces.InterfaceID = InterfaceTraffic.InterfaceID
        WHERE Nodes.Caption = @node_name
        AND DateTime BETWEEN @start_time AND @end_time
        AND dbo.isBusinessHours(DateTime, Interfaces.gmtOffset) = '1'
        AND Interfaces.isOnUtilRept = '1'
        AND Interfaces.CIR = @cir
        ORDER BY InterfaceTraffic.Out_Averagebps ASC
    ) AS AA
    RETURN @value

RETURN @value

END

 

/*Monthly Report*/

SELECT DISTINCT

Interfaces.FunctionDesc AS FunctionDesc,

Nodes.SiteName AS SiteName,

Nodes.NodeID,

Nodes.Caption,

Interfaces.FunctionCode,

Interfaces.Burst as Burst,

Interfaces.CIR AS CIR,

dbo.in95th(Nodes.Caption, Interfaces.CIR, dbo.monthBegin(0), dbo.monthEnd(0)) / (Interfaces.CIR * 1048576) as in95th,

dbo.out95th(Nodes.Caption, Interfaces.CIR, dbo.monthBegin(0), dbo.monthEnd(0)) / (Interfaces.CIR * 1048576) as out95th,

dbo.in95th(Nodes.Caption, Interfaces.CIR, dbo.monthBegin(0), dbo.monthEnd(0))/1048576 as in95thMbps,

dbo.out95th(Nodes.Caption, Interfaces.CIR, dbo.monthBegin(0), dbo.monthEnd(0))/1048576 as out95thMbps

FROM

Nodes INNER JOIN Interfaces ON Nodes.NodeID = Interfaces.NodeID

WHERE

Interfaces.isOnUtilRept = '1'

ORDER BY

Interfaces.FunctionDesc, Nodes.SiteName

고수님들의 조언 부탁드립니다.!

이 글에 대한 댓글이 총 2건 있습니다.

@monthPast로 입력한 정수만큼의 이전달을 가져오는데 그 달의 시작과 끝을 보여주는 함수인가? 보네요

그럼 주( Week)로 할려면 어떤식으로 나와야하는지 구체적으로 설명을 해주셔야..

입력한 값 이전 주의 첫째일과 마지막일? 인가요?

 

 

착한넘(agoodman99)님이 2011-03-30 16:55에 작성한 댓글입니다.

죄송합니다. 제가 SQL에 전혀 아는 봐가 없어서 @monthPast에 관한 내용을 알길이 없어서 Monthly Report를 얻기위해 쓰이는 모든 fucntion을 본문에 다시 집어 넣었습니다. 그리고 이 구문을 Week으로 바꾼다면 1 - 2주 전의 Report를 얻고자 합니다.  감사합니다.

완전초보님이 2011-03-31 01:19에 작성한 댓글입니다. Edit
[Top]
No.
제목
작성자
작성일
조회
5862재귀쿼리 삭제 문의
레이첼
2011-03-26
6356
5860group by 질문입니다. [1]
2011-03-23
6215
5859경우의수만큼 레코드를 나열하는 방법을 알고 싶어요^^
이만실
2011-03-22
7187
5857[질문]SQL구문 바꾸기? [2]
완전초보
2011-03-17
7203
5856필드명이 아닌 데이터로만 조회 가능할까요? [1]
김태희
2011-03-15
5951
5855프로시저 파라미터 질문 [2]
이은택
2011-03-14
6738
58542개이상중복되는 Row 얻어오는방법이있을까요.. [1]
육식동물
2011-03-14
6726
Valid XHTML 1.0!
All about the DATABASE... Copyleft 1999-2024 DSN, All rights reserved.
작업시간: 0.018초, 이곳 서비스는
	PostgreSQL v16.2로 자료를 관리합니다