Marck
Marck

Reputation: 65

SQL get values without duplicates (that are different in case)

I'm trying to get a list of all users in my database, but when I run the following query:

SELECT cu.user_name AS username, cu.display_name AS displayname, 
cm.lower_parent_name AS group, cu.email_address AS email
FROM cwd_user AS cu
INNER JOIN cwd_membership AS cm
ON cu.directory_id = cm.directory_id
AND cu.lower_user_name = cm.lower_child_name
AND cm.membership_type = 'GROUP_USER'
WHERE cm.lower_parent_name LIKE 'zz%'
ORDER BY cu.user_name;

I get duplicate entries (that's OK, because I have multiple values for the same user) but I'm only interested in the lowercase one.

+------------------------------------------------------------------+
|      username        |    displayname     |   group  |   email   |
+------------------------------------------------------------------+
|   firstname.lastname | Firstname Lastname | zz group | [email protected] |
|   Firstname.Lastname | Firstname Lastname | zz group | [email protected] |
+------------------------------------------------------------------+

I just want one of these users (preferably the first one), so I tried the following SQL query:

SELECT t.user_name, cu.display_name, cm.lower_parent_name, 
cu.email_address
FROM (
SELECT cu.user_name
FROM cwd_user AS cu
INNER JOIN cwd_membership AS cm ON cu.directory_id=cm.directory_id
AND cu.lower_user_name=cm.lower_child_name
AND cm.membership_type='GROUP_USER'
WHERE cm.lower_parent_name LIKE 'zz%'
GROUP BY LOWER(cu.user_name) ) u JOIN cwd_user t ON t.user_name = 
u.user_name ORDER BY t.user_name;

but with no luck. My SQL isn't that great, so I'm stuck here.

Any thoughts?

Upvotes: 0

Views: 100

Answers (3)

Marck
Marck

Reputation: 65

I ended up creating two seperate queries in a bash script which does the trick.

Upvotes: 0

ScaisEdge
ScaisEdge

Reputation: 133400

You sould select the proper value in the subselect and refer with the proper alias and you should use distinct not group by lower

SELECT  distinct 
        t.user_name
      , u.display_name
      , u.lower_parent_name
      , u.email_address
FROM (
  SELECT 
        cu.user_name
      , cu.display_name
      , cm.lower_parent_name
      , cu.email_address
  FROM cwd_user AS cu
  INNER JOIN cwd_membership AS cm  ON cu.directory_id=cm.directory_id
              AND cu.lower_user_name=cm.lower_child_name
              AND cm.membership_type='GROUP_USER'
  WHERE cm.lower_parent_name LIKE 'zz%'
) u 
JOIN cwd_user t ON t.user_name = u.user_name 
ORDER BY t.user_name;

and if you need only the lowercase user_name you can try binary lower

SELECT  distinct 
        t.user_name
      , u.display_name
      , u.lower_parent_name
      , u.email_address
FROM (
  SELECT 
        cu.user_name
      , cu.display_name
      , cm.lower_parent_name
      , cu.email_address
  FROM cwd_user AS cu
  INNER JOIN cwd_membership AS cm  ON cu.directory_id=cm.directory_id
              AND cu.lower_user_name=cm.lower_child_name
              AND cm.membership_type='GROUP_USER'
  WHERE cm.lower_parent_name LIKE 'zz%'
  AND BINARY cu.user_name = lower( cu.user_name)
) u 
JOIN cwd_user t ON t.user_name = u.user_name 
ORDER BY t.user_name;

Upvotes: 1

Saeed ur Rehman
Saeed ur Rehman

Reputation: 737

SELECT distinct (t.user_name), cu.display_name, cm.lower_parent_name, 
cu.email_address
FROM (
SELECT cu.user_name
FROM cwd_user AS cu
INNER JOIN cwd_membership AS cm ON cu.directory_id=cm.directory_id
AND cu.lower_user_name=cm.lower_child_name
AND cm.membership_type='GROUP_USER'
WHERE cm.lower_parent_name LIKE 'zz%'
GROUP BY LOWER(cu.user_name) ) u JOIN cwd_user t ON t.user_name = 
u.user_name ORDER BY t.user_name;

In SQL SERVER, DISTINCT keyword picks out only different data.

Upvotes: 0

Related Questions