I'm relatively new to SQL. I currently have the following CoursesTbl
StudentName CourseID InstructorName
Harry Potter 180 John Wayne
Harry Potter 181 Tiffany Williams
John Williams 180 Robert Smith
John Williams 181 Bob Adams
Now what I really want is this:
StudentName Course1(180) Course2(181)
Harry Potter John Wayne Tiffany Williams
John Williams Robert Smith Bob Adams
I've tried this query:
Select StudentName, Min(InstructorName) as Course1, Max(InstructorName) as
Course2 from CoursesTbl
Group By StudentName
Now it's clear to me that I need to group by the Student Name. But using Min and Max messes up the instructor order.
i.e. Min for Harry is John Wayne and Max is Tiffany Williams
Min for John Williams is Bob Adams and Max is Robert Smith.
So it does not display instructors in the correct order.
Can anyone please suggest how this could be fixed?
You can use conditional aggregation with a CASE statement along with an aggregate function to PIVOT the data into columns:
select
[StudentName],
Course1 = max(case when CourseId = 180 then InstructorName end),
Course2 = max(case when CourseId = 181 then InstructorName end)
from #Table1
group by StudentName
See Demo. You could also use the PIVOT function to get the result:
select
StudentName,
Course1 = [180],
Course2 = [181]
from
(
select StudentName,
CourseId,
InstructorName
from #Table1
) d
pivot
(
max(InstructorName)
for CourseId in ([180], [181])
) piv
Another Demo.
Related
I'm looking to add some form of count function to my table, but am not quite sure how to do it. The table I have is:
First name
Surname
Tom
James
Mike
James
Tom
James
Mike
Hamilton
William
Morris
Mike
James
Mike
James
I would like it to have a count, of the full names that come up twice or more, like so:
First name
Surname
Count
Tom
James
1
Mike
James
1
Tom
James
2
Mike
Hamilton
1
William
Morris
1
Mike
James
2
Mike
James
3
What is the best way to go about this in SQL? Unfortunately I need the result as per the table above, rather than simply:
First name
Surname
Count
Tom
James
2
Mike
James
3
Mike
Hamilton
1
William
Morris
1
row_number function should work
select *,
row_number() over(partition by [first name],surname order by [first name]) as count
from table_name
I believe using group by on multiple columns would be a more appropriate approach to
select [first name], surname, COUNT(*) from Employee Group BY [first name], surname;
I believe using group by on multiple columns would be a more appropriate approach to
select [first name], surname, COUNT(*) from Employee Group BY [first name], surname;
Result:
I'm trying to do with following with an SQL query in Impala. I've got a single data table that has (among other things) two columns with values that intersect multiple times. For example, let's say we have a table with two columns for related names and phone numbers:
Names Phone Numbers
John Smith (123) 456-7890
Rob Johnson (123) 456-7890
Greg Jackson (123) 456-7890
Tom Green (123) 456-7890
Jack Mathis (123) 456-7890
John Smith (234) 567-8901
Rob Johnson (234) 567-8901
Joe Wolf (234) 567-8901
Mike Thomas (234) 567-8901
Jim Moore (234) 567-8901
John Smith (345) 678-9012
Rob Johnson (345) 678-9012
Toby Ellis (345) 678-9012
Sam Wharton (345) 678-9012
Bob Thompson (345) 678-9012
John Smith (456) 789-0123
Rob Johnson (456) 789-0123
Kelly Howe (456) 789-0123
Hank Rehms (456) 789-0123
Jim Fellows (456) 789-0123
What I need to get from this table is a selection of each item from the Name column that has multiple entries from the Phone Numbers column associated with it, like this:
Names Phone Numbers
John Smith (123) 456-7890
John Smith (234) 567-8901
John Smith (345) 678-9012
John Smith (456) 789-0123
Rob Johnson (123) 456-7890
Rob Johnson (234) 567-8901
Rob Johnson (345) 678-9012
Rob Johnson (456) 789-0123
This is the query I've got so far, but it's not quite giving me the results I'm looking for:
SELECT a.name, a.phone_number, b.phone_number, b.count1
FROM databasename a
INNER JOIN (
SELECT phone_number, COUNT(phone_number) as count1
FROM databasename
GROUP BY phone_number
) b
ON a.phone_number = b.phone_number;
Any ideas on how to improve my query to get the results I'm looking for?
Thank you.
Working with your query...
This generates a subset by name of users having more than 1 phone number it then joins back to the entire set based on name returning all phone numbers for users having more than 1 phone number. however if a user has the same phone number listed more than once it would get returned. to eliminate those if needed, add distinct to the count in the inline view.
SELECT a.name, a.phone_number
FROM databasename a
INNER JOIN (
SELECT name, COUNT(phone_number) as count1
FROM databasename
GROUP BY name
having COUNT(phone_number) > 1
) b
on a.name = b.name
Order by a.name, a.phone_Number
One method is to use exists:
select t.*
from tablename t
where exists (select 1 from tablename t2 where t2.name = t.name and t2.phonenumber <> t.phonenumber)
SELECT DISTINCT x.*
FROM my_table x
JOIN my_table y
ON y.name = x.name
AND y.phone <> x.phone;
My first time reading a question on here.
I am working at a university and I have a table of student IDs and their supervisors, some of the students have one supervisor and some have two or three depending on their subject.
The table looks like this
ID Supervisor
1 John Doe
2 Peter Jones
2 Sarah Jones
3 Peter Jones
3 Sarah Jones
4 Stephen Davies
4 Peter Jones
4 Sarah Jones
5 John Doe
I want to create a view that turns that into this:
ID Supervisor 1 Supervisor 2 Supervisor 3
1 John Doe
2 Peter Jones Sarah Jones
3 Peter Jones Sarah Jones
4 Stephen Davies Peter Jones Sarah Jones
5 John Doe
I have looked at PIVOT functions, but don't think it matches my needs.
Any help is greatly appreciated.
PIVOT was the right clue, it only needs a little 'extra' :)
DECLARE #tt TABLE (ID INT,Supervisor VARCHAR(128));
INSERT INTO #tt(ID,Supervisor)
VALUES
(1,'John Doe'),
(2,'Peter Jones'),
(2,'Sarah Jones'),
(3,'Peter Jones'),
(3,'Sarah Jones'),
(4,'Stephen Davies'),
(4,'Peter Jones'),
(4,'Sarah Jones'),
(5,'John Doe');
SELECT
*
FROM
(
SELECT
ID,
'Supervisor ' + CAST(ROW_NUMBER() OVER(PARTITION BY ID ORDER BY Supervisor) AS VARCHAR(128)) AS supervisor_id,
Supervisor
FROM
#tt
) AS tt
PIVOT(
MAX(Supervisor) FOR
supervisor_id IN ([Supervisor 1],[Supervisor 2],[Supervisor 3])
) AS piv;
Result:
ID Supervisor 1 Supervisor 2 Supervisor 3
1 John Doe NULL NULL
2 Peter Jones Sarah Jones NULL
3 Peter Jones Sarah Jones NULL
4 Peter Jones Sarah Jones Stephen Davies
5 John Doe NULL NULL
You will notice that the assignment to Supervisor X is done by ordering by the Supervisor-VARCHAR. If you want the ordering done differently, you might want to include an [Ordering] column; then change to ROW_NUMBER() OVER(PARTITION BY ID ORDER BY [Ordering]). Eg an [Ordering] column could be an INT IDENTITY(1,1). I'll leave that as an excercise to you if that's what's really needed.
Given a table in the format of
ID Forename Surname
1 John Doe
2 Jane Doe
3 Bob Smith
4 John Doe
How would you go about getting the output
Forename Surname Count
John Doe 2
Jane Doe 1
Bob Smith 1
For a single column I would just use count, but am unsure how to apply that for multiple ones.
SELECT Forename, Surname, COUNT(*) FROM YourTable GROUP BY Forename, Surname
I think this should work:
SELECT Forename, Surname, COUNT(1) AS Num
FROM T
GROUP BY Forename, Surname
I have a table which contains names:
Name
----
John Smith
John Smith
Sam Wood
George Wright
John Smith
Sam Wood
I want to create a select statement which shows this:
Name
'John Smith 1'
'John Smith 2'
'Sam Wood 1'
'George Wright 1'
'John Smith 3'
'Sam Wood 2'
In other words, I want to add separate counters to each name. Is there a way to do it without using cursors?
Use ROW_NUMBER():
SELECT Name, ROW_NUMBER() OVER(Partition BY Name ORDER BY Name) as [Rank]
FROM MyTable
Doing:
select name, count(*) as total from table group by name;
will get you something that looks like this:
name | total
-------------+------------
John Smith | 2
-------------+------------
Sam Wood | 2
-------------+------------
George Wright| 1
This isn't what you really wanted though - ROW_NUMBER(), as ck pointed out, is what you want, but not all databases support it - mysql doesn't, for example. If you're using MySQL, this might help:
ROW_NUMBER() in MySQL