
                
                
                SELECT 
              	replace(
              	    replace(
              	        replace(
              	            replace(to_char(us.LoginTime, 'YYYY-MM-DD HH24:MI:SS.mi'), '-', ''),
              	            ':', ''),
              	        '.', ''),
              	    ' ', ''
              	) as "Unic_id",
            	us.Session_id as "Session_id",
            	us.IP as "IP",
              	pu.PMUser_id as "PMUser_id",
              	LTRIM(RTRIM(pu.PMUser_Name)) as "PMUser_Name",
              	to_char(us.LoginTime, 'YYYY-MM-DD HH24:MI:SS') as "LoginTime",
              	to_char(us.LogoutTime, 'YYYY-MM-DD HH24:MI:SS') as "LogoutTime",
              	case
              	    when us.Status = 1 THEN datediff('ss', us.LoginTime, coalesce(us.LogoutTime, localtimestamp))
              	    else null
              	end as "WorkTime",
              	case when us.Status = '1' then 'удачный вход' else 'неудачный вход' end as "Status",
              	case when us.Status = '1' then 1 else 2 end as "Status_id",
                case
               		when us.AuthType_id = '1' then 'по логину/паролю'
               		when us.AuthType_id = '2' then 'по соцкарте'
               		when us.AuthType_id = '3' then 'через УЭК'
                   	when us.AuthType_id = '4' then 'через ЭЦП'
                   	when us.AuthType_id = '5' then 'через ЕСИА'
                    else ''
               	end as "AuthType_id",
              	pu.PMUser_Login as "PMUser_Login",
              	case when pu.MedPersonal_id is not null
              	    then 'true' else 'false'
              	end as "IsMedPersonal",
              	(SELECT COUNT(*) FROM UserSessions
                    WHERE pmUser_id = us.pmUser_id
                    AND LoginTime > '2023-07-21'
                    AND LogoutTime IS NULL) as "ParallelSessions"
            	
                FROM
                
                UserSessions us
           		left join promed.pmUserCache pu on us.pmUser_id = pu.pmUser_id
        	 WHERE 
                    to_char(us.LoginTime, 'YYYY-MM-DD HH24:MI:SS') >= '2023-07-14 00:00:00' and to_char(us.LoginTime, 'YYYY-MM-DD HH24:MI:SS') <= '2023-07-21 23:59:59' and 
                    pu.PMUser_id in (
                        select puco.pmUserCache_id
                        from promed.pmUserCacheOrg puco
                        where puco.Org_id = '13091664'
                    )
                
                
                
                        ORDER BY
                            
            us.LoginTime desc
            
                OFFSET 0 ROWS
                FETCH NEXT 100000 ROWS ONLY
                
