Solved

Hive / SQL select last item from values separated by commas

Posted on 2013-10-30
5
1,281 Views
Last Modified: 2013-11-04
I've got a field that holds values separated by commas.  How can i get the *LAST* value from the list?

---------------------------
|    Example_Field   |
---------------------------
[apple, orange, pear]
[apple, pear]    
[pear, pear, apple]
[orange]
---------------------------

I want a select query to return the last value from each list:
pear
pear
apple
orange

So:
Select SpecialFunctionToGetLastVal(Example_Field) as LastFruit from Table

Open in new window


This is for a hive query, but i'll cross that bridge when i come to it.  Is there some generic function or method in SQL to do this?

Thanks in advance
0
Comment
Question by:ducky801
5 Comments
 
LVL 40

Accepted Solution

by:
Kyle Abrahams earned 500 total points
ID: 39612513
this should do it for you:

select substring(
                          Example_Field,
                                    len(Example_Field) - charindex(',', reverse(Example_Field)) + 2,
                                    charindex(',', reverse(Example_Field)))


note that if your values are seperated by the ', ' then you would use a ' ' instead of ',' in the query (2 replacements).
0
 
LVL 28

Expert Comment

by:sammySeltzer
ID: 39612516
Sorry this is wrong. - my first solution  that is.

How about this?

DECLARE @LastVAR NVARCHAR(100)
DECLARE MYTESTCURSOR CURSOR
DYNAMIC 
FOR
SELECT Example_Field as lastItem FROM yourtable
OPEN MYTESTCURSOR
FETCH LAST FROM MYTESTCURSOR INTO @LastVAR 
CLOSE MYTESTCURSOR
DEALLOCATE MYTESTCURSOR
SELECT @LastVAR 

Open in new window


OR

set rowcount 1
 
select * from yourtable order by id desc

Open in new window

0
 
LVL 32

Expert Comment

by:awking00
ID: 39614755
I'm not very familiar with Hive, but I do know it has string functions for reverse(), instr(), trim(), replace(), and substr() from which you could create a user_defined function. In pseudo code, something like the following:
if instr(field,',') = 0 then field
else
reverse(field)
find first position of reversed field using instr(reverse(field),',')
substr(reverse(field),1, instr(reverse(field),',')
replace the ',' from above, then trim the result
and finally reverse that.
Sorry I couldn't be more specific but I hope this might help.
0
 
LVL 40

Expert Comment

by:Sharath
ID: 39618478
Is it a STRING or ARRAY data type? And do you have open and closing brackets also as part of the string?
0
 
LVL 40

Expert Comment

by:Sharath
ID: 39618482
try this.

SELECT reverse(sentences(reverse(your_column))[0][0]) FROM your_table

tested on my machine and it returned the last element.

select reverse(sentences(reverse('abc,def,ghi'))[0][0]) from default.dual
0

Featured Post

Master Your Team's Linux and Cloud Stack!

The average business loses $13.5M per year to ineffective training (per 1,000 employees). Keep ahead of the competition and combine in-person quality with online cost and flexibility by training with Linux Academy.

Question has a verified solution.

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

Suggested Solutions

Title # Comments Views Activity
Query group by data in SQL Server - cursor? 3 49
Format Data Field - SQL 11 37
How to count the days a record spends in a step 21 51
SQL - Simple Pivot query 8 15
As they say in love and is true in SQL: you can sum some Data some of the time, but you can't always aggregate all Data all the time! Introduction: By the end of this Article it is my intention to bring the meaning and value of the above quote to…
Occasionally there is a need to clean table columns, especially if you have inherited legacy data. There are obviously many ways to accomplish that, including elaborate UPDATE queries with anywhere from one to numerous REPLACE functions (even within…
Two types of users will appreciate AOMEI Backupper Pro: 1 - Those with PCIe drives (and haven't found cloning software that works on them). 2 - Those who want a fast clone of their boot drive (no re-boots needed) and it can clone your drive wh…
This video shows how to use Hyena, from SystemTools Software, to bulk import 100 user accounts from an external text file. View in 1080p for best video quality.

827 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