Skip to content
Advertisement

How do I select rows that are not matched on specific column (sqlite)

Ok hopefully my table works, I’ve never posted here before, but I’m trying to find who has received doses of more than one type of vaccine from this table (all details are dummy data, not real people or id numbers etc)

NHI_id fname lname vaccine_name vac_date
16120428 Clayton Marsh Comirnaty 2021-03-20
16120428 Clayton Marsh Janssen 2021-04-01
16120430 Dillon Richards Comirnaty 2021-03-20
16120430 Dillon Richards Comirnaty 2021-04-10
16120432 Giselle Miles Janssen 2021-04-15
16120433 Taylor Buckley Comirnaty 2021-03-20
16120434 Kermit Greene Janssen 2021-04-15
16120435 Harrison Dorsey Janssen 021-04-15

So Clayton March is the only person who has received doses of different vaccines and I’m trying show that with a query. I got the above table with the following query:

SELECT NHI_id, fname, lname, vaccine_name, vac_date
FROM Patients
NATURAL JOIN Vaccinations
NATURAL JOIN Vaccines

and I’ve been able to query everyone who has received two doses of any vaccine, but I’m having trouble then narrowing it down to doses of DIFFERENT vaccines.

Advertisement

Answer

Something like

SELECT NHI_id, fname, lname
FROM Patients
NATURAL JOIN Vaccinations
NATURAL JOIN Vaccines
GROUP BY NHI_id, fname, lname
HAVING count(DISTINCT vaccine_name) > 1

should do it.

User contributions licensed under: CC BY-SA
4 People found this is helpful
Advertisement