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

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

Related

Replace empty column by some string in Oracle Materialized view

I have to create a materialized view based on data:-
ID Class_Code Student_Name
1 1011 Jatin
2 1012 Pual
3 Patrick
4 1014 Liaba
5 Noah
and i want a materialized view to return
ID Class_Code Student_Name
1 1011 Jatin
2 1012 Pual
3 Not Enrolled Patrick
4 1014 Liaba
5 Not Enrolled Noah
Yes i did some research on google and did not get any thing. CASE WHEN statements return weird results. Please help.
I dont think this is much complexed. You may try this below simple query -
SELECT ID, NVL(Class_Code, 'Not Enrolled') Class_Code, Student_Name
FROM YOUR_MV;
You can try to use the NVL:
NVL(class_code, 'Not Enrolled')
This way, whenever the value for the column CLASS_CODE is NULL, it will populate the void with a "Not Enrolled".

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 sum two column from different Table in H2 Database

I have two tables in database called purchase AND sales
Tables as follow
Table purchases
----------------------------
ID PRODUCT QTY
----------------------------
1 APPLE 100
2 BANANA 200
3 GRAPES 150
4 AVACADO 110
Table Sales
----------------------------
ID PRODUCT QTY
----------------------------
1 APPLE 50
2 BANANA 200
3 GRAPES 100
Here in the above tables i have purchase and sales i want to do subtraction from purchases quantity with sales quantity
and get the output like this
Output
----------------------------
ID PRODUCT QTY
----------------------------
1 APPLE 50
2 BANANA 0
3 GRAPES 50
4 AVACADO 110
i written query but not working
select id, product, (purchases.qty)-(sales.qty) from purchases left join sales on purchases.id=sales.id
Solution
SELECT purchases.id,purchases.product, (purchases.qty - IF (sales.qty IS NULL, 0, sales.qty)) as qty FROM pruchases LEFT JOIN sales ON purchases.product = sales.product
See all three Images your problem may be solved.
SALES TABLE [Same as Sales Table]1
PURCHASE TABLE [Same as Purchase Table]2
RESULTANT IMAGE [Resultant Image With Query]3

How to run SQL query group by issue?

I have a table with columns :
Table Name: IdentityTable
ID Dest_Name Dest_Reference_Id Format IG
31231231 India Delhi XKHYGUI 21
12313131 USA Washington XHKOWKG 1
34645542 India Mumbai XKLWOFH 1
31231314 USA California XLGJDJG 21
31234531 India Delhi XKHIHUI 21
12375671 USA Washington XHKLHKG 21
12574613 USA Washington XLKWMKG 1
and so on...
I want to query this table to retrieve information in this form:
Dest_Name Dest_Reference_Id Total_Format Format IG
India Delhi 2 XKHYGUI 21
India Delhi 2 XKHIHUI 21
USA Washington 3 XHKOWKG 1
USA Washington 3 XHKLHKG 21
USA Washington 3 XLKWMKG 1
India Mumbai 1 XKLWOFH 1
USA California 1 XLGJDJG 21
I did:
select dest_name, dest_reference_id, count (format)
from IdentityTable
Group By dest_name, dest_reference_id;
I could retrieve all information required except Format column in the result. How should I modify my query to return expected result ?
Make two sub-queries and join em up.
SELECT counts.dest_name,
counts.dest_reference_id,
counts.total_format,
idents.format,
idents.IG
FROM
(
select dest_name, dest_reference_id, count (format) as total_format
from IdentityTable
Group By dest_name, dest_reference_id;
) counts
join
(
select distinct dest_name, dest_reference_id, format, IG
from identitytable
) idents
on counts.dest_name = idents.dest_name
and counts.dest_reference_id = idents.dest_reference_id
count(*) over(partition by dest_name, dest_reference_id)
and without a group by. google "oracle analytic functions".

Perl JOIN-like behavior in Oracle?

I have two tables, let's call them PERSON and NAME.
PERSON
person_id
dob
NAME
name_id
person_id
name
And let's say that the NAME table has data like:
name_id person_id name
1 1 Joe
2 1 Fred
3 1 Sam
4 2 Jane
5 2 Kim
I need a query (Oracle 10g) that will return
name_id names
1 Joe, Fred, Sam
2 Jane, Kim
Is there a simple way to do this?
Update:
According to the article that figs was kind enough to provide, starting in 9i you can do:
SELECT wmsys.wm_concat(dname) departments FROM dept;
For this example, the answer becomes:
SELECT name_id, wmsys.wm_concat(name) from names group by name_id
The short answer is to use a PL/SQL function. For more details, have a look in this post.

Resources