views:

33

answers:

1

I'm using SQL-Server 2005 Standard.

I have Users table with following columns:

userID(int),
userImage(varchar),
userText(varchar),
userLastVisit(smalldatetime),
isActivated (bit),
userHobby1 (bit),
.....
userHobby10 (bit)
userCharacteristic1 (bit),
.....
userCharacteristic10 (bit)

I did 6 queries to select

  1. users that haven't been on site for two weeks
  2. users that doesn't have picture
  3. users that doesn't have text
  4. users that doesn't have a single hobby
  5. users that doesn't have a single characteristic
  6. users that did not activate their account

Now what i need to get is list of users with actions that need to be told about.

For example userID 2004 doesn't have an image and doesn't have a single hobby. Each single user should appear only once in the list with all it's actions need to be done.

Thanks in advance.

+2  A: 

Try string concatenation and CASE WHEN:

SELECT UserID,
    CASE WHEN UserImage IS NULL THEN 'no image.' ELSE '' END +
    CASE WHEN UserText IS NULL THEN 'no text.' ELSE '' END +
    ... -- more CASE WHEN conditions
    AS Info
WHERE (UserImage IS NULL) 
   OR (UserText IS NULL)
   ... -- OR same conditions as in CASE WHEN clauses
devio
@devio, wouldn't it be better to create temp table so i can insert bits and userID into it?
eugeneK
This is a single query returning unique UserIDs and all required information. If you use a temp table, you'd need an INSERT SELECT *and* an UPDATE statement (including INNER and OUTER JOIN to the temp table resp.) for each condition, as several criteria might match a single user (as in your OP).
devio