Solved

SQL Server Query of active and inactive users

Posted on 2009-05-05
7
416 Views
Last Modified: 2012-05-06
I've got a [Users] table and a [products] table.

There are 3 products that a user can subscribe to.  He must select at least 1, but can also select all 3.

I want to create a query that shows a list of all the active and inactive users for a selected product.
Any idea how I can create a simple query to do that?

0
Comment
Question by:koossa
[X]
Welcome to Experts Exchange

Add your voice to the tech community where 5M+ people just like you are talking about what matters.

  • Help others & share knowledge
  • Earn cash & points
  • Learn & ask questions
7 Comments
 
LVL 10

Expert Comment

by:mahome
ID: 24302246
Please give us your table structure for better help.
0
 

Author Comment

by:koossa
ID: 24302269
[Users Table]
  [ID]
  [Name]
  [Surname]


[Products Table]
  [ID]
  [UserID]
  [Product Name]


0
 
LVL 25

Accepted Solution

by:
lwadwell earned 500 total points
ID: 24302296
Hi koossa,

For a simple report for a specified product, try ...

SELECT t1.Name, t1.Surname, CASE WHEN t2.ID IS NULL THEN 'Inactive' ELSE 'Active' END as Status
FROM Users as t1
LEFT JOIN Products as t2 ON t1.ID = t2.UserID AND t1.ProductName = 'specify your value here'


lwadwell
0
Best Practices: Disaster Recovery Testing

Besides backup, any IT division should have a disaster recovery plan. You will find a few tips below relating to the development of such a plan and to what issues one should pay special attention in the course of backup planning.

 
LVL 41

Expert Comment

by:Sharath
ID: 24302359
>> I want to create a query that shows a list of all the active and inactive users for a selected product.

Can you define active and inactive as per your requirement?
0
 
LVL 31

Expert Comment

by:RiteshShah
ID: 24302430
you may want this:



 --for active
  select u.ID
  from [Users Table] as u 
  where exists (select userid from [product table] where userid=u.id)
  
  --for non-active
  select u.ID
  from [Users Table] as u 
  where not exists (select userid from [product table] where userid=u.id)

Open in new window

0
 

Author Comment

by:koossa
ID: 24302450
Hi lwadwell

Thank you, it's exactly what I want.
I also have a field name [Not confirmed] in the Users table that I don't want to show.
When I try to add it in my query it still shows all the records.

SELECT t1.Name, t1.Surname, CASE WHEN t2.ID IS NULL THEN 'Inactive' ELSE 'Active' END AS Status
FROM         Users AS t1 LEFT JOIN
                      ProductSubscriptions AS t2 ON t1.ID = t2.UserID AND t2.ProductID = 2 AND t1.[Not confirmed] = 0
0
 
LVL 31

Expert Comment

by:RiteshShah
ID: 24302463
what about this one?




  select u.ID,'active' as stat
  from [Users Table] as u 
  where exists (select userid from [product table] where userid=u.id)
  union
  select u.ID,'non-active' as stat
  from [Users Table] as u 
  where not exists (select userid from [product table] where userid=u.id)

Open in new window

0

Featured Post

Free eBook: Backup on AWS

Everything you need to know about backup and disaster recovery with AWS, for FREE!

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

Suggested Solutions

I have a large data set and a SSIS package. How can I load this file in multi threading?
This article shows the steps required to install WordPress on Azure. Web Apps, Mobile Apps, API Apps, or Functions, in Azure all these run in an App Service plan. WordPress is no exception and requires an App Service Plan and Database to install
Via a live example, show how to backup a database, simulate a failure backup the tail of the database transaction log and perform the restore.
Via a live example, show how to setup several different housekeeping processes for a SQL Server.

710 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question