sql from row to columns - sql

Please help me out with the following issue. want to convert row data into the columns based on value of "PeriodNum" column max column will up to period8... there are multiple teachers in the table...below is just sample of 2 teachers.
Sample of data:
id teachername course_name periodnum courseid
------------------------------------------------------------------
14088 Smith, John 1; Physical Education 9 GYM ABCD 1 10064
14088 Smith, John 2; Physical Education 9 GYM ABCD 2 10064
14088 Smith, John 3; Physical Education 11 GYM BD 3 10065
14088 Smith, John 5; Physical Education 11 GYM AC 5 10065
14088 Smith, John 6; Physical Education 11 GYM ABCD 6 10065
14088 Smith, John 7; Health 9 P373 ABCD 7 10059
14088 Smith, John 8; Physical Education 11 GYM AC 8 10065
14088 Smith, John 8; Health 10 GYM BD 8 10066
15411 Yong, Jerry 1; Science 6 ABCD 1 10078
15411 Yong, Jerry 2; Science 9 ABCD 2 10078
15411 Yong, Jerry 3; Science 11 BD 3 10078
15411 Yong, Jerry 5; Science 11 AC 5 10078
15411 Yong, Jerry 6; Science 11 ABCD 6 10078
15411 Yong, Jerry 7; Maths P373 ABCD 7 10080
15411 Yong, Jerry 8; Biology 11 AC 8 10041
I want to have desired result to look like following:
id teachername Period1 Period2 Period3 Period4 Period5 Period6 Period7 Period8
14088 Smith, John 1; Physical Education 9 GYM ABCD 2; Physical Education 9 GYM ABCD 3; Physical Education 11 GYM BD NULL 5; Physical Education 11 GYM AC 6; Physical Education 11 GYM ABCD 7; Health 9 P373 ABCD 8; Physical Education 11 GYM AC + 8; Health 10 GYM BD
15411 Yong, Jerry 1; Science 6 ABCD 2; Science 9 ABCD 3; Science 11 BD 5; Science 11 AC 6; Science 11 ABCD 7; Maths P373 ABCD 8; Biology 11 AC
thanks...so much.
I've tried following query
SELECT DISTINCT
t.[id],
t.[teachername]
,t1.course_name AS period1,t2.course_name AS period2,t3.course_name AS period3, t4.course_name AS period4, t5.course_name AS period5, t6.course_name AS period6,
t7.course_name AS period7,t8.course_name AS period8 -- into ccs_test
FROM #teacher t
LEFT OUTER JOIN CompanyGrouped t1 ON t.[id]=t1.[id] AND t1.ColumnNumber=1 and t.periodnum=1
LEFT OUTER JOIN CompanyGrouped t2 ON t.[id]=t2.[id] AND t2.ColumnNumber=2 and t.periodnum=2
LEFT OUTER JOIN CompanyGrouped t3 ON t.[id]=t3.[id] AND t3.ColumnNumber=3 and t.periodnum=3
LEFT OUTER JOIN CompanyGrouped t4 ON t.[id]=t4.[id] AND t4.ColumnNumber=4 and t.periodnum=4
LEFT OUTER JOIN CompanyGrouped t5 ON t.[id]=t5.[id] AND t5.ColumnNumber=5 and t.periodnum=5
LEFT OUTER JOIN CompanyGrouped t6 ON t.[id]=t6.[id] AND t6.ColumnNumber=6 and t.periodnum=6
LEFT OUTER JOIN CompanyGrouped t7 ON t.[id]=t7.[id] AND t7.ColumnNumber=7 and t.periodnum=7
LEFT OUTER JOIN CompanyGrouped t8 ON t.[id]=t8.[id] AND t7.ColumnNumber=8 and t.periodnum=8
Regards,
HT

I brought the data into a table in SQL Server and was able to pivot to the results you were looking for.
SELECT ID
, TeacherName
,[1] as Period1
,[2] as Period2
,[3] as Period3
,[4] as Period4
,[5] as Period5
,[6] as Period6
,[7] as Period7
,[8] as Period8
FROM
(SELECT ID, TeacherName, peridonum, course_name
FROM teacher) src
PIVOT
(MAX(course_name) FOR peridonum in ([1],[2],[3],[4],[5],[6],[7],[8])
) pvt

