Solved

SQL Server Query of active and inactive users

Posted on 2009-05-05
7
402 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
Threat Intelligence Starter Resources

Integrating threat intelligence can be challenging, and not all companies are ready. These resources can help you build awareness and prepare for defense.

 
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

What Security Threats Are You Missing?

Enhance your security with threat intelligence from the web. Get trending threat insights on hackers, exploits, and suspicious IP addresses delivered to your inbox with our free Cyber Daily.

Join & Write a Comment

International Data Corporation (IDC) prognosticates that before the current the year gets over disbursing on IT framework products to be sent in cloud environs will be $37.1B.
Ever needed a SQL 2008 Database replicated/mirrored/log shipped on another server but you can't take the downtime inflicted by initial snapshot or disconnect while T-logs are restored or mirror applied? You can use SQL Server Initialize from Backup…
Via a live example, show how to extract information from SQL Server on Database, Connection and Server properties
Via a live example, show how to set up a backup for SQL Server using a Maintenance Plan and how to schedule the job into SQL Server Agent.

759 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

Need Help in Real-Time?

Connect with top rated Experts

22 Experts available now in Live!

Get 1:1 Help Now