Transpose data in Apache Pig Latin - hadoop

I need to "transpose" data that looks like this:
id City
111 Chicago
111 New York
111 LA
222 Paris
222 London
222 Tokyo
to:
111 Chicago New York LA
222 Paris London Tokyo
Every id would have three entries each, so the resulting relation would have 4 fields. I'm trying to avoid using a UDF. Any ideas?

Isn't this basic grouping?
B = GROUP A BY id
Check http://pig.apache.org/docs/r0.7.0/piglatin_ref2.html#GROUP

Related

How to categorize data in a ORACLE table based on a primary key?

I have a table TRAVEL with column TRIP_ID as primary key.
SOURCE DATA
TRIP_ID PERSON_NAME DESTINATION SOURCE TRANSACTION_ID TRIP_COUNT
100 Mike London Zurich 1000B112 1
101 Mike Paris Capetown 1000B112 1
102 Mike Moscow Madrid 1000B112 1
103 John Delhi Moscow 1100A110 1
104 John Toronto Zurich 1100A110 1
105 Mary Chennai Madrid 1100A111 1
106 Mary Berlin Zurich 1100A111 1
EXPECTED RESULTS:
when I do select * from TRAVEL where TRANSACTION_ID = 1100A111 it returns below two rows as below .
so I want my data to be categorized based on the transaction_ID on a run time.I dont want to hardcode the value for transaction-ID each time as above but i want to group it in such a way that it should fetch me the above expected results.I mean it should return all the data which are corresponding to the TransactionID in the table and it should not sum up the TRIP COUNT.It should return me the rows as below in my table.I am ok to create view .Please suggest
TRIP_ID PERSON_NAME DESTINATION SOURCE TRANSACTION_ID TRIP_COUNT
105 Mary Chennai Madrid 1100A111 1
106 Mary Berlin Zurich 1100A111 1
Can someone suggest a query in ORACLE to handle this ? I donot want to hardcode transaction ID
Regards
Sameer

How to show subjects name from subjects table depending on subjects codes in users table in laravel

I have students table with subject codes in the fields named like subj_one, subj_two, subj_three etc. There are another table named subjects with subject's codes and name.
Sample Records from students table
ID Name Subj_one Subj_two Subj_three
1 Mohan 101 102 107
2 Sohan 245 101 147
3 Neha 101 103 247
Sample Records from subjects table
subj_code subj_name
101 Hindi
102 English
103 Physics
147 Chemistry
245 Math
Now how to show subject name while I am showing student's data from students table?
In this case, firstly, create relation for each column. (not recommended though, but, since you mentioned that you want to achieve this without changing the database)
e.g for Subj_one, in the Student::class (model)
public function subjectOne() {
return this->belongsTo('App\Subject', 'Subj_one', 'subj_code'); // 'foreign_key', 'other_key'
}
Then, when you are querying for the students, use with().
$students = Student::with('subjectOne', 'subjectTwo')->get();
Later in your blade,
#foreach($students as $student)
{{ $student->Name }} with {{ $student->subjectOne->subj_name }}
#endforeach
Please go though Eloquent Relationships docs.
You have to create a relationship Many To Many.
Students
ID Name
1 Mohan
2 Sohan
3 Neha
Subjects
SubjectId SubjectName
101 Hindi
102 English
103 Physics
147 Chemistry
245 Maths
student_subject
Id Student_id Subject_id
1 1 101
2 1 102
3 1 103
4 2 147
5 2 245

How to convert the following source into the below target [duplicate]

This question already has answers here:
"pivot" table Oracle - how to change row items into columns
(2 answers)
Closed 5 years ago.
Student Name Subject Name Marks
Sam Maths 100
Tom Maths 80
Sam Physical Science 80
John Maths 75
Sam Life Science 70
John Life Science 100
John Physical Science 85
Tom Life Science 100
Tom Physical Science 85
We want to load our Target Table as:
Student Name Maths Life Science Physical Science
Sam 100 70 80
John 75 100 85
Tom 80 100 85
Try This.
SELECT student_name,
"Maths",
"Life Science",
"Physical Science"
FROM
(SELECT s.*
FROM Student s )
PIVOT (MAX(Marks)
FOR subject_name IN
('Maths' AS "Maths",'Life Science' AS "Life Science",'Physical Science' AS "Physical Science") )
ORDER BY 3;

How to get multiple rows into single row data in oracle sql?

Suppose in our Source Table we have data as given below:
Student Name Subject Name Marks
Sam Maths 100
Tom Maths 80
Sam Physical Science 80
John Maths 75
Sam Life Science 70
John Life Science 100
John Physical Science 85
Tom Life Science 100
Tom Physical Science 85
We want to load our Target Table as:
Student Name Maths Life Science Physical Science
Sam 100 70 80
John 75 100 85
Tom 80 100 85
Use the PIVOT operator:
SELECT *
FROM source
PIVOT ( MAX( marks ) FOR subject_name IN (
'Maths' AS Maths,
'Life Science' AS Life_Science,
'Physical Science' AS Physical_Science
) );
Output:
STUDENT_NAME MATHS LIFE_SCIENCE PHYSICAL_SCIENCE
------------ ----- ------------ ----------------
Sam 100 70 80
John 75 100 85
Tom 80 100 85

Aggregating the table from many rows to one

I have a table with data as follows:
Name opening receipt transfer closing
abcd 1000 40 30 1010
efg 256 109 219 146
hjk 9356 210 210 9356
mnp 2000
I need is a result like this -
opening_abcd receipt_abcd transfer_abcd closing_abcd opening_efg receipt_efg .....
1000 40 30 1010 256 109 .....
What I have tried till now is to create multiple views one by one having the one row values like -
select opening opening_abcd, receipt receipt_abcd, transfer transfer_abcd, closing closing_abcd
from this_table
where name = 'abcd'
and similarly for others and then I have merged them together by using JOIN.
Is there a solution possible if I can select all of them in one query or any other way which is better than mine?

Resources