The value of column scores are delimited by comma. Each userid has at least a score and there is no upper limit for the number of scores of each userid.
+--------+---------+
| userid | scores |
+--------+---------+
| u1 | C,B,A |
| u2 | A |
| u3 | A,C |
+--------+---------+
I want to get the result by 'select ...' sql
+--------+---------+
| userid | score |
+--------+---------+
| u1 | C |
| u1 | B |
| u1 | A |
| u2 | A |
| u3 | A |
| u3 | C |
+--------+---------+
In hive, lateral view explode(split(score,',')) may realize the requirement,
select userid, score from my_table lateral view explode(split(scores,',')) scores as score;
However impala does not support explode.
Is there an alternative way to realize it in impala?
Related
The value of column scores are delimited by comma. Each userid has at least a score and there is no upper limit for the number of scores of each userid.
+--------+---------+
| userid | scores |
+--------+---------+
| u1 | C,B,A |
| u2 | A |
| u3 | A,C |
+--------+---------+
I want to get the result by 'select ...' sql
+--------+---------+
| userid | score |
+--------+---------+
| u1 | C |
| u1 | B |
| u1 | A |
| u2 | A |
| u3 | A |
| u3 | C |
+--------+---------+
In hive, lateral view explode(split(score,',')) may realize the requirement,
select userid, score from my_table lateral view explode(split(scores,',')) scores as score;
However impala does not support explode.
Is there an alternative way to realize it in impala?
I have a similar scenario to what's below. Is there a way to join the items audit table to the users table?
I would like the user Id of the user that last audited the items.
Users table:
| UserId | FirstName | LastName | Email |
|--------|-----------|----------|-----------------------|
| 00001 | Bob | Hackman | bob.hackman#test.org |
| 00002 | Peter | Slot | peter.slot#test.org |
ItemAudit Table:
| ItemId | LastAuditedBy |
|--------|---------------|
| abcd1 | Bob Hackman |
| qw341 | Peter Slot |
| w2re1 | Bob Hackman |
| fsdf1 | Bob Hackman |
| wetr1 | Peter Slot |
You should really have the id in the audit table, but you can use:
select ia.*, u.*
from itemAudit ia left join
users u
on concat(u.firstname, ' ', u.lastname) = ia.lastauditedby
tbl user
+--------+--------+
| UserID | Name |
+--------+--------+
| 1 | John |
| 2 | Anny |
| 3 | Andy |
| 4 | Tom |
+--------+--------+
tbl rol
+--------+--------+
| RolID | Name |
+--------+--------+
| 1 | Manager|
| 2 | Lead |
| 3 | Tester |
| 4 | Develop|
+--------+--------+
tbl user_rol
+--------+--------+
| UserID | RolID |
+--------+--------+
| 1 | 1 |
| 1 | 2 |
| 1 | 4 |
| 2 | 1 |
| 3 | 3 |
+--------+--------+
I have a List of parameters 1,2,4
SELECT u.UserID,Name
FROM [user] u
WHERE u.UserID in(
SELECT ur2.UserID
FROM user_rol ur2
WHERE ur2.ROlID IN(1,2,4)
GROUP BY ur2.UserID
HAVING COUNT(DISTINCT ur2.RolID)=3
)
The response will be:
+--------+--------+
| UserID | Name |
+--------+--------+
| 1 | John |
but if my parameters will be 1,2,3,4 will not work, but i need the response to be like previous
The problem is in count I did not know how much exists in the intermediate table user_rol.
It's hard to tell from you question, but it seems to me are looking to output any user that has more than one role listed in the user_rol table. Here is how you would do that:
SELECT user.userid, user.name
FROM user
INNER JOIN user_rol
ON user.userid = user_rol.userid
GROUP BY user.userid, user.name
HAVING COUNT(*) > 1
Tested here: http://sqlfiddle.com/#!9/35288d
I have table with data as follows
+----+------+
| id | code |
+----+------+
| 1 | M |
| 1 | Y |
| 2 | M |
| 2 | S |
| 3 | M |
| 3 | Q |
+----+------+
I would like to know if its possible to write a query that would return a list of codes that are unique to each ID? If there is no intersection the query should return no rows.
In the example above the only value common to all is M.
+----+------+
| id | code |
+----+------+
| 1 | M |
| 1 | S |
| 2 | M |
| 2 | S |
| 2 | H |
| 3 | M |
| 3 | S |
| 3 | Q |
+----+------+
The above would return M and S, common to all three ID's
Thanks
Try this:
SELECT code
FROM mytable
GROUP BY code
HAVING COUNT(*) = (SELECT COUNT(DISTINCT id) FROM mytable)
The above query assumes that code can appear only once per id.
I have two tables tags and users
Table Name: tags
| id | name |
| 1 | one |
| 2 | two |
| 3 | three |
| 4 | four |
| 5 | five |
Table Name: users
| id | fname | tags |
| 1 | Ram | {1,5} |
| 2 | Sham | {1,2,3,4} |
| 3 | Bham | {1,3} |
| 4 | Kam | {5,2} |
| 5 | Lam | {4,2} |
Expected output:
| id | fname | tags |
| 1 | Ram | one, five |
| 2 | Sham | one, two, three, four |
| 3 | Bham | one, three |
| 4 | Kam | five, two |
| 5 | Lam | four, two |
Trial-1 : using JOIN
SELECT I.id, I.fname, I.tags, J.name FROM users I
JOIN tags J ON J.id = ANY(I.cached_tag_ids)
LIMIT 1
Result:
| id | fname | tags |
| 1 | Ram | one |
| 1 | Ram | five |
Expected:
| id | fname | tags |
| 1 | Ram | one, five |
Your tags should have a INTEGER[] type.
CREATE TABLE users(
id SERIAL,
fname VARCHAR(50),
tags INTEGER[]
);
Then,
SELECT I.id, I.fname, array_agg(J.name)
FROM users I
LEFT JOIN tags J
ON J.id = ANY(I.tags)
GROUP BY fname,I.id ORDER BY id
should work. See sqlfiddle
This question may help.