Related

PostgreSQL: Return entries based on conditions and grouping

Imagine the following table:
result_id student subject grade
1 Anne A 63
2 Anne B 63
3 Bob A 89
4 Bob B 51
5 Carla A 70
6 Carla B 70
7 Carla C 68
8 Dylan B 75
9 Dylan C 54
I would like to return the entries where a student got the exact same grade for subject A and B. So the ideal output would be:
result_id student subject grade
1 Anne A 63
2 Anne B 63
5 Carla A 70
6 Carla B 70
Can this even be achieved with queries? Struggling to find information about it.
A bit complicated but I hope easy to read.
select * from the_table
where student in
(
with t as
(
select student, count(*) cnt, min(grade) mig, max(grade) mag
from the_table
where subject in ('A', 'B') group by student
)
select student from t where mig = mag and cnt = 2
)
and subject in ('A', 'B')
order by student, subject;
result_id
student
subject
grade
1
Anne
A
63
2
Anne
B
63
5
Carla
A
70
6
Carla
B
70

SQL Distinct pairs matched on array of strings

This is a simplified version of the tables but I have a table called NAME with 2 columns, names (strings) and classes (strings)
index
name
classes
0
Joe
Mat 12
1
Hector
Mat 12
2
Terry
Arc 13
3
Elizabeth
Soc 7
4
Wyatt
Arc 13
5
Alex
His 9
6
James
Mat 12
7
Rebecca
Soc 7
NOTE: All names and classes actually have a numerical ID associated with them, used actual names for illustration.
I want to pair up unique people taking the same class, with the output table looking something like this.
name1
name2
class
Joe
Hector
Mat 12
Joe
James
Mat 12
Hector
James
Mat 12
Terry
Wyatt
Arc 12
Worth noting is that the output table should have unique pairs and a person shouldn't be paired with themselves (so don't want outcome like this)
name1
name2
class
Joe
Hector
Mat 12
Hector
Joe
Mat 12
Joe
Joe
Mat 12
I've tried something along the lines of
SELECT NAME.names as name1, NAME.names as name2, NAME.classes as c1, COUNT(*) as pa
FROM NAME
JOIN NAME
ON name1.c1 = name2.c1
GROUP BY name1, name2
ORDER BY c1
But doesn't work.
You may achieve this using a join and comparison based on the index/name
Query #1
SELECT
n1.name as name1,
n2.name as name2,
n1.classes
FROM
NAME n1
INNER JOIN
NAME n2 ON n1.classes=n2.classes AND n1.index < n2.index;
name1
classes
name2
Joe
Mat 12
Hector
Terry
Arc 13
Wyatt
Joe
Mat 12
James
Hector
Mat 12
James
Elizabeth
Soc 7
Rebecca
Query #2
SELECT
n1.name as name1,
n2.name as name2,
n1.classes
FROM
NAME n1
INNER JOIN
NAME n2 ON n1.classes=n2.classes AND n1.name < n2.name;
name1
classes
name2
Hector
Mat 12
Joe
James
Mat 12
Joe
Terry
Arc 13
Wyatt
Hector
Mat 12
James
Elizabeth
Soc 7
Rebecca
View on DB Fiddle
Let me know if this works for you.

How to join different table some column?

