Reputation: 60074
I want to set a column in the group by
table based on the first (actually, only) value in the group.
Specifically, given a table
id good
1 t
1 t
2 f
3 t
I want to produce the table
id multiplicity goodN
1 2 0
2 1 0
3 1 1
where goodN
is 1 if and only if multiplicity
is 1 and good
is t
:
select id, count(*) as multiplicity,
if (count(*) > 1, 0, if(good = 't', 1, 0)) as goodN
from ...
The question is: how do I extract the first (in my case, only) value of good
from the group?
PS. is there a cheaper way to test that the group has size 1 than count(*)=1
?
Upvotes: 1
Views: 122
Reputation: 562931
If the count is 1, then both the MAX(good) and MIN(good) will be the "first" row in the group.
select id, count(*) as multiplicity,
if (count(*) > 1, 0, if(max(good) = 't', 1, 0)) as goodN
from ...
Upvotes: 1
Reputation: 559
Select good from... Where id=(select min(id) from... Where good >0)
And
Group by id having count(id)=1
You can test count(goods) instead of count(*) and at the end add group by id. To take only record with moltiplicity 1 add having count(goods)=1
Upvotes: 0
Reputation: 15329
I don't have experience with hiveql, but this does what you want in mySql. I think should work the same in hiveql.
select t.id, count(*) as multiplicity,
(select case count(*) when 1 then 1 else 0 end
from table1
where id = t.id
and good = 't') as goodN
from table1 t
group by t.id
Upvotes: 0
Reputation: 3071
If it were MySQL then simply:
SELECT id,
good,
count(*) AS multiplicity,
if((count(*) > 1 AND good = 't'), @flag := 1, @flag := 0) AS goodN
FROM goods
GROUP BY good, id;
PS: Let me know if you want me to delete my answer.
Upvotes: 0