SQL - counting by id - sql

I'm currently trying to list the number of visits attended by each vet. I have to include the vets name details with the count of visits attended. I then have to sort the report by the vets title.
Visit table structure:
visit_id, vet_id, pet_id, Visit_Date, Basic_Cost, Symptom, Treatment
Vet table structure:
vet_id, Surname, Forename, Title, Ext_Number, Position, Salary
Below is my initial SQL command that I have created. It doesn't work and I was wondering how to create an SQL command for the above question. All help is appreciated and please note I'm using ms access.
SELECT vet_id, Title, Forename, Surname, COUNT(vet_id)
FROM visit, vet
ORDER BY Title;

Assuming you want all vets even if they haven't had visits...
SELECT P.vet_id, Title, Forename, Surname, coalesce(COUNT(V.vet_id),0) as VetVisitCount
FROM VET P
LEFT JOIN Visit V
on P.Vet_ID = V.Vet_ID
GROUP BY P.vet_id, Title, Forename, Surname
ORDER BY Title;

SELECT vet_id, title, Forename, Surname,
(SELECT COUNT(visit.vet_id) FROM visit WHERE visit.vet_id = vet.vet_id) AS visits
FROM vet
ORDER BY vet.vet_id;

You need a GROUP BY
SELECT vet_id, Title, Forename, Surname, COUNT(vet_id)
FROM visit, vet
GROUP BY vet_id, Title, Forename, Surname
ORDER BY Title;

Related

VB.NET/Access - SELECT SUM SQL statement

I have a table with LastName, FirstName, Wins, Losses, CompFormat and Medals columns. In case the person who told me not to use pictures on my last question sees this I tried your suggestion and couldn't figure it out so I have to use pictures. So don't bite my head off this time. I successfully added and grouped Wins with CompFormat like this...
("SELECT SUM(Wins) AS Total, FirstName, LastName, CompFormat FROM CompetitionDate GROUP BY LastName, FirstName, CompFormat;")
Which correctly produced this in my datagridview...
Instead of what I did I want to add counting losses and group it to look like this
Here is my access table...
I think you just need to add the Losses column:
SELECT FirstName, LastName, CompFormat,
SUM(Wins) AS Wins, SUM(Losses) as Losses
FROM CompetitionDate
GROUP BY LastName, FirstName, CompFormat

Trouble with COUNT

I have two tables, PATIENT and VISIT. One with PatientID as the primary key and one with VisitID as the primary key. I need to select the first name and last name of the patients that have visited the hospital more than twice.
I have tried DISTINCT, a nested where clause, INNER JOIN, etc.
SELECT FirstName
, LastName
, PatientID
, COUNT(*) AS total_visits
FROM VISIT
WHERE total_visits > 2;
It should just show the first and last name of the patients that have more than two occurrences in the VISIT table, but no matter how I rearrange the code it doesn't work.
Following on from Gordon's answer and your comment I presume that PatientID in VISIT is a key to the PATIENT table. So you will need to use an ´INNER JOIN´. So your query looks something like this:
SELECT FirstName, LastName, v.PatientID, COUNT(*) AS total_visits
FROM VISIT v
INNER JOIN PATIENT p ON p.PatientID = v.PatientID
GROUP BY FirstName, LastName, v.PatientID
HAVING COUNT(*) > 2;
Note that AFAIK in Access you cannot use the alias name in the HAVING clause. You need to repeat the COUNT(*) as is.
You need GROUP BY and HAVING:
SELECT FirstName, LastName, PatientID, COUNT(*) AS total_visits
FROM VISIT
GROUP BY FirstName, LastName, PatientID
HAVING total_visits > 2;

No column was specified for column 1 of 'T1' when using a sub-select with a group by

I have a working query:
SELECT
COUNT(*), ACCOUNT_ID
FROM
CDS_PLAYER
GROUP BY
ACCOUNT_ID
HAVING
COUNT(*) > 1`
Output
No column name Account_ID
----------------------------
'2' '12345'
I'm trying to add names to these accounts (all from the same table) but with no luck. The only query that gets me close is:
SELECT
LASTNAME, FIRSTNAME, COUNT(ACCOUNT_ID) AS NUMBER
FROM
(SELECT
COUNT(*), ACCOUNT_ID
FROM
CDS_PLAYER
GROUP BY
ACCOUNT_ID
HAVING
COUNT(*) > 1) AS T1
GROUP BY
LASTNAME, FIRSTNAME, PLAYER_ID
But I get an error:
No column was specified for column 1 of 'T1'
Like I said VERY NEW AT THIS. My boss of 4 months wanted me to learn and so I'm self taught (books and google). Any help at all to get me where I need to be would be appreciated!
(I'm using Windows Server 2003 and SQL Server 2000)
The error message can be resolved as below
SELECT LASTNAME, FIRSTNAME, COUNT(ACCOUNT_ID) AS NUMBER
FROM
(SELECT COUNT(*) AS Total, ACCOUNT_ID FROM CDS_PLAYER GROUP BY ACCOUNT_ID HAVING
COUNT(*) > 1) AS T1
GROUP BY LASTNAME, FIRSTNAME, PLAYER_ID`
Add as TOTAL after the count(*)
Does this do what you want?
SELECT COUNT(*), ACCOUNT_ID, LASTNAME, FIRSTNAME, PLAYER_ID
FROM CDS_PLAYER
GROUP BY ACCOUNT_ID, LASTNAME, FIRSTNAME, PLAYER_ID
HAVING COUNT(*) > 1;
You should also update your version of SQL Server. It is like 15 years out of date and hasn't been supported in many years. You can download a free version of SQL Server Express from Microsoft.
you want to select the LASTNAME and FIRSTNAME, but havn't it selected in your subselect. You only can access field which are in the resultset.
Solution: Add it to your GROUP BY clause.
ie:
SELECT
LASTNAME, FIRSTNAME, COUNT(ACCOUNT_ID) AS NUMBER
FROM
(SELECT COUNT(*), LASTNAME, FIRSTNAME, ACCOUNT_ID
FROM CDS_PLAYER
GROUP BY ACCOUNT_ID, LASTNAME, FIRSTNAME
HAVING COUNT(*) > 1) AS T1
GROUP BY
LASTNAME, FIRSTNAME, PLAYER_ID

group by while selecting many more columns

I have this query :
select first_name, last_name, MAX(date)
from person p inner join
address a on
a.person_id = p.id
group by first_name, last_name
with person(sid, last_name, first_name), address(data, cp, sid, city)
My question is how I can have a query that select first_name, last_name, MAX(date), city, cp
without adding city and cp to the group
I mean I want to have all 5 columns but only for the datas grouped by first_name, last_name and date
Many Thanks
This is not possible. Say you have three John Smith in your database, each of them having one or two addresses. When you group by name now, then what city do you want to get? The city of which John Smith and of which of his addresses? As there is no implicit answer to this question, there is no way to write a select statement without explicitly saying which city is to be selected.

SQL DISTINCT [Alternative Using]

I have a simple query on Oracle.
SELECT DISTINCT City, Name, Surname FROM Persons
Is there any alternative sql query for the same query without DISTINCT ?
Have a look at this article
Example as;
select City
from (
select City,
row_number() over
(partition by City
order by City) rownumber
from Persons
) t
where rownumber = 1
SELECT City, Name, Surname FROM Persons
UNION
SELECT City, Name, Surname FROM Persons
SELECT First(City), First(Name), First(Surname)
FROM Persons
GROUP BY City, Name, Surname