1

当我执行以下 sql 查询时:

select nodename, message from messages, where messages like '%hit%'

我得到以下输出:(字符串字段):

oracle01 file system /apl/oracleasm hit 93% usage
         file system /apl/oraclears hit 96% usage
         file system /apl/oracleadv hit 97% usage

有什么方法可以更改我的查询以获取下面的输出?

oracle01 file system /apl/oracleasm hit 93% usage
oracle01 file system /apl/oraclears hit 96% usage
oracle01 file system /apl/oracleadv hit 97% usage
4

1 回答 1

3

试试这个——

询问:

DECLARE @messages TABLE
(
      nodename VARCHAR(50)
    , [message] VARCHAR(100)
)

INSERT INTO @messages (nodename, [message])
VALUES 
('oracle01', 'file system /apl/oracleasm hit 93% usage'),
('',         'file system /apl/oraclears hit 96% usage'),
('',         'file system /apl/oracleadv hit 97% usage'),
('oracle02', 'file system /apl/oracleadv hit 99% usage'),
('',         'file system /apl/oracleadv hit 80% usage')

;WITH cte AS 
(
    SELECT 
          m.nodename
        , m.[message]
        , RowID = ROW_NUMBER() OVER (ORDER BY (SELECT 1))
    FROM @messages m
    WHERE m.[message] LIKE '%hit%' 
)
SELECT
      nodename = 
        CASE WHEN ISNULL(m.nodename, '') = '' 
            THEN (
                SELECT TOP 1 m2.nodename 
                FROM cte m2
                WHERE m2.RowID - 1 < m.RowID
                    AND ISNULL(m2.nodename, '') != ''
                ORDER BY m2.RowID DESC
            ) 
            ELSE m.nodename 
        END
    , [message]
FROM cte m

输出:

nodename    message
----------- ------------------------------------------
oracle01    file system /apl/oracleasm hit 93% usage
oracle01    file system /apl/oraclears hit 96% usage
oracle01    file system /apl/oracleadv hit 97% usage
oracle02    file system /apl/oracleadv hit 99% usage
oracle02    file system /apl/oracleadv hit 80% usage

更新:

DECLARE @messages TABLE
(
      nodename VARCHAR(50)
    , [message] VARCHAR(500)
)

INSERT INTO @messages (nodename, [message])
SELECT 'oracle01', 
'file system /apl/oracleasm hit 93% usage
file system /apl/oraclears hit 96% usage
file system /apl/oracleadv hit 97% usage'

SELECT nodename + ' ' + REPLACE([message], CHAR(10), nodename + ' ')
FROM @messages

更新输出:

oracle01 file system /apl/oracleasm hit 93% usage
oracle01 file system /apl/oraclears hit 96% usage
oracle01 file system /apl/oracleadv hit 97% usage

更新评论:

SELECT errormessage = 

    n.caption + ' ' + 
    n.ambiente + ' ' + 
    REPLACE(ccs.errormessage, CHAR(10), n.caption + ' ' + n.ambiente + ' ')

FROM dbo.APM_CurrentComponentStatus ccs WITH (NOLOCK) 
JOIN dbo.APM_Application a WITH (NOLOCK) ON ccs.ApplicationID = a.ID 
JOIN dbo.Nodes n WITH (NOLOCK) ON a.NodeID = n.NodeID 
WHERE ccs.ErrorMessage LIKE '%hit%' 
    AND n.ambiente IN ('homologação', 'desenvolvimento') 

有没有办法按可用的 MB 升序对输出进行排序?

DECLARE @table TABLE 
(
      nodename VARCHAR(50)
    , [message] VARCHAR(500)
)

INSERT INTO @table (nodename, [message])
SELECT 'oracle01', 
'FS /bd1/devsdata/logs1 hit 93% usage -> 742 MB available 
FS /bd1/devsdata/logs1 hit 98% usage -> 542 MB available 
FS /bd1/devsdata/tmp hit 99% usage -> 793 MB available'

SELECT *
FROM (
    SELECT 
          d.nodename
        , [message] = p.value('(.)[1]', 'VARCHAR(500)')
    FROM (
        SELECT 
              t.nodename
            , kxml = CAST('<r><s>' + REPLACE(t.[message], CHAR(10), '</s>' + '<s>') + '</s></r>' AS XML)  
        FROM @table t
    ) d
    CROSS APPLY kxml.nodes('/r/s') t(p)
) d
ORDER BY d.nodename, SUBSTRING([message], CHARINDEX('-> ', [message]) + 3, CHARINDEX('MB', [message]) - CHARINDEX('-> ', [message]) - 3)

输出:

nodename    message
----------- ---------------------------------------------------------
oracle01    FS /bd1/devsdata/logs1 hit 98% usage -> 542 MB available 
oracle01    FS /bd1/devsdata/logs1 hit 93% usage -> 742 MB available 
oracle01    FS /bd1/devsdata/tmp hit 99% usage -> 793 MB available
于 2013-05-14T05:57:05.970 回答