home *** CD-ROM | disk | FTP | other *** search
/ moodle.waes.ac.uk / moodle.waes.ac.uk.zip / moodle.waes.ac.uk / TMG / SP1-TMG-KB981324-AMD64-ENU.msp / PCW_CAB_SHFx2 / F2143_msfpcui.dll / BINARY / 25494 < prev    next >
Text File  |  2010-06-15  |  4KB  |  105 lines

  1. CREATE PROCEDURE [dbo].[ISA_spAppTopUsersRDL]
  2.     @FromDate datetime,
  3.     @ToDate datetime,
  4.     @ReportType varchar(10),
  5.     @TopCount int,
  6.     @SortOrder varchar(100)
  7. AS
  8. BEGIN
  9.     DECLARE @SummTableName varchar(100)
  10.     SET @SummTableName = dbo.fnGetSummaryTableName('tblUserSummary', @FromDate, @ToDate, @ReportType)
  11.     DECLARE @OrderColumn varchar(100)
  12.     SET @OrderColumn =
  13.             CASE
  14.                 WHEN @SortOrder = 'Requests' THEN 'Requests'
  15.                 WHEN @SortOrder = 'BytesIn' THEN 'BytesIn'
  16.                 WHEN @SortOrder = 'BytesOut' THEN 'BytesOut'
  17.                 ELSE 'TotalBytes'
  18.             END;
  19.     CREATE TABLE #Table1
  20.     (
  21.         UserName nvarchar(514),
  22.         Requests bigint,
  23.         BytesIn bigint,
  24.         BytesOut bigint,
  25.         TotalBytes bigint
  26.     )
  27.     IF OBJECT_ID (@SummTableName, 'u') IS NOT NULL
  28.     BEGIN
  29.         DECLARE @strQuery varchar(8000)
  30.         SET @strQuery =
  31.         'SELECT
  32.             [UserName] ,
  33.             SUM([Requests]) - SUM([WebRequests]) AS Requests,
  34.             SUM([BytesIn]) - SUM([WebBytesIn]) AS BytesIn,
  35.             SUM([BytesOut]) - SUM([WebBytesOut]) AS BytesOut,
  36.             SUM([TotalBytes]) - SUM([WebTotalBytes]) AS TotalBytes
  37.         FROM ' + @SummTableName + '
  38.         WHERE
  39.             ([Requests] - [WebRequests]) >0 AND
  40.             ([date] >= ' + QUOTENAME(CONVERT(varchar, @FromDate, 126),CHAR(39)) + ' AND [date] <= ' + QUOTENAME(CONVERT(varchar, @ToDate, 126),CHAR(39)) + ')
  41.         GROUP BY
  42.             [UserName]
  43.         ORDER BY ' + QUOTENAME(@OrderColumn) + ' DESC;
  44.         '
  45.         INSERT #Table1
  46.         EXEC(@strQuery)
  47.     END
  48.     CREATE TABLE #Table2
  49.     (
  50.         UserName nvarchar(514),
  51.         Requests bigint,
  52.         BytesIn bigint,
  53.         BytesOut bigint,
  54.         TotalBytes bigint
  55.     )
  56.     INSERT INTO #Table2
  57.     SELECT TOP(@TopCount)
  58.         [UserName] AS UserName,
  59.         [Requests] AS Requests,
  60.         [BytesIn] AS BytesIn,
  61.         [BytesOut] AS BytesOut,
  62.         [TotalBytes] AS TotalBytes
  63.     FROM #Table1
  64.     WHERE [UserName] != N'{[23124]}'
  65.     INSERT INTO #Table2
  66.     SELECT
  67.        N'{[23124]}' As UserName,
  68.         SUM(Requests) AS Requests,
  69.         SUM([BytesIn]) AS BytesIn,
  70.         SUM([BytesOut]) AS BytesOut,
  71.         SUM([TotalBytes]) AS TotalBytes
  72.     FROM #Table1 t1
  73.     WHERE NOT EXISTS
  74.         (SELECT *
  75.          FROM #Table2 t2
  76.          WHERE t1.UserName = t2.UserName)
  77.     DECLARE @TotalRequests bigint
  78.     SET @TotalRequests = (SELECT SUM(Requests) FROM #Table2 )
  79.     IF (@TotalRequests = 0)
  80.         SET @TotalRequests = 1
  81.     DECLARE @totalBytes bigint
  82.     SET @totalBytes = (SELECT SUM(TotalBytes) FROM #Table2 )
  83.     IF (@totalBytes = 0)
  84.         SET @totalBytes = 1
  85.     DECLARE @TotalBytesIn bigint
  86.     SET @TotalBytesIn = (SELECT SUM(BytesIn) FROM #Table2)
  87.     IF (@TotalBytesIn = 0)
  88.         SET @TotalBytesIn = 1
  89.     DECLARE @TotalBytesOut bigint
  90.     SET @TotalBytesOut = (SELECT SUM(BytesOut) FROM #Table2)
  91.     IF (@TotalBytesOut = 0)
  92.         SET @TotalBytesOut = 1
  93.     SELECT
  94.         [dbo].[fnUserOrIpAddressToText]([UserName]) AS [UserName],
  95.         [Requests] AS Requests,
  96.         (CAST([Requests] AS decimal) / @TotalRequests) AS RequestRatio,
  97.         [BytesIn] AS BytesIn,
  98.         (CAST([BytesIn] AS decimal) / @TotalBytesIn) AS BytesInRatio,
  99.         [BytesOut] AS BytesOut,
  100.         (CAST([BytesOut] AS decimal) / @TotalBytesOut) AS BytesOutRatio,
  101.         [TotalBytes] AS TotalBytes,
  102.         (CAST([TotalBytes] AS decimal) / @totalBytes) AS TotalByteRatio
  103.     FROM #Table2
  104. END
  105.