How do I count from 2 different tables in access sql - sql

I have two tables, one that contains information about patients visits: PatientName,DoctorName,DataOfVisit, etc and the second table that contains information about doctors: DoctorName and DoctorSpeciality.
I need to create a query that will print me the PatientName, the number of doctors that patient went to, and the number of different specialties.
If I run
SELECT PatientName, COUNT(VISITS.DoctorName) as DocNum, Count(DoctorSpeciality) as SpecNum
FROM VISITS
INNER JOIN Doctors
ON VISITS.DoctorName = Doctors.DoctorName
GROUP BY PatientName, VISITS.DoctorName, DoctorSpeciality
I get the number of Doctors but not the number of Specialities and the patients are not grouped.

In most databases, you would just use count(distinct specialty). But, Access doesn't support that.
You can do what you want with two group bys:
SELECT PatientName, COUNT(*) as NumSpecialties, SUM(NumDocs) as NumDocs
FROM (SELECT PatientName, Doctors.DoctorSpeciality, COUNT(*) as NumDocs
FROM VISITS INNER JOIN
Doctors
ON VISITS.DoctorName = Doctors.DoctorName
GROUP BY PatientName, DoctorSpeciality
) as pd
GROUP BY PatientName;

You can add DISTINCT within your COUNT() function, also you should not include the aggregate fields in your GROUP BY.
Here is how I would change the query.
SELECT PatientName, COUNT(DISTINCT VISITS.DoctorName) as DocNum,
Count(DISTINCT DoctorSpeciality) as SpecNum
FROM VISITS INNER JOIN
Doctors ON VISITS.DoctorName = Doctors.DoctorName
GROUP BY PatientName

use table name with fields that you want to select from db
SELECT VISITS.PatientName, COUNT(VISITS.DoctorName) as DocNum, Count(Doctors.DoctorSpeciality) as SpecNum
FROM VISITS
INNER JOIN Doctors
ON VISITS.DoctorName = Doctors.DoctorName
GROUP BY PatientName, VISITS.DoctorName, DoctorSpeciality

Related

Join with count

I need to write SQL query like:
Show all countries with more than 1000 users, sorted by user count.
The country with the most users should be at the top.
I have tables:
● Table users (id, email, citizenship_country_id)
● Table countries (id, name, iso)
Users with columns: id, email, citizenship_country_id
Countries with columns: id, name, iso
SELECT countries.name,
Count(users.citiizenship_country_id) AS W1
FROM countries
LEFT JOIN users ON countries.id = users.citizenship_country_id
GROUP BY users.citiizenship_country_id, countries.name
HAVING ((([users].[citiizenship_country_id])>2));
But this does not work - I get an empty result set.
Could you please tell me what I'm doing wrong?
A LEFT JOIN is superfluous for this purpose. To have 1000 users, you need at least one match:
SELECT c.name, Count(*) AS W1
FROM countries c JOIN
users u
ON c.id = u.citizenship_country_id
GROUP BY c.name
HAVING COUNT(*) > 1000;
Notice that table aliases also make the query easier to write and to read.
Group by country name and use HAVING Count(u.citiizenship_country_id)>1000, it filters rows after aggregation:
SELECT c.name,
Count(u.citiizenship_country_id) AS W1
FROM countries c
INNER JOIN users u ON c.id = u.citizenship_country_id
GROUP BY c.name
HAVING Count(u.citiizenship_country_id)>1000
ORDER BY W1 desc --Order top counts first
;
As #GordonLinoff pointed, you can use INNER JOIN instead of LEFT JOIN, because anyway this query does not return counries without users and INNER JOIN performs better because no need to pass not joined records to the aggregation.

SQL: Write a query to get the average order value by gender

I have two tables: one represents transaction and the other represents customer attributes. "transactions" table has 4 columns: id, user_id, product_id, quantity. "users" table has 3 columns: id, name, sex.
The goal is to write a query to find the average quantity value by gender. I can't quiet get this one. I guess we have to use groupby for gender, and avg() for quantity? I'm new to SQL and I'm not sure how to use these. Thank you!
Try this query:
SELECT AVG(t.quantity), u.sex
FROM TRANSACTIONS t
INNER JOIN USERS u ON t.user_id = u.id
GROUP BY u.sex
I think below should work
select avg(price), u.sex
from products
join transaction as t
on products.id = t.product_id
join users as u
on t.user_id = u.id
group by u.sex

Aggregate query across two tables in SQL?

