Solved

SQL Server Query of active and inactive users

Posted on 2009-05-05
7
408 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
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
3 Use Cases for Connected Systems

Our Dev teams are like yours. They’re continually cranking out code for new features/bugs fixes, testing, deploying, testing some more, responding to production monitoring events and more. It’s complex. So, we thought you’d like to see what’s working for us.

 
LVL 40

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

PRTG Network Monitor: Intuitive Network Monitoring

Network Monitoring is essential to ensure that computer systems and network devices are running. Use PRTG to monitor LANs, servers, websites, applications and devices, bandwidth, virtual environments, remote systems, IoT, and many more. PRTG is easy to set up & use.

Question has a verified solution.

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

Suggested Solutions

Why is this different from all of the other step by step guides?  Because I make a living as a DBA and not as a writer and I lived through this experience. Defining the name: When I talk to people they say different names on this subject stuff l…
Many companies are looking to get out of the datacenter business and to services like Microsoft Azure to provide Infrastructure as a Service (IaaS) solutions for legacy client server workloads, rather than continuing to make capital investments in h…
Via a live example, show how to shrink a transaction log file down to a reasonable size.
Viewers will learn how the fundamental information of how to create a table.

831 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