Solved

SQL Server Query of active and inactive users

Posted on 2009-05-05
7
404 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
Ransomware-A Revenue Bonanza for Service Providers

Ransomware – malware that gets on your customers’ computers, encrypts their data, and extorts a hefty ransom for the decryption keys – is a surging new threat.  The purpose of this eBook is to educate the reader about ransomware attacks.

 
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

Complete VMware vSphere® ESX(i) & Hyper-V Backup

Capture your entire system, including the host, with patented disk imaging integrated with VMware VADP / Microsoft VSS and RCT. RTOs is as low as 15 seconds with Acronis Active Restore™. You can enjoy unlimited P2V/V2V migrations from any source (even from a different hypervisor)

Question has a verified solution.

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

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…
I have a large data set and a SSIS package. How can I load this file in multi threading?
Via a live example, show how to setup several different housekeeping processes for a SQL Server.
Polish reports in Access so they look terrific. Take yourself to another level. Equations, Back Color, Alternate Back Color. Write easy VBA Code. Tighten space to use less pages. Launch report from a menu, considering criteria only when it is filled…

863 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

26 Experts available now in Live!

Get 1:1 Help Now