2016-12-28 41 views
1

如今改造臨時表,我來到了一個查詢,我認爲是「次優」使用計數到查詢

它看起來如下:

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 

唯一缺少的是增加計數,有沒有辦法做到這一點,而不必重複大部分主要選擇?

回答

1

嘗試這種方式將不勝感激..

這裏我用Outer Apply替換了Co-related Sub查詢。直到我的知識Corelated子查詢將比較外部集的每個記錄與內部集的每個記錄。外部申請將在每行執行一次。

;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 
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