The table f has these values:
name1 name2 count
Harrison Ford Mullah Omar 3
Harrison Ford Michael Caine 6
Michael Caine Mullah Omar 2
Michael Caine Harrison Ford 6
Mullah Omar Michael Caine 2
Mullah Omar Harrison Ford 3
How to choose only the distinct pairs?
select
distinct name1,
distinct name2, count from f group by name1
Given that you have a duplicate for each pair, you can just use:
select f.*
from f
where name1 < name2;
If you did not have the reverse in all cases, you can use:
select f.*
from f
where f.name1 < f.name2 or
not exists (select 1
from f f2
where f2.name1 = f.name2 and f2.name2 = f.name1
);
Related
is it possible to change these two tables
Employee A B C
Alex G G B
Martin B G G
...
------------------------------
Employee2 AA BB CC
Sam G B B
Max G G B
...
Into one table?
List Employee Mark
AAA Alex G
BBB Alex G
CCC Alex B
AAA Sam G
BBB Sam B
CCC Sam B
...
AA and A are different column in different table but I want to display them in one table with the same name (e.g. "AAA") and it must be in row not in column.
You can use a UNION query to unpivot the data from columns into rows:
SELECT 'AAA' AS List, Employee, A AS Mark
FROM t1
UNION ALL
SELECT 'BBB' AS List, Employee, B AS Mark
FROM t1
UNION ALL
SELECT 'CCC' AS List, Employee, C AS Mark
FROM t1
UNION ALL
SELECT 'AAA' AS List, Employee2, AA AS Mark
FROM t2
UNION ALL
SELECT 'BBB' AS List, Employee2, BB AS Mark
FROM t2
UNION ALL
SELECT 'CCC' AS List, Employee2, CC AS Mark
FROM t2
ORDER BY Employee, List
Output (for your sample data)
List Employee Mark
AAA Alex G
BBB Alex G
CCC Alex B
AAA Martin B
BBB Martin G
CCC Martin G
AAA Max G
BBB Max G
CCC Max B
AAA Sam G
BBB Sam B
CCC Sam B
Demo on SQLFiddle
I have 2 tables with rows that don't relate to each other except for 2 common fields. I'm trying to join them in such a way that there are 2 fields that merge.
For example:
Table A:
NameA ID-A SomefieldA SomefieldB
John 101 A-1 B-2
John 200 A-1 B-10
Smith 101 A-10 B-2
Table B:
NameB ID-B SomefieldC SomefieldD
John 101 C-1 D-2
David 2000 C-100 A-10
George 120 C-2 D-20
I want to join these tables together so it would be like this:
Table C:
Name ID Somefield A SomefieldB SomefieldC SomefieldD
John 101 A-1 B-2 (null) (null)
John 200 A-1 B-10 (null) (null)
Smith 101 A-10 B-2 (null) (null)
John 101 (null) (null) C-1 D-2
David 2000 (null) (null) C-100 A-10
George 120 (null) (null) C-2 D-20
That's just a simple union query. You will need to provide all fields in both tables.
SELECT NameA, [ID-A] As ID, SomefieldA, SomefieldB, Null As SomefieldC, Null As SomefieldD
FROM TableA
UNION ALL
SELECT NameB, [ID-B], Null As SomefieldA, Null As SomefieldB, SomefieldC, SomefieldD
FROM TableB
If you want to perform a left join on the result, you would have to use a subquery, and it would look something like this:
SELECT *
FROM (
SELECT NameA, [ID-A] As ID, SomefieldA, SomefieldB, Null As SomefieldC, Null As SomefieldD
FROM TableA
UNION ALL
SELECT NameB, [ID-B], Null As SomefieldA, Null As SomefieldB, SomefieldC, SomefieldD
FROM TableB
) AS UQuery
LEFT JOIN TableC ON TableC.ID = UQuery.ID
I see nothing merging here. Well, this should do:
select name, id, field_a, field_b, null as field_c, null as field_d from a
union all
select name, id, null as field_a, null as field_b, field_c, field_d from b;
My query:
select substr(name,1,1), name
from authors
group by name
order by name;
This is the data (MySQL)
1 C Chris
2 C Cary
3 D Doug
4 D Dave
5 D Drake
6 E Eli
7 E Elma
8 E Ezra
And this is what I want to achieve.
1 C Chris
2 Cary
3 D Doug
4 Dave
5 Drake
6 E Eli
7 Elma
8 Ezra
Any help is appreciated.
Give this a go...
select #substr := if(substr(#name,1,1) <> substr(name,1,1),substr(name,1,1) ,'') as groupedSubstr
,#name := name as name
from (
select name from authors
group by name
order by name
) a;
I am new to SQL queries and I am working on a query to print the COUNT of movies an actor has appeared in, including a total of 0 if they have not appeared in any movies.
I am getting all results as expected except if an actor has not appeared in any movies then they do not appear in the list.
Here is what I have so far -
SELECT a.actor_name, count(*)
FROM ACTOR a
JOIN CAST_MEMBER c ON (a.actor_name = c.actor_name)
GROUP BY a.actor_name
UNION
SELECT c2.actor_name, 0
FROM ACTOR a2 JOIN CAST_MEMBER c2 ON (a2.actor_name = c2.actor_name)
WHERE a2.actor_name NOT IN
(
SELECT a3.actor_name
FROM ACTOR a3
JOIN CAST_MEMBER c3 ON (a3.actor_name = c3.actor_name)
)
EXPECTED OUTPUT
ACTOR_NAME COUNT(*)
-------------------------------------------------- ----------
Amy Adams 1
Brad Pitt 4
Christian Bale 1
Jennifer Anitson 0
Jennifer Lawrence 2
Leonardo DiCaprio 2
OUTPUT I HAVE
ACTOR_NAME COUNT(*)
-------------------------------------------------- ----------
Amy Adams 1
Brad Pitt 4
Christian Bale 1
Jennifer Lawrence 2
Leonardo DiCaprio 2
I would really appreciate it if someone could guide me in the right direction.
Thank you for all the help.
SELECT a.actor_name, count(*)
FROM ACTOR a
LEFT JOIN CAST_MEMBER c ON (a.actor_name = c.actor_name)
GROUP BY a.actor_name;
You need to use LEFT OUTER JOIN in order to show all rows from table ACTOR.
An outer join is a join that displays data from the same rows an inner
join does, but also adds data from rows that don’t necessarily have
matches in all the tables that are joined together. There are three
types of outer joins—LEFT, RIGHT, and FULL.
ANSWER WITHOUT THE UNION
ACTOR_NAME COUNT(*)
-------------------------------------------------- ----------
Amy Adams 1
Brad Pitt 4
Christian Bale 1
Jennifer Aniston 1
Jennifer Lawrence 2
Leonardo DiCaprio 2
ANSWER WITH THE UNION
ACTOR_NAME COUNT(*)
-------------------------------------------------- ----------
Amy Adams 1
Brad Pitt 4
Christian Bale 1
Jennifer Aniston 0
Jennifer Lawrence 2
Leonardo DiCaprio 2
My error was on the joins, Now I have a better understanding of them.
I have four columns output in my resultset:
Name, Subject, Grade, Value
Is it possible to populate a result set using the Max(Value), but also bring through it's adjacent Subject and Grade and how would I achieve this? eg:
Name Subject Grade Value
Helen Chemistry C 7
Helen Physics B 8
Helen Biology A 9
Brad Chemistry C 7
Brad Biology D 6
Brad Physics F 4
Becomes
Name Subject Grade Value
Helen Biology A 9
Brad Chemistry C 7
SELECT *
FROM (
SELECT *,
ROW_NUMBER() OVER (PARTITION BY name ORDER BY value DESC) rn
FROM mytable
) q
WHERE rn = 1