There are 2 tables from the above example there one table is university name and block name, in table 1 there are university name and the teams they have in respective sports, in table 2 there are universities and sports according to block name and i want to add the number of occurrences of that sport in the table 1
This is just an example table, the main tables are really large but the principle is same
If you are using sql server, you can do this:
update t1
set
Rugby = RugbyCount
, Basketball = BasketballCount
, Baseball = BaseballCount
from table1 t1
join ( select UNI
, count(case when sport ='Rugby' then 1 end) RugbyCount
, count(case when sport ='Basketball' then 1 end) BasketballCount
, count(case when sport ='Baseball' then 1 end) BaseballCount
from table2
group by UNI
) t2
join on t1.uni = t2.uni
this is what worked for me on MySQL
update db.tbl t1
join (select UNI,
(select count(SPORT) where SPORT= 'Rugby') CRugby,
(select count(SPORT) where SPORT= 'Basketball') CBasketball,
(select count(SPORT) where SPORT= 'Baseball') CBaseball,
from db.tb2
group by sport, UNI) t2 on t1.UNI = t2.UNI
set Rugby= CRugby,
Basketball= CBasketball,
Baseball= CBaseball;
Related
I have a table of clients in the following format
client_firstname client_lastname fruit purchase_date
Lionel Messi apples 11/11/2020
Lionel Messi bananas 11/13/2020
Federico Chiesa oranges 11/20/2020
.
.
.
I want to create a random subset of 10000 first name,lastname pairs who never bought apples and extract all the entries associated with them.
so far I have:
select t.*
from client_table t
where exists (select 1
from client_table t2
where t2.firstname = t.firstname and
t2.lastname = t.lastname
t2.fruit <> "apples")
I know it is possible to create a second table with:
select distinct client_table.firstname, client_table.lastname
where table.fruit <> "apples"
order by rand() limit 10000
but is it possible to include this table in the where exists statement and avoid creating a second table?
who never bought apples
This:
exists (...
t2.fruit <> "apples")
..does not meet the specification. The code is "people who bought something that is not an apple", not "people who never bought an apple"
This gives you non-apple-buyers:
select t.*
from client_table t
where not exists (select 1
from client_table t2
where t2.firstname = t.firstname and
t2.lastname = t.lastname
t2.fruit = 'apples')
But I don't think I'd do it this way, because that list will contain duplicates
Instead let's ask for the unique list of people that haven't bought apples:
select firstname, lastname
from client_table
group by firstname, lastname
having sum(case when fruit = 'apples' then 1 else 0 end) = 0
Then you can add your order by/limit onto that, and if you need more data from the table, turn it into a subquery and join it back to the main table
select t.*
from
client_table t
inner join
(
select firstname, lastname
from client_table
group by firstname, lastname
having sum(case when fruit = 'apples' then 1 else 0 end) = 0
) x on x.firstname = t.firstname and x.lastname = t.lastname
Hi i get the error 'Every derived table must have its own alias' what can i do about it??
My question is :
Show team ID and names from teams from Germany who have never played in UEFA Champions league
UEFA Champions league = 1
Cid= Competions ID
Tid = Team ID
SELECT teams.TID, teams.name from(
SELECT Tid1 FROM(
(SELECT tid1,cid FROM matches
WHERE tid1 IN (SELECT tid FROM teams WHERE country='Germany')
UNION
SELECT tid2,cid FROM matches
WHERE tid2 IN (SELECT tid FROM teams WHERE country='Germany'))
)WHERE cid <> (SELECT cid FROM competitions WHERE cid='1'))
INNER JOIN matches ON tid1=team.tid;
I have tried looking at others derived soulutions but i cant get to work with mine...
It looks like its asking for you to give alias to the tables you have created within the query. Try this.
EFA Champions league = 1
Cid= Competions ID
Tid = Team ID
SELECT teams.TID, teams.name from(
SELECT Tid1 FROM(
(SELECT tid1,cid FROM matches
WHERE tid1 IN (SELECT tid FROM teams WHERE country='Germany')
UNION
SELECT tid2,cid FROM matches
WHERE tid2 IN (SELECT tid FROM teams WHERE country='Germany'))ALIAS1
)WHERE cid <> (SELECT cid FROM competitions WHERE cid='1')) ALIAS2
INNER JOIN matches ON tid1=team.tid;
I have two tables, student and school.
student
stid | stname | schid | status
school
schid | schname
Status can be many things for temporary students, but NULL for permanent students.
How do I list names of schools which has no temporary students?
Using Conditional Aggregate you can count the number of permanent student in each school.
If total count of a school is same as the conditional count of a school then the school does not have any temporary students.
Using JOIN
SELECT sc.schid,
sc.schname
FROM student s
JOIN school sc
ON s.schid = sc.schid
GROUP BY sc.schid,
sc.schname
HAVING( CASE WHEN status IS NULL THEN 1 END ) = Count(*)
Another way using EXISTS
SELECT sc.schid,
sc.schname
FROM school sc
WHERE EXISTS (SELECT 1
FROM student s
WHERE s.schid = sc.schid
HAVING( CASE WHEN status IS NULL THEN 1 END ) = Count(*))
You can use not exists to only select schools that do not have temporary students:
select * from school s
where not exists (
select 1 from student s2
where s2.schid = s.schid
and s2.status is not null
)
You can use a regular join.
SELECT DISTINCT c.schName
FROM Students s
INNER JOIN Schools c ON s.schid = c.schid
WHERE s.status IS NULL
i hope i can explain my Problem in detail, so you guys can understand me.
Ive created a small example.
I have a Table which looks like this:
City | Name
Berlin | Mike
Berlin City| Peter
Stuttgart | Boris
here is my Query:
SELECT CASE
WHEN City like '%Berlin%' THEN 'Count Person in Berlin:'
WHEN City like '%Stuttgart%' THEN 'Count Person in Stuttgart:'
WHEN City like '%Dresden%' THEN 'Count Person in Dresden:'
ELSE 'unknown'
END AS Text,
COUNT(Name) AS countPersons
FROM tblTest
GROUP BY City
This is the result:
Count Person in Berlin: 2
Count Person in Stuttgart: 1
But my desired result is:
Count Person in Berlin: 2
Count Person in Stuttgart: 1
Count Person in Dresden: 0
how can i get my desired result? hope you can help me.
Thanks in advance.
SQL Fiddle Demo
If you don't have a table with the list of cities, then you can use a subquery. The key to solving this type of problem is left outer join:
select cities.city, count(t.city) as numpeople
from (select 'Berlin' as city union all
select 'Stuttgart' union all
select 'Dresden'
) cities left outer join
tbltest t
on t.city = cities.city
group by cities.city;
If you want to have 'unknown' as well, then full outer join can be used:
select coalesce(cities.city, 'unknown') as city, count(t.city) as numpeople
from (select 'Berlin' as city union all
select 'Stuttgart' union all
select 'Dresden'
) cities full outer join
tbltest t
on t.city = cities.city
group by coalesce(cities.city, 'unknown');
I'm trying to make a query that looks at a single table to see if a student is in a team called CMHT and in a medic team - if they are I don't want to see the result.
I only want see the record if they're only in CMHT or medic, not both.
Would the right direction be using sub query to filter it out? I've done a search on NOT IN but how could you get to see check if its in more then 2 teams are not?
Student Team ref
1 CMHT 1
1 Medic 2
2 Medic 3 this would be in the result
3 CMHT 5 this would be in the result
So far I've done the following code would I need use a sub query or do a self join and filter it that way?
SELECT Table1.Student, Table1.Team, Table1.refnumber
FROM Table1
WHERE (((Table1.Team) In ('Medics','CMHT'))
This is Mark Byers's answer with a HAVING clause instead of a subquery:
SELECT Student, Team, ref
FROM Table1
GROUP BY Student
HAVING COUNT(Student) = 1
SELECT *
FROM students
WHERE NOT EXISTS
(
SELECT NULL
FROM students si
WHERE si.student = s.student
AND si.team = 'CMHT'
)
OR NOT EXISTS
(
SELECT NULL
FROM students si
WHERE si.student = s.student
AND si.team = 'Medic'
)
SELECT a.*
FROM Table1 a
INNER JOIN
( SELECT Student, COUNT(*) FROM Table1
GROUP BY Student
HAVING COUNT(*) = 1)b
ON (a.Student = b.Student)
how could you get to see check if its in 2 or more teams?
You can count the number of teams per student and then filter only those you want to see:
SELECT student FROM
(
SELECT student, COUNT(*) AS cnt
FROM Table1
GROUP BY student
) T1
WHERE cnt = 1
You can do it with outer join
select COALESCE(t1.Student, t2.Student) as Student,
COALESCE(t1.Team, t2.Team) as Team,
COALESCE(t1.ref, t2.ref) as ref
from
(select * from Student where Team = 'CMHT') t1
outer join
(select * from Student where Team = 'Medic') t2
on t1.Student = t2.Student
where
t1.Student is null or
t2.Student is null;