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