将使用计数的临时表转换为查询

时间:2016-12-28 11:27:07

标签: sql-server count subquery temp-tables

今天我来了一个我认为不太理想的问题'

它看起来如下:

CREATE TABLE #temp ( 
                    Sessionid    UNIQUEIDENTIFIER, 
                    PolicyNumber VARCHAR(30), 
                    StartDate    DATETIME, 
                    RequestUrl   VARCHAR(255), 
                    ClientSource VARCHAR(50),
                    Comment VARCHAR(250),
                    ExceptionCount INT 
                ) 

                INSERT INTO #temp 
                    SELECT sessionid, policynumber, startdate  "Timestamp", requesturl "Instance", clientsource , '' Comment 
                    , (SELECT COUNT(*) from Logging.Exceptions where [Key] = sessionId) as exceptioncount 
                    FROM   [Transaction].[sessions] tr WITH  (nolock)
                     WHERE sessionId = '7B851237-3790-4A6D-9688-2E77843E0BB1' 

                SELECT (SELECT COUNT(*) FROM #temp WHERE exceptioncount > -1) as TotalRecords,
                       (SELECT COUNT(*) FROM #temp WHERE exceptioncount > -1) as TotalDisplayRecords,* from
                    (SELECT * FROM
                        (SELECT ROW_NUMBER() OVER (ORDER BY StartDate  desc) AS rownumber,
                            sessionid, policynumber, startdate  "Timestamp", requesturl "Instance", 
                            clientsource, exceptioncount,Comment            
                         FROM #temp
                         WHERE exceptioncount > -1) as cte
                     WHERE cte.rownumber BETWEEN 1 AND 10) as "Results"


                drop table #temp

我现在以下面的形式重写了

SELECT   
--count(*) TotalRecords,
 --   count(*) TotalDisplayRecords,
                       * from (

SELECT ROW_NUMBER() OVER (ORDER BY "Timestamp"  desc) AS rownumber, * from (
                        SELECT sessionid, policynumber, startdate  "Timestamp", requesturl "Instance", clientsource , '' Comment 
                    , (SELECT COUNT(1) from Logging.Exceptions where [Key] = sessionId) as exceptioncount
                    FROM   [Transaction].[sessions] tr WITH  (nolock)
                     WHERE sessionId = '7B851237-3790-4A6D-9688-2E77843E0BB1' ) withrownumber 
                     WHERE exceptioncount > -1 ) as cte

唯一缺少的是添加计数,有没有办法在不重复主要选择的情况下执行此操作?

2 个答案:

答案 0 :(得分:1)

试试这种方式..

在这里,我已将{Co-related Sub查询替换为Outer Apply。根据我的知识,Corelated Sub查询会将Outer Set的每条记录与Inner set的每条记录进行比较。外部应用将在每行执行一次。

;WITH CTE AS (
    SELECT ROW_NUMBER() OVER (ORDER BY StartDate DESC) AS rownumber
    , sessionid
    , policynumber
    , startdate  AS [Timestamp]
    , requesturl AS [Instance]
    , clientsource
    , '' Comment
    , EXC.exceptioncount 
    FROM [Transaction].[sessions] tr WITH (NOLOCK)
    OUTER APPLY
    (
        SELECT COUNT(1) AS exceptioncount 
            FROM Logging.Exceptions
            WHERE [Key] = tr.sessionId
    )AS EXC
    WHERE sessionId = '7B851237-3790-4A6D-9688-2E77843E0BB1'
)

SELECT 
(SELECT COUNT(1) FROM CTE WHERE exceptioncount > - 1 ) AS TotalRecords
,(SELECT COUNT(1) FROM CTE WHERE exceptioncount > - 1 ) AS TotalDisplayRecords
, sessionid
, policynumber
, [Timestamp]
, [Instance]
, clientsource
, Comment
, exceptioncount 
FROM CTE 
WHERE rownumber BETWEEN 1 AND 10

答案 1 :(得分:0)

这就是我最终的结果,我不太喜欢它,如果有人想出更优雅的解决方案,我将不胜感激

    select * from 
            (SELECT   
                count(1) TotalRecords,
                count(1) TotalDisplayRecords,
                * from (
                    SELECT ROW_NUMBER() OVER (ORDER BY "Timestamp"  desc) AS rownumber, * from (
                        SELECT sessionid, policynumber, startdate  "Timestamp", requesturl "Instance", clientsource, 
                            (SELECT COUNT(1) from Logging.Exceptions  WITH  (nolock) where [Key] = sessionId) as exceptioncount,
                        '' Comment 
                        FROM [Transaction].[sessions] tr WITH (nolock)
                        WHERE sessionId = '7B851237-3790-4A6D-9688-2E77843E0BB1') withrownumber 
                WHERE exceptioncount > -1 ) as cte
                group by rownumber, sessionid, policynumber,  "Timestamp",  "Instance", clientsource ,  Comment, exceptioncount) as r
        WHERE r.rownumber BETWEEN 1 AND 10