SQL Server:从另一个查询查询结果?

时间:2014-04-24 18:57:59

标签: sql sql-server

我在下面有一个查询,返回124行,我试图计算Name并返回值124.是否有一种简单的方法可以查询此查询的结果或只是添加另一列?

declare @Logins varchar(max)

Select 
    Name, UserName, CreationDate, TimeDataRetrieval, 
    TimeProcessing, TimeRendering, ByteCount, [RowCount], 
    path, TimeEnd, Format, 
    COUNT(*) over(partition by format) AS FormatCnt, 
    COUNT(NAME) AS HUH
From
    (SELECT      
        cat.Name, 
        SUBSTRING(ex.UserName, CHARINDEX('\', ex.UserName) + 1, 
        LEN(ex.UserName) - CHARINDEX('\', ex.UserName)) AS UserName, 
        cat.CreationDate, ex.TimeDataRetrieval, ex.TimeProcessing, 
        ex.TimeRendering, ex.ByteCount, ex.[RowCount], 
        cat.path, ex.TimeEnd, ex.Format
     FROM         
        ExecutionLog AS ex 
     INNER JOIN
        Catalog AS cat ON ex.ReportID = cat.ItemID) AS ZZ
--WHERE UserName in (@Logins)
--and
WHERE UserName = 'user1'
  AND (Path NOT LIKE '%Autodelivery%'
       AND Path NOT LIKE '%admin%'
       AND Path NOT LIKE '%qa%'
       AND path NOT LIKE '%test%')
GROUP BY 
   ZZ.UserName, ZZ.Name, ZZ.ByteCount, ZZ.CreationDate, ZZ.Format, ZZ.Path, 
   ZZ.[RowCount], ZZ.TimeDataRetrieval, ZZ.TimeEnd, ZZ.TimeProcessing, 
   ZZ.TimeRendering
ORDER BY 
   Name, TimeEnd DESC

1 个答案:

答案 0 :(得分:1)

在您的代码中,您可以使用此格式...

declare @Logins varchar(max)

SELECT COUNT(a.name)
FROM (    
    Select Name, UserName, CreationDate, TimeDataRetrieval, TimeProcessing,TimeRendering, ByteCount, [RowCount], path, TimeEnd, Format, COUNT(*) over(partition by format) AS FormatCnt, COUNT(NAME)AS HUH
    From
    (
    SELECT      cat.Name, SUBSTRING(ex.UserName, CHARINDEX('\', ex.UserName) + 1, LEN(ex.UserName) - CHARINDEX('\', ex.UserName)) AS UserName, 
                      cat.CreationDate, ex.TimeDataRetrieval, ex.TimeProcessing, ex.TimeRendering, ex.ByteCount, ex.[RowCount], cat.path, ex.TimeEnd, ex.Format
    FROM         ExecutionLog AS ex INNER JOIN
                      Catalog AS cat ON ex.ReportID = cat.ItemID
    ) AS ZZ
    --WHERE UserName in (@Logins)
    --and
    WHERE UserName = 'user1'
     and 
    (Path not like '%Autodelivery%'
        and Path not like '%admin%'
        and Path not like '%qa%'
        and path not like '%test%')
        GROUP BY ZZ.UserName, ZZ.Name, ZZ.ByteCount, ZZ.CreationDate, ZZ.Format, ZZ.Path, ZZ.[RowCount], ZZ.TimeDataRetrieval, ZZ.TimeEnd, ZZ.TimeProcessing, ZZ.TimeRendering
    --order by Name, TimeEnd desc
) a