I'm working in BigQuery. I've got two tables:
TABLE: orgs
code: STRING
group: STRING
TABLE: org_employees
code: STRING
employee_count: INTEGER
The code in each table is effectively a foreign key. I want to get all unique groups, with a count of the orgs in them, and (this is the tricky bit) a count of how many of of those orgs only have a single employee. Data that looks like this:
group,orgs,single_handed_orgs
00Q,23,12
00K,15,7
I know how to do the first bit, get the unique groups and count of associated orgs from the orgs table:
SELECT
count(code), group
FROM
[orgs]
GROUP BY group
And, I know how to get the count of single-handed orgs from the practice table:
SELECT
code,
(employee_count==1) AS is_single_handed
FROM
[org_employees]
But I'm not sure how to glue them together. Can anyone help?
for BigQuery: legacy SQL
SELECT
[group],
COUNT(o.code) as orgs,
SUM(employee_count = 1) as single_handed_orgs
FROM [orgs] AS o
LEFT JOIN [org_employees] AS e
ON e.code = o.code
GROUP BY [group]
using LEFT JOIN in case if some codes are missing in org_employees tables
for BigQuery: standard SQL
SELECT
grp,
COUNT(o.code) AS orgs ,
SUM(CASE employee_count WHEN 1 THEN 1 ELSE 0 END) AS single_handed_orgs
FROM orgs AS o
LEFT JOIN org_employees AS e
ON e.code = o.code
GROUP BY grp
Note use of grp vs group - looks like standard sql does like use of Reserved Keywords even if i put backticks around
Confirmed:
you can use keyword with backticks around
You could join the two tables to get the groups that have just one employee. Then you wrap this in a sub query and you count the groups that you have.
I'm using a COUNT DISTINCT and GROUP BY because I don't know how your data is structured. Is there only a single line per group or multiple?
SELECT
COUNT(DISTINCT group)
FROM (
SELECT
group
FROM
orgs AS o INNER JOIN org_employees AS e ON o.code = e.code
WHERE
employee_count = 1
GROUP BY
group
)

SQL View how to Group By and Sum

I have an Entries Table and a Members table and I want to sum all of the entries based on a member name. I've created a view to do this but I'm having a terrible time trying to get the syntax correct.
CREATE VIEW [dbo].[Members_View] AS
SELECT Members.ID, Members.Name, Members.Email,
(SELECT COUNT(*) WHERE AssignedTo = Members.Name) as ECount
From Members JOIN dbo.Entries ON Members.[Name] = Entries.[AssignedTo]
Group By
Name,
Members.ID,
Members.Email,
Entries.AssignedTo
If I remove the Group By I simply get the number 1 in my new ECount column for each entry but multiples of each name. Once I group by I only have one of each name but each entry still has only 1 count. How do I Group By AND Sum?
I think you can just do a simple aggregation query, leaving out Entries.AssignedTo:
CREATE VIEW [dbo].[Members_View] AS
SELECT Members.ID, Members.Name, Members.Email,
COUNT(*) as ECount
From Members JOIN
dbo.Entries
ON Members.[Name] = Entries.[AssignedTo]
Group By
Name,
Members.ID,
Members.Email;
You could count the entries in a sub-select and then join it to the main table
CREATE VIEW [dbo].[Members_View] AS
SELECT Members.ID, Members.Name, Members.Email, entries.ECount
FROM Members
JOIN (SELECT AssignedTo, COUNT(*) AS ECount FROM Entries GROUP BY AssignedTo) entries ON entries.AssignedTo = Members.Name

Count, inner join

I have two tables:
DRIVER(Driver_Id,First name,Last name,...);
PARTICIPANT IN CAR ACCIDENT(Participant_Id,Driver_Id-foreign key,responsibility-yes or no,...).
Now, I need to find out which driver participated in accident where responsibility is 'YES', and how many times. I did this:
Select Driver_ID, COUNT (Participant.Driver_ID)as 'Number of accidents'
from Participant in car accident
where responsibility='YES'
group by Driver_ID
order by COUNT (Participant.Driver_ID) desc
But, I need to add drivers first and last name from the first table(using inner join, I suppose). I don't know how, because it is not contained in either an aggregate function or the GROUP BY clause.
Please help :)
As you suspected, you need to use an inner join. And because the first name and last name are now part of the SELECT, you also need to include those columns in the GROUP BY.
Select Driver_ID, First_name, Last_name COUNT (Participant.Driver_ID) as "Number of accidents"
from "Participant in car accident" join Driver on "Participant in car accident".Driver_ID = Driver.Driver_ID
where responsibility='YES'
group by Driver_ID, First_name, Last_name
order by COUNT (Participant.Driver_ID) desc
Is this homework?
You could use an inline table:
SELECT d.driver_first_name,
d.driver_last_name,
r.incident_count
FROM DRIVER d
INNER JOIN (SELECT driver_id,
count(*) incident_count
FROM PARTICIPANT_IN_CAR_ACCIDENT
WHERE responsibility = 'YES'
GROUP BY driver_id) r
ON d.driver_id = r.driver_id
ORDER BY r.incident_count DESC
Should work.