Database Management
 
Forums: » Register « |  User CP |  Games |  Calendar |  Members |  FAQs |  Sitemap |  Support | 
User Name:
Password:
Remember me
Go Back   Dev Shed ForumsDatabasesDatabase Management

Reply
Add This Thread To:
  Del.icio.us   Digg   Google   Spurl   Blink   Furl   Simpy   Y! MyWeb 
Thread Tools Search this Thread Rate Thread Display Modes
  #1  
Old May 1st, 2003, 09:15 AM
chatguy2020 chatguy2020 is offline
Junior Member
Dev Shed Newbie (0 - 499 posts)
 
Join Date: Apr 2003
Posts: 4 chatguy2020 User rank is Just a Lowly Private (1 - 20 Reputation Level) 
Time spent in forums: < 1 sec
Reputation Power: 0
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  
Old May 1st, 2003, 11:49 AM
bcyde's Avatar
bcyde bcyde is offline
Me likey breadsticks...
Dev Shed Beginner (1000 - 1499 posts)
 
Join Date: Jan 2003
Location: Los Angeles
Posts: 1,189 bcyde User rank is Sergeant (500 - 2000 Reputation Level)bcyde User rank is Sergeant (500 - 2000 Reputation Level)bcyde User rank is Sergeant (500 - 2000 Reputation Level)bcyde User rank is Sergeant (500 - 2000 Reputation Level)bcyde User rank is Sergeant (500 - 2000 Reputation Level) 
Time spent in forums: 3 Days 1 h 12 m 57 sec
Reputation Power: 12
Send a message via AIM to bcyde Send a message via Yahoo to bcyde
Not exactly sure what DB you're using, but here's how I'd do it in Postgres:

SELECT login.userid FROM (SELECT userid, count(*) as logincount FROM usertab WHERE action = 'LOGIN' GROUP BY userid) as login,
(SELECT userid, count(*) as logoutcount FROM usertab WHERE action = 'LOGOUT' GROUP BY userid) as logout WHERE login.userid = logout.userid AND login.logincount > logout.logoutcount

I think that should do it. Maybe someone can come up with something a bit cleaner.

-b
__________________
PostgreSQL, it's what's for dinner...

Reply With Quote
  #3  
Old May 1st, 2003, 01:46 PM
vorlon000001 vorlon000001 is offline
Junior Member
Dev Shed Newbie (0 - 499 posts)
 
Join Date: May 2003
Posts: 2 vorlon000001 User rank is Just a Lowly Private (1 - 20 Reputation Level) 
Time spent in forums: 23 m 28 sec
Reputation Power: 0
try something like this (no guarantees on syntax)...

select result.userid
from (select userid as login_userid,count(*) as login_count
from usertab
where action = 'login'
group by userid) login_result
inner join
(select userid as logout_userid,count(*) as logout_count
from usertab
where action = 'logout'
group by userid) logout_result
on logout_result.userid = login_result.userid
where logout_result.logout_count <>
login_result.login_count
and login_result.login_count <>
logout_result.logout_count + 1; /* user may be logged in */

HTH

v

Reply With Quote
Reply

Viewing: Dev Shed ForumsDatabasesDatabase Management > complex query with count and group by


Thread Tools  Search this Thread 
Search this Thread:

Advanced Search
Display Modes  Rate This Thread 
Rate This Thread:


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 On
HTML code is Off
View Your Warnings | New Posts | Latest News | Latest Threads | Shoutbox
Forum Jump

 Free IT White Papers!
 
Accelerating Trading Partner Performance
One in five. That's how many partner transactions have at least one error. That is an amazing statistic, particularly given the extraordinary leaps in innovation across the global supply chain during the past two decades. Download this white paper to learn more.

 
Competing on Analytics
This Tech Analysis is designed to help identify characteristics shared by analytics competitors, and includes information about 32 organizations that have made a commitment to quantitative, fact-based analysis.

 
Cost Effective Scaling with Virtualization and Coyote Point Systems
An overview of the industry trend toward virtualization, how server consolidation has increased the importance of application uptime and the steps being taken to integrate load balancing technology with virtualized servers.

 
Five Checkpoints to Implementing IP Telephony
Implementation planning for IP PBX software and IP telephony has become vital as businesses replace discontinued legacy PBX phone systems. This informative whitepaper outlines five "checkpoints" for any implementation plan that will help make IP communications a successful proposition.

 
Hosted Email Security: Staying Ahead of New Threats
In the last two years, email has become a fierce battleground between the nefarious forces of spam and malware, and the heroes of messaging protection. The spam volumes increased alarmingly every month, bringing clever new forms of phishing and virus propagation attacks.

 

Forums: » Register « |  User CP |  Games |  Calendar |  Members |  FAQs |  Sitemap |  Support | 
  
 





© 2003-2008 by Developer Shed. All rights reserved. DS Cluster 1 hosted by Hostway