Welcome to the dBforums forums.

You are currently viewing our boards as a guest which gives you limited access to view most discussions, articles and access our other FREE features. By joining our free community you will have access to post topics, communicate privately with other members (PM), respond to polls, upload your own photos and access many other special features. Registration is fast, simple and absolutely free so please, join our community today!

If you have any problems with the registration process or your account login, please contact contact support.

If you prefer not to see double-underlined words and corresponding ads, place your cursor
here for ContentLink opt out.

Go Back  dBforums > Database Server Software > Informix > complex query with count and group by

Reply
 
LinkBack Thread Tools Search this Thread Display Modes
  #1 (permalink)  
Old 05-01-03, 11:27
chatguy2020 chatguy2020 is offline
Registered User
 
Join Date: May 2003
Posts: 58
complex query with count and group by

Hey I have a table with the following fields:

userid
action

now, action include login and logout. Here in this table there will be multiple userids with multiple actions. Is there a way that we can retrieve the user ids whose Logins are greater than logouts?

I wrote this:

select distinct(userid), action, count(*) from usertab where action in("LOGIN", "LOGOUT") group by userid, action order by userid desc

A snippet of result is:

ROBL LOGIN 2
ROBL LOGOUT 2
RIOS LOGIN 4
RIOS LOGOUT 4
PRATI LOGIN 43
PRATI LOGOUT 37

Now I need to extract PRATI only as that user has more logins than logouts. That way, I can get only users whose logins are more than logouts. I tried using count and having but I didn't get the result.

Is there any way to do it without views?

Any answers would be appreciated.

Thanks!
Reply With Quote
  #2 (permalink)  
Old 05-01-03, 12:12
r937 r937 is offline
SQL Consultant
 
Join Date: Apr 2002
Location: Toronto, Canada
Posts: 13,561
does informix allow derived tables in sql?
Code:
select login.userid , login.howmany as logins , coalesce(logout.howmany,0) as logouts from ( select userid, count(*) as howmany from usertab where action = 'LOGIN' group by userid ) login left outer join ( select userid, count(*) as howmany from usertab where action = 'LOGOUT' group by userid ) logout on login.userid = logout.userid where login.howmany > coalesce(logout.howmany,0)
untested

rudy
http://r937.com/
Reply With Quote
Reply


Thread Tools Search this Thread
Search this Thread:

Advanced Search
Display Modes

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

vB code is On
Smilies are On
[IMG] code is Off
HTML code is Off
Trackbacks are On
Pingbacks are On
Refbacks are On