Student Table 1
ID Name Surname School Number Class Number ClassBranch
-----------------------------------------------------------------------------
113 Jane Smith 19 4 A
121 John Konl 42 5 B
331 Albert Smith 61 4 A
742 Jack Ronal 52 5 B
759 Jan Ronal 84 6 C
Student Table 2
ID Name Surname School Number Class Number Class Branch
-----------------------------------------------------------------------------
113 Jane Smith 11 4 D
151 John Konl 18 4 D
804 Albert Smith 26 5 F
605 Jack Ronal 32 5 F
785 Jan Ronal 87 8 L
Created Student Table
ID Name Surname School Number Class Number Class Branch
--------------------------------------------------------------------
113 Jane Smith NULL NULL NULL
151 John Konl NULL NULL NULL
804 Albert Smith NULL NULL NULL
605 Jack Ronal NULL NULL NULL
NULL NULL NULL 11 4 D
NULL NULL NULL 18 4 D
NULL NULL NULL 26 5 F
NULL NULL NULL 32 5 F
I want this table
ID Name Surname School Number Class Number Class Branch
113 Jane Smith 11 4 D
151 John Konl 18 4 D
804 Albert Smith 26 5 F
605 Jack Ronal 32 5 F
I want to Student Table 1 ---> ID,Name,Surname and Student Table 2 --> School Number,Class Number and ClassBranch joın.But joın is not successful.Class Branch A and B removed and D,F is adding.
First table ID,Name,Surname (3 columns) and Second table School Number,Class Number,Class Branch join.
Where conditions:
Removed column --> 4 - A AND 5 - B
Adding column --> 4- D AND 5- F
How can I write query?
Now that you've completely changed the original tables, this should be a simple JOIN.
SELECT t2.id, t1.name, t1.surname, t2.SchoolNumber, t2.ClassNumber, t2.ClassBranch
FROM Student1 AS t1
JOIN Student2 AS t2 ON t1.name = t2.name AND t1.surname = t2.surname
DEMO
To get those results?
1) The complicated way. By joining them.
SELECT
s2.ID,
s1.Name, s1.Surname,
s2."School Number", s2."Class Number", s2.ClassBranch
FROM Student1 AS s1
JOIN Student2 AS s2 ON (s2.Name = s1.Name AND s2.Surname = s1.Surname)
WHERE s1."Class Number" IN (4, 5);
2) The simple way, select only from the 2nd table
SELECT *
FROM Student2
WHERE "Class Number" IN (4, 5);
Test on SQL Fiddle here

Using multiple inner joins and sorting result

In my DB there are three tables. I am trying to figure out how to List the school name, class name and teacher's name. Sort the records by class name within school name. I have a query but it is not working. How can I list the school name, class name and teacher's name and sort record by class name within school name?
SELECT c.class_name FROM class c INNER JOIN teacher t WHERE t.teacher_id = c.teacher_id INNER JOIN school s WHERE t.school_id = s.school_id;
TABLES
SQL> select * from class;
CLASS_ID CLASS_NAME TEACHER_ID MAX_SEATS_AVAILABLE
---------- ------------------- ---------- -------------------
1 Intro to ALGEBRA 11 12
2 Basic CALCULUS 2 10
3 ABC and 123 1 15
4 Sharing 101 8 10
5 Good Talk, Bad Talk 9 20
6 Nap Time 1 21
7 WRITing 101 5 10
8 Finger Painting 9 14
9 Physics 230 2 20
10 Gym 5 25
10 rows selected.
SQL> select * from teacher;
TEACHER_ID FIRST_NAME LAST_NAME T HOME_ROOM_NUM PHONE_NUM START_DAT HO SCHOOL_ID
---------- ---------------- ---------------- - ------------- ---------- --------- -- ----------
1 FRanK JOHNSON k 10C 22-OCT-97 In 11090
2 LISA JONES h 11Bc 317-587-90 19-JAN-15 iN 123134
87
3 Jeff Dafferty C W8CZ 12-DEC-96 OH 11546
4 Frank MARTIN g 12aA 212-098-98 19-JAN-15 IN 11090
76
5 John Smith H 34C 10-OCT-93 In 123134
6 John Smith G 34C 10-OCT-93 in 11090
7 Lisa Jones G 11E 317-587-90 19-JAN-15 IN 123134
87
8 Trevor Horse k x 19-JAN-15 Oh 11090
9 Gregor Ivan K 12A 317-987-09 10-NOV-96 KY 11090
87
10 Gregor Ivan g 12A 317-987-09 10-NOV-96 Ky 11090
87
11 Pat Francis H 1z1a 123-317-09 19-JAN-15 Il 11546
12
12 Brad Smith G 13A 18-NOV-94 IN 11546
12 rows selected.
SQL> select * from school;
SCHOOL_ID SCHOOL_NAME SCHOOL_TYPE
---------- ----------------------------- ------------
11546 Ivy Tech College COLLegE
11090 LAWRENCE Central Grade School GRADE SCHOOL
11111 Lawrence NORTH High School HIGH SCHooL
19283 Howe High SCHOOL High SchooL
123134 Lawrence Central High School HIGH SCHOOL
192 Little Big Horn Grade School GRADE SCHOOL
You can use the join to get what you want. This will give the all schools with the teachers and there classes.
SELECT DISTINCT
sc.SCHOOL_NAME,
teach.FIRST_NAME,
teach.LAST_NAME ,
cs.CLASS_NAME
FROM
school sc JOIN teacher teach
ON sc.SCHOOL_ID = teach.SCHOOL_ID
JOIN class cs ON cs.TEACHER_ID = teach.TEACHER_ID
ORDER BY
sc.SCHOOL_NAME,cs.CLASS_NAME
SELECT t.FIRST_NAME +' '+t.LAST_NAME, s.SCHOOL_NAME, c.class_name
FROM class c
INNER JOIN teacher t
ON t.teacher_id = c.teacher_id
INNER JOIN school s
ON t.school_id = s.school_id
ORDER BY s.SCHOOL_NAME,c.CLASS_NAME

