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

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

Related

Counting occurences in a column within SQL Oracle

This is what I have at the moment in terms of data:
Customer_id
Choco_type
100
Milk
544
Dark
74
White
100
White
544
Milk
82
Crisp
544
White
544
White
What I am looking to achieve is to find out for example is this:
Customer_id
Choco_type
Count
100
Milk
2
544
Dark
1
74
White
1
544
Milk
1
82
Crisp
1
544
White
2
Use GROUP BY to aggregate data. In this case you want a count, so...
select id
,chocolate_type
,count(*)
from your_table
group by id
,chocolate_type
/
If you just want to know how many different types of chocolate each person has bought you would do this:
select id
,count(distinct chocolate_type)
from your_table
group by id
/
You haven't posted your table structure, so I have made some assumptions. You will need to adjust my code examples to fit your actual situation.

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;

Tables Summary into single table in laravel

I have two tables named 'Customers' and 'Committeetables' in mysql database. They have following columns
Table: Customer
nd Name FatherName Cell# AdvanceAmount
25 Waseem Asghar 0302 2500
30 Ramzan Khan 0303 3500
35 Rana Ali 0307 2000
Table: CommitteeTables
nd Amount RecievingDate DrawDate
25 1500 2-10-15 10-10-15
30 1500 2-10-15 10-10-15
25 1500 3-10-15 10-11-15
30 1500 3-10-15 10-11-15
35 1500 3-10-15 10-11-15
A Column 'nd' in 'Customers' table is primary key and in 'CommitteeTables' table is foreign key. It means 'Customers' table joins with 'ComitteeTables' table through 'nd' column.
Expected Result table should be as under:
nd Name Father Name Cell# Advance Amount Balance
25 Waseem Asghar 0302 25000 3000 22000
30 Ramzan Khan 0303 35000 3000 32000
32 Rana Ali 0307 20000 1500 18500
----------------------------------------------------------------------------
total 80000 7500 72500
I am using Laravel 5 and HTML, Bootstrap. I need this result in Laravel 5. I shall be really thankful if someone help me to solve my problem.
Thanks
Waseem
This is not at all a Laravel question. It's a general SQL question.
Something like this:
SELECT nd, Name, FatherName, Cell#, AdvanceAmount as Advance,
(SELECT SUM(Amount) FROM CommitteeTables WHERE CommitteeTables.nd=Custoemr.nd) as Amount, Advance-Balance AS Balance
FROM Customer;
I'm not entirely sure that's correct but you can try it and fix any errors. As Bogdan suggests you should try something first then come back here with any problems that you have.

Transpose data in Apache Pig Latin

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

Resources