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
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
I did search the forum before posting this and found some topics which were close to the same issue but I still had questions so am posting it here.
EMP_ID SEQ_NR NAME
874830 3 JOHN
874830 4 JOE
874830 21 MIKE
874830 22 BILL
874830 23 ROBERT
874830 24 STEVE
874830 25 JERRY
My output should look like this.
EMP ID SEQ3NAME SEQ4NAME SEQ21NAME SEQ22NAME SEQ23NAME SEQ24NAME SEQ25NAME
874830 JOHN JOE MIKE BILL ROBERT STEVE JERRY
SELECT A.EMP_ID
,A.NAME SEQ3NAME
,B.NAME SEQ4NAME
FROM AC_XXXX_CONTACT A
INNER JOIN AC_XXXX_CONTACT B ON A.EMP_ID = B.EMP_ID
WHERE A.SEQ_NR = '03' AND B.SEQ_NR = '04'
AND B.EMP_ID = '874830';
The above query helped me get the below results.
EMP_ID SEQ3NAME SEQ4NAME
874830 JOHN JOE
My question is to get all the fields(i.e till seq nr = 25) should I be joining the table 5 more times.
Is there a better way to get the results ?
I m querying against the Oracle DB
Thanks for your help.
New Requirement
New Input
STU-ID SEM CRS-NBR
12345 1 100
12345 1 110
12345 2 200
New Output
stu-id crs1 crs2
12345 100 200
12345 110
Not tested since you didn't provide test data (from table AC_XXXX):
(using Oracle 11 PIVOT clause)
select *
from ( select emp_id, seq_nr, name
from ac_xxxx
where emp_id = '874830' )
pivot ( max(name) for seq_nr in (3 as seq3name, 4 as seq4name, 21 as seq21name,
22 as seq22name, 23 as seq23name, 24 as seq24name, 25 as seq25name)
)
;
For Oracle 10 or earlier, pivoting was done "by hand", like so:
select max(emp_id) as emp_id, -- Corrected based on comment from OP
max(case when seq_nr = 3 then name end) as seq3name,
max(case when seq_nr = 4 then name end) as seq4name,
-- etc. (similar expressions for the other seq_nr)
from ac_xxxx
where emp_id = '874830'
;
Or, emp_id doesn't need to be within max() if we add group by emp_id - which then will work even without the WHERE clause, for a different but related question.
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
The variables in the data set are: Event, EventType, FName, LName, Age, Gender, Score.
I am trying to create a report that gives me the Lowest 5 scores per event/event type per each Age(18-65) per Gender.
For example, I want the 5 lowest scores for everyone who participated in EventA EventtypeB who were 18 year old females then I want all the 19 year old females and so on.. For each gender. A side note- Not all ages have 5 participants.. For example there may be no 20 year olds who participated and there may only be 2 21 year olds.
I originally tried to tackle this by making a bunch of separate data sets for each age, but I know there must be a better way to do it. I would appreciate any help thank you I am pretty new to SAS but I have introductory experience in all aspects.
Here is some sample input:
Mile Sprint John Smith 19 Male 15.31
Mile Sprint Alex Doe 19 Male 13.21
Mile Sprint Ian Sore 19 Male 23.51
Mile Sprint Sean Lae 19 Male 12.34
Mile Sprint Mike Rai 19 Male 17.27
Mile Sprint Connor Te 19 Male 11.23
Mile Sprint Simon Doe 19 Male 15.21
Mile Long Jane Joy 37 Female 35.12
Mile Long Victoria K 37 Female 27.31
Mile Long Chris Li 25 Male 23.43
For the Mile Sprint 19 Males I would want it to return:
Mile Sprint Connor Te 19 Male 11.23
Mile Sprint Sean Lae 19 Male 12.34
Mile Sprint Alex Doe 19 Male 13.21
Mile Sprint Simon Doe 19 Male 15.21
Mile Sprint John Smith 19 Male 15.31
For the Mile Long 37 Female I would want it to just return this due to there not being 5 participants:
Mile Long Victoria K 37 Female 27.31
Mile Long Jane Joy 37 Female 35.12
With the sample input shown I am trying to get the 5 lowest scores for Mile Sprint for Males age 19. Then the same for age 20-65. Then the same for Mile Long for all males. Vice versa for females. With the assumption that there may not be 5 participants in a race or there may be more than 5. Is there anyway to do all of this in one or two dataset outputs?
/* Creating Sample dataset */
data input_dataset;
infile datalines dlm=",";
input Event : $10.
EventType : $ 10.
FName : $10.
LName : $10.
Age : 8.
Gender : $10.
Score : 8.
;
datalines;
Mile,Sprint,John,Smith,19,Male,15.31
Mile,Sprint,Alex,Doe,19,Male,13.21
Mile,Sprint,Ian,Sore,19,Male,23.51
Mile,Sprint,Sean,Lae,19,Male,12.34
Mile,Sprint,Mike,Rai,19,Male,17.27
Mile,Sprint,Connor,Te,19,Male,11.23
Mile,Sprint,Simon,Doe,19,Male,15.21
Mile,Long,Jane,Joy,37,Female,35.12
Mile,Long,Victoria,K,37,Female,27.31
Mile,Long,Chris,Li,25,Male,23.43
;
run;
/* Sorting based on desired parameters - event EventType age Gender */
proc sort data = input_dataset;
by event EventType age Gender score;
run;
/*Picking the lowest five scores based on above parameters */
data input_dataset_1(drop=num);
set input_dataset;
retain num;
by event EventType age Gender score;
if first.gender then num=1 ; else num=num+1;
if num<=5;
run;