SQL Server: Merge Data Rows in single table in output

I have a SQL Server table with the following fields and sample data:
ID Name Address Age
23052-PF Peter Timbuktu 25
23052-D1 Jane Paris 22
23052-D2 David London 24
23050-PF Sam Beijing 22
23051-PF Nancy NYC 26
23051-D1 Carson Cali 22
23056-PF Grace LA 28
23056-D1 Smith Boston 23
23056-D2 Mark Adelaide 26
23056-D3 Hose Mexico 25
23056-D4 Mandy Victoria 24
Each ID with -PF is unique in the table.
Each ID with the -Dx is related to the same ID with the -PF.
Each ID with -PF may have 0 or more IDs with -Dx.
The maximum number of -Dx rows for a given -PF is 9.
i.e. an ID 11111-PF can have 11111-D1, 11111-D2, 11111-D3 up to 11111-D9.
Output expected for above sample data:
ID ID (without suffix) PF_Name PF_Address PF_Age D_Name D_Address D_Age
23052-PF 23052 Peter Timbuktu 25 Jane Paris 22
23052-PF 23052 Peter Timbuktu 25 David London 24
23050-PF 23050 Sam Beijing 22 NULL NULL NULL
23051-PF 23051 Nancy NYC 26 Carson Cali 22
23056-PF 23056 Grace LA 28 Smith Boston 23
23056-PF 23056 Grace LA 28 Mark Adelaide 26
23056-PF 23056 Grace LA 28 Hose Mexico 25
23056-PF 23056 Grace LA 28 Mandy Victoria 24
I need to be able to join the -PF and -Dx as above.
If a -PF has 0 Dx rows, then D_Name, D_Address and D_Age columns in the output should return NULL.
If a -PF has one or more Dx rows, then PF_Name, PF_Address and PF_Age should repeat for each row in the output and D_Name, D_Address and D_Age should contain the values from each related Dx row.
Need to use MSSQL.
Query should not use views or create additional tables.
Thanks for all your help!
select
pf.ID,
pf.IDNum,
pf.Name as PF_Name,
pf.Address as PF_Address,
pf.Age as PF_Age,
dx.Name as D_Name,
dx.Address as D_Address,
dx.Age as D_Age
from
(
select
ID, left(ID, 5) as IDNum, Name, Address, Age
from
mytable
where
right(ID, 3) = '-PF'
) pf
left outer join
(
select
ID, left(ID, 5) as IDNum, Name, Address, Age
from
mytable
where
right(ID, 3) != '-PF'
) dx
on pf.IDNum = dx.IDNum
SqlFiddle demo: http://sqlfiddle.com/#!6/dfdbb/1
SELECT t1.ID, LEFT(t1.ID,5) "ID (without Suffix)",
t1.Name "PF_Name", t1.Address "PF_Address", t1.Age "PF_Age",
t2.Name "D_Name", t2.Address "D_Address", t2.Age "D_Age"
FROM PFTable t1
LEFT JOIN PFTable t2 on LEFT(t1.ID,5) = LEFT(t2.ID,5)
WHERE RIGHT(t1.ID,2) = 'PF'