SQL查询 - 单个列

时间:2016-10-07 08:56:14

标签: sql database sql-server-2008 row

我想创建一个SQL Query,它将保存为存储过程,然后由XML脚本调用。它必须是某种格式,并且只有一个值列。

我需要在单个列中以不同的行显示三个不同总和的结果。我还没有弄清楚这是如何实现的,目前价值正在输出到三个不同的价值观。列。

查询的目的是在具有特定名称的SQL Server上显示数据库的大小。

该查询当前正在查询SQL Server,并且目标数据库包含" @ name"以他们的名义。它显示这些数据库的总大小,包括其日志文件,包括脱机数据库。我让它显示了总大小,但我还想在同一列中分别显示不同行中的数据库文件和日志文件的总大小。

这是我的问题:

declare
    @name NVARCHAR(20)
set
    @name = 'H3'

select
    @name as Channel,
    sum((a.size*8)/1024) Value,
    SUM(CASE WHEN type_desc like 'ROWS' THEN (a.size*8)/1024 ELSE 0 END) Value,
    SUM(CASE WHEN type_desc like 'LOG' THEN (a.size*8)/1024 ELSE 0 END) Value,
    '1' as IsInt,
    'MB' as Unit
from 
    sys.databases b
    left join sys.master_files a
        on b.database_id = a.database_id

where 
    b.name like '%'+@name+'%'

目前的输出如下:

Channel   Value    Value   Value  IsInt  Unit
--------- -------  ------- ------ ------ ------
H3        140091   134737  5354   1      MB

我希望它显示:

Channel   Value   IsInt  Unit
--------- ------- ------ ------
H3        140091  1      MB
H3        134737  1      MB
H3        5354    1      MB

我很感激任何帮助,因为我真的很困惑。 我对SQL很新,所以答案可能很明显。

这是在SQL Server 2008中。

4 个答案:

答案 0 :(得分:1)

使用CTE制作结果并使用UNION ALL合并。

declare @name NVARCHAR(20)
set @name = 'H3'

;with cte_1
as
(select
    @name as Channel,
    sum((a.size*8)/1024) Value1,
    SUM(CASE WHEN type_desc like 'ROWS' THEN (a.size*8)/1024 ELSE 0 END) Value2,
    SUM(CASE WHEN type_desc like 'LOG' THEN (a.size*8)/1024 ELSE 0 END) Value3,
    '1' as IsInt,
    'MB' as Unit
from 
    sys.databases b
    left join sys.master_files a
        on b.database_id = a.database_id
where 
    b.name like '%'+@name+'%')
SELECT Channel,Value1 Value,IsInt,Unit
FROM cte_1
UNION ALL
SELECT Channel,Value2,IsInt,Unit
FROM cte_1
UNION ALL
SELECT Channel,Value3,IsInt,Unit
FROM cte_1

答案 1 :(得分:1)

您需要的是取消结果。有不同的方法可以做到这一点。您可以使用最常用的方式:UNION ALL方法或UNPIVOT方法。但是,还有另一种方法通常比这两种方法更快,CROSS APPLY方法:

DECLARE @name nvarchar(20)
SET @name = 'H3';

WITH Cte(Channel, TotalValue, RowsValue, LogsValue, IsInt, Unit) AS(
    SELECT
        @name AS Channel,
        SUM((a.size * 8) / 1024) Value,
        SUM(CASE WHEN type_desc LIKE 'ROWS' THEN (a.size * 8) / 1024 ELSE 0 END) AS Value,
        SUM(CASE WHEN type_desc LIKE 'LOG' THEN (a.size * 8) / 1024 ELSE 0 END) AS Value,
        '1' AS IsInt,
        'MB' AS Unit
    FROM sys.databases b
    LEFT JOIN sys.master_files a
        ON b.database_id = a.database_id

    WHERE b.name LIKE '%' + @name + '%'
)
SELECT
    c.Channel,
    t.value,
    c.IsInt,
    c.Unit
FROM Cte c
CROSS APPLY (VALUES
    (c.TotalValue), (c.RowsValue), (c.LogsValue)
) t(value)

这是Dwain Camp的文章,比较了不同的方式:

您可以在VALUES中轻松添加其他列,以便更具描述性Channel

DECLARE @name nvarchar(20)
SET @name = 'H3';

WITH Cte(Channel, TotalValue, RowsValue, LogsValue, IsInt, Unit) AS(
    SELECT
        @name AS Channel,
        SUM((a.size * 8) / 1024) Value,
        SUM(CASE WHEN type_desc LIKE 'ROWS' THEN (a.size * 8) / 1024 ELSE 0 END) AS Value,
        SUM(CASE WHEN type_desc LIKE 'LOG' THEN (a.size * 8) / 1024 ELSE 0 END) AS Value,
        '1' AS IsInt,
        'MB' AS Unit
    FROM sys.databases b
    LEFT JOIN sys.master_files a
        ON b.database_id = a.database_id

    WHERE b.name LIKE '%' + @name + '%'
)
SELECT
    c.Channel + ' ' + name AS Channel,
    t.value,
    c.IsInt,
    c.Unit
FROM Cte c
CROSS APPLY (VALUES
    ('Total', c.TotalValue), 
    ('Rows', c.RowsValue), 
    ('Logs', c.LogsValue)
) t(name, value)

答案 2 :(得分:0)

也许使用UNION ALL并将查询拆分为3个不同的查询将解决您的问题?

declare @name NVARCHAR(20)
set   @name = 'H3'

select
@name as Channel,
sum((a.size*8)/1024) Value,
'1' as IsInt,
'MB' as Unit
from 
sys.databases b
left join sys.master_files a on b.database_id = a.database_id

where 
b.name like '%'+@name+'%'

UNION ALL

select
@name as Channel,
SUM(CASE WHEN type_desc like 'ROWS' THEN (a.size*8)/1024 ELSE 0 END) Value,
'1' as IsInt,
'MB' as Unit
from 
sys.databases b
left join sys.master_files a on b.database_id = a.database_id

where 
b.name like '%'+@name+'%'

UNION ALL

select
@name as Channel,
SUM(CASE WHEN type_desc like 'LOG' THEN (a.size*8)/1024 ELSE 0 END) Value,
'1' as IsInt,
'MB' as Unit
from 
sys.databases b
left join sys.master_files a on b.database_id = a.database_id

where 
b.name like '%'+@name+'%'

答案 3 :(得分:0)

使用Pivot / Unpivot:

            declare
                @name NVARCHAR(20)
            set
                @name = 'H3'

                select Channel , Value ,IsInt ,Unit from 
                            (
            select
                @name as Channel,
                sum((a.size*8)/1024) Value0,
                SUM(CASE WHEN type_desc like 'ROWS' THEN (a.size*8)/1024 ELSE 0 END) Value1,
                SUM(CASE WHEN type_desc like 'LOG' THEN (a.size*8)/1024 ELSE 0 END) Value2,
                '1' as IsInt,
                'MB' as Unit             
                from 
                sys.databases b
                left join sys.master_files a
                on b.database_id = a.database_id
                where b.name like '%'+@name+'%'

                            ) as xx
                               unpivot
                               (
                Value for col in (Value0,Value1,value2)
            )aaa

如果您有任何想法,请告诉我们