Sqlserver
 sql >> Teknologi Basis Data >  >> RDS >> Sqlserver

SQL maks sesi bersamaan per jam dalam sehari

OK, mengambil pendekatan yang sedikit berbeda. Saya membuat tabel Logon/Log Off - yang bisa dalam cte tetapi saya bukan ahli oracle jadi tidak yakin apakah itu didukung jadi hilang untuk SQL yang lebih sederhana dan tabel temp. Saya semua telah membuat tabel bilangan bulat statis (juga dikenal sebagai tabel penghitungan) untuk 0-30 untuk menangani rentang tanggal pengujian dalam kueri saya berdasarkan jawaban Anda di atas - jika Anda sudah memilikinya, Anda bisa menggunakannya dengan rentang ke bawah dan menyimpan beberapa upaya. Menjalankan kueri akan mengembalikan tabel seperti ini (menggunakan data sampel Anda juga di bawah)

StartDate   WorkingHour LoggedInUsers
2002-10-17 00:00:00.000 10  0
2002-10-17 00:00:00.000 11  0
2002-10-17 00:00:00.000 12  0
2002-10-17 00:00:00.000 13  0
2002-10-17 00:00:00.000 14  2
2002-10-17 00:00:00.000 15  5
2002-10-17 00:00:00.000 16  3
2002-10-17 00:00:00.000 17  2
2002-10-17 00:00:00.000 18  2
2002-10-17 00:00:00.000 19  1
2002-10-17 00:00:00.000 20  1
2002-10-17 00:00:00.000 21  1
2002-10-17 00:00:00.000 22  1
2002-10-17 00:00:00.000 23  0

Kode sebagai Jalankan di sql

DECLARE @StartDate DATETIME ,
@NoDays INT ;


select
@StartDate = '2002-10-01',
@NoDays = 20;



DECLARE @Sessions TABLE (
UserRef UNIQUEIDENTIFIER,
DATE DATETIME,
LogType VARCHAR(100), 
SessionID UNIQUEIDENTIFIER
);
INSERT INTO @Sessions
SELECT '96AD647C-D061-43F5-9F8D-FA6C74817E07', '2002-10-17 14:11:25.763', 'LOGON', '8D451569-0260-46BB-9B9E-F49B3E778161'
UNION SELECT '96AD647C-D061-43F5-9F8D-FA6C74817E07', '2002-10-17 18:11:32.547', 'LOGOFF', '8D451569-0260-46BB-9B9E-F49B3E778161'
UNION SELECT '60738820-5F72-4E20-A070-57E07C83B6DE', '2002-10-17 14:53:31.153', 'LOGON', 'C773894C-8B2D-4054-A550-3F04B4C5669F'
UNION SELECT '60738820-5F72-4E20-A070-57E07C83B6DE', '2002-10-17 22:55:25.607', 'LOGOFF', 'C773894C-8B2D-4054-A550-3F04B4C5669F'
UNION SELECT '90A55FDD-967E-4D99-96DF-96840CDB2CDF', '2002-10-17 15:26:40.123', 'LOGON', '1CE5F5A5-4E20-4D4A-BB67-EB0CB33976D7'
UNION SELECT '96AD647C-D061-43F5-9F8D-FA6C74817E07', '2002-10-17 15:51:28.590', 'LOGON', '7EFDEE1C-15CF-4DE1-B59F-7AFC49B4BE73'
UNION SELECT '90A55FDD-967E-4D99-96DF-96840CDB2CDF', '2002-10-17 15:58:05.217', 'LOGOFF', '1CE5F5A5-4E20-4D4A-BB67-EB0CB33976D7'
UNION SELECT '96AD647C-D061-43F5-9F8D-FA6C74817E07', '2002-10-17 15:58:31.013', 'LOGOFF', '7EFDEE1C-15CF-4DE1-B59F-7AFC49B4BE73'
UNION SELECT '90A55FDD-967E-4D99-96DF-96840CDB2CDF', '2002-10-17 15:58:32.733', 'LOGON', '03F56AB8-FED5-4CC7-8445-26BF55F58E60'
UNION SELECT '90A55FDD-967E-4D99-96DF-96840CDB2CDF', '2002-10-17 16:13:02.827', 'LOGOFF', '03F56AB8-FED5-4CC7-8445-26BF55F58E60';  


DECLARE @staticintegers TABLE (myInteger INT);
INSERT INTO @staticintegers 
SELECT 0 UNION SELECT 1  UNION SELECT 2 UNION SELECT 3
UNION SELECT 4 UNION SELECT 5 UNION SELECT 6 UNION SELECT 7 UNION SELECT 8
UNION SELECT 9 UNION SELECT 10 UNION SELECT 11 UNION SELECT 12 UNION SELECT 13
UNION SELECT 14 UNION SELECT 15 UNION SELECT 16 UNION SELECT 17 UNION SELECT 18
UNION SELECT 19 UNION SELECT 20 UNION SELECT 21 UNION SELECT 22 UNION SELECT 23
UNION SELECT 24 UNION SELECT 25 UNION SELECT 26 UNION SELECT 27 UNION SELECT 28
UNION SELECT 29 UNION SELECT 30 UNION SELECT 31 UNION SELECT 32 UNION SELECT 33;

DECLARE @sessionsOutput TABLE (SessionID UNIQUEIDENTIFIER, StartTime DATETIME, EndTime DATETIME);
INSERT INTO @sessionsOutput
(SessionID, StartTime)
SELECT 
    SessionID,
    [date]
FROM 
    @Sessions
    WHERE logtype = 'Logon';

UPDATE @sessionsOutput
SET EndTime = [date]
FROM @sessionsOutput aa
INNER JOIN @Sessions bb
ON aa.SessionID = bb.SessionID
WHERE bb.LogType = 'Logoff';



SELECT
    DATEADD(dd, DateIntegers.myInteger, @StartDate) AS StartDate
    ,hoursintegers.myINteger AS WorkingHour
    ,COUNT(aa.SessionID) AS LoggedInUsers


FROM 
    @staticintegers DateIntegers
    LEFT OUTER JOIN @StaticIntegers HoursIntegers
    ON HoursIntegers.myInteger BETWEEN 0 AND 23

LEFT OUTER JOIN @sessionsOutput aa
ON
    HoursIntegers.myInteger BETWEEN DATEPART(hh, aa.StartTime) AND DATEPART(hh, aa.endtime)
    AND CAST(aa.StartTime AS DATE) = DATEADD(dd, dateintegers.myInteger, @StartDate) 

GROUP BY 
    DATEADD(dd, DateIntegers.myInteger, @StartDate),
    HoursIntegers.myInteger
ORDER BY 
    DATEADD(dd, DateIntegers.myInteger, @StartDate),
    HoursIntegers.myInteger;



  1. Database
  2.   
  3. Mysql
  4.   
  5. Oracle
  6.   
  7. Sqlserver
  8.   
  9. PostgreSQL
  10.   
  11. Access
  12.   
  13. SQLite
  14.   
  15. MariaDB
  1. Cek kosong atau kosong untuk variabel string

  2. Ekspor Excel ke SQL menggunakan VBA

  3. Ubah hasil SQL Server yang disetel menjadi string

  4. Warisan dalam database?

  5. Jalankan prosedur tersimpan di blok OUTER APPLY