Still celebrating National IT Professionals Day with 3 months of free Premium Membership. Use Code ITDAY17

x
?
Solved

SQL Server Query of active and inactive users

Posted on 2009-05-05
7
Medium Priority
?
425 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 1500 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
NFR key for Veeam Agent for Linux

Veeam is happy to provide a free NFR license for one year.  It allows for the non‑production use and valid for five workstations and two servers. Veeam Agent for Linux is a simple backup tool for your Linux installations, both on‑premises and in the public cloud.

 
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

Independent Software Vendors: We Want Your Opinion

We value your feedback.

Take our survey and automatically be enter to win anyone of the following:
Yeti Cooler, Amazon eGift Card, and Movie eGift Card!

Question has a verified solution.

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

In part one, we reviewed the prerequisites required for installing SQL Server vNext. In this part we will explore how to install Microsoft's SQL Server on Ubuntu 16.04.
In this article, we’ll look at how to deploy ProxySQL.
Via a live example, show how to extract insert data into a SQL Server database table using the Import/Export option and Bulk Insert.
Using examples as well as descriptions, and references to Books Online, show the documentation available for datatypes, explain the available data types and show how data can be passed into and out of variables.

722 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