Re: sessions numbers





Tom wrote:
> RHEL 3
> 9.2.0.4
>
> I currently log into a file the number of connected users at a given
> time with
>
> select username, count(*) from v$session group by username;
>
> Is there anyway to include session numbers into this also? eg each user
> has x sessions?
>
> thanks!

I don't understand why your query isn't returning such information, as
you have it written to provide such data:

USERNAME COUNT(*)
------------------------------ ----------
ETSDEV 2
SYS 1
7

Your count(*), from v$session, provides the number of sessions each
username has. Why do you need sessionid with that? What purpose does
it serve?

SQL> select username, sid, count(*)
2 from v$session
3 group by username, sid;

USERNAME SID COUNT(*)
------------------------------ ---------- ----------
1 1
2 1
3 1
4 1
5 1
6 1
7 1
SYS 8 1
ETSDEV 9 1
ETSDEV 11 1

10 rows selected.

You end up with each username listed for each session they hold, each
with a count of 1 (since SID is included in the grouping, and it's
unique in conjunction with the username), unlike your original output
which listed distinct usernames (by virtue of the group by) and a total
of the sessions for each user. And, it sounds as though what you
really want is your original output. But, if you DO want such output
then a little 'magic' on the part of SQL*Plus is in order:

SQL> break on username skip 1
SQL> select username, sid, count(*)
2 from v$session
3 group by username, sid;

USERNAME SID COUNT(*)
------------------------------ ---------- ----------
1 1
2 1
3 1
4 1
5 1
6 1
7 1

SYS 8 1

ETSDEV 9 1
11 1

10 rows selected.

Although I still fail to see what the SID is going to provide you, as
you need both the SID and SERIAL# to identify the session. Possibly
this is what you want:

SQL> select username, sid, serial#
2 from v$session;

USERNAME SID SERIAL#
------------------------------ ---------- ----------
1 1
2 1
3 1
4 1
5 1
6 1
7 1

SYS 8 114

ETSDEV 9 9
11 11


10 rows selected.

Or, possibly, SQL*Plus won't provide what you want and, thus, you need
to move to PL/SQL:

declare
cursor get_user_sess is
select username, sid, serial#
from v$session
where username is not null;

ucount number:=0;

prevuser v$session.username%type:=NULL;
outvar varchar2(200);
begin
for sessrec in get_user_sess loop
if prevuser is null then
ucount := 1;
prevuser := sessrec.username;
outvar := sessrec.username||'
('||sessrec.sid||','||sessrec.serial#||')';
elsif prevuser = sessrec.username then
ucount := ucount + 1;
prevuser := sessrec.username;
outvar := outvar||'
'||sessrec.sid||','||sessrec.serial#||')';
elsif prevuser <> sessrec.username then
outvar := outvar ||' sessions: '||ucount;
dbms_output.put_line(outvar);
ucount := 1;
prevuser := sessrec.username;
outvar := sessrec.username||'
('||sessrec.sid||','||sessrec.serial#||')';
end if;
end loop;
outvar := outvar ||' sessions: '||ucount;
dbms_output.put_line(outvar);
end;
/

SQL> @sessions
SYS (8,114) sessions: 1
ETSDEV (9,9) (11,11) sessions: 2

PL/SQL procedure successfully completed.

SQL>

Possibly the examples I've provided will prod you into being a clearer
on what you really want.


David Fitzjarrell

.



Relevant Pages

  • Re: Left Join Problem
    ... Why is access seem so...nonstandard when it comes to SQL? ... >> WHERE scoutid IS NULL; ... >>Please note that sid is a variable, ... > FROM [sessions] ...
    (microsoft.public.access.tablesdbdesign)
  • Re: ASP.NET User.Identity.Name value after a domain username chang
    ... Can you point me to any resources I can use about getting the SID of the ... WebRequest user without using the username as the base of a lookup (since I ... access a website on the server. ... I have a very confusing issue when the domain login of a user is changed ...
    (microsoft.public.dotnet.framework.aspnet.security)
  • Re: Problems adding users
    ... I had to do some SQL surgery this afternoon that may or may not help. ... A domain username was ... First we tried to rename the user, but WSS ... >> account in the AD was never deleted, the account name for this user was ...
    (microsoft.public.sharepoint.windowsservices)
  • RE: How to evade white spaces in a SQL injection
    ... The 2nd one is more likely since they're proably adding the ' character to ... So you'd enter '+1-- as the username. ... +1 tells sql to add 1 to a character field, which it can't do, the -- ... comments out the remainder of the query so it doesn't get parsed. ...
    (Pen-Test)
  • Re: Need help with "User does not exist" error
    ... You may have some old remnants in SQL. ... Try searching for both the username and the ... been added to several WSS sites. ... "User Management" as the misspelled username. ...
    (microsoft.public.sharepoint.windowsservices)