SQL Select for each Age Group and COUNT Members - sql

I have the following tables
[Members]:
MemberID DOB Name
------------------------------------
1 01/01/2000 Jon
2 01/01/2005 Bob
3 01/01/2009 Mike
4 01/01/1980 Greg
[AgeGroups]:
GroupID GroupTitle MinAge MaxAge
--------------------------------------------------
1 GroupA 0 5
2 GroupB 6 10
3 GroupC 11 18
[Events]:
EventID EventStart
---------------------------
1 01/01/2015
I want to be able to do a SQL select command which gets the Group Title and a count of the number of members who's age will be within the age range of each of the Age Groups at the date of the EventStart.
GroupTitle MemberCount
----------------------------------
GroupA 1
GroupB 2
GroupC 1
I've been trying to do this with SELECT CASE WHEN but have got stuck.
Any help would be greatly appreciated!

Take a look at Date and Time Data Types and Functions, and in particular at the DATEDIFF function.
You could try something like the following:
SELECT AgeGroups.GroupID, AgeGroups.GroupTitle, COUNT(1) AS MemberCount
FROM AgeGroups
JOIN Events ON EventID = 1
JOIN Members
ON DATEDIFF(year, Members.DOB, Events.EventStart) >= AgeGroups.MinAge
AND DATEDIFF(year, Members.DOB, Events.EventStart) <= AgeGroups.MaxAge
ORDER BY AgeGroups.GroupID
Does that help?

Related

count different column values after grouping by

Consider this table:
id name department email
1 Alex IT blah#gmail.com
1 Alex IT blah#gmail.com
2 Jay HR jay#gmail.com
2 Jay Marketing zou#gmail.com
If I group byid,name and count I get:
id name count(*)
1 Alex 2
2 Jay 2
With this query:
select id,name,count(*) from tb group by id,name;
However I would like to count only records that diverge from department,email, so as to have:
id name count(*)
1 Alex 0
2 Jay 1
This time the count for the first group 1,Alex is 0 because department,email have the same values (duplicated) , on the other hand 2,Jay is one because department,email has one different value.
If you meant "two different values" for "Jay", you can use distinct:
select id,name,count(*) from (SELECT distinct * FROM tb) group by id,name;
You can use count(*) - 1 to get similar results in your question.

Count values separately until certain amount of duplicates SQL

I need a Statement that selects all patients and the amount of their appointments and when there are 3 or more appointments that are taking place on the same date they should be counted as one appointment
That is what my Statement looks so far
SELECT PATSuchname, Count(DISTINCT AKTDATUM) AS AKTAnz
FROM tblAktivitaeten
LEFT OUTER JOIN tblPatienten ON (tblPatienten.PATID=tblAktivitaeten.PATID)
WHERE (AKTDeleted<>'J' OR AKTDeleted IS Null)
GROUP BY PATSuchname
ORDER BY AKTAnz DESC
The result should look like this
PATSuchname Appointments
----------------------------------------
Joey Patner 13
Billy Jean 15
Example Name 13
As you can see Joey Patner has 13 Appointments, in the real table though he has 15 appointments but three of them have the same Date and because of that they are only counted as 1
So how can i write a Statement that does exactly that?
(I am new to Stack Overflow, sorry if the format I use is wrong and tell me if it is.
In the table it looks like this.
tblPatienten
----------
PATSuchname PATID
------------------------
Joey Patner 1
Billy Jean 2
Example Name 3
tblAktivitaeten
----------
AKTDatum PATID AKTID
-----------------------------------------
08.02.2021 1 1000 ----
08.02.2021 1 1001 ---- So these 3 should counted as 1
08.02.2021 1 1002 ----
09.05.2021 1 1003
09.07.2021 2 1004 -- these 2 shouldn't be counted as 1
09.07.2021 2 1005 --
Two GROUP BY should do it:
SELECT
x.PATID, PATSuchname, SUM(ApptCount)
FROM (
SELECT
PATID, AKTDatum, CASE WHEN COUNT(*) < 3 THEN COUNT(*) ELSE 1 END AS ApptCount
FROM tblAktivitaeten
GROUP BY
PATID, AKTDatum
) AS x
LEFT JOIN tblPatienten ON tblPatienten.PATID = x.PATID
GROUP BY
x.PATID, PATSuchname

Query to get every sample with at least 3 occurrences at every category

I'm struggling to select every patient which has at least 3 visits at every specialist in 2019, but so far all my attempts have failed.
Could someone has any idea how to achieve?
Thanks in advance.
Table A:
PacjentId
Name
Surname
DateOfVisit
SpecializationId
1
john
doe
2019-04-05
1
1
john
doe
2019-05-06
2
2
mary
jane
2019-02-03
1
2
mary
jane
2019-09-07
2
3
mark
twain
2020-01-01
1
...
...
...
...
...
Table B:
Id
NameOfSpecialisation
1
Diabetologist
2
Internist
...
...
You just need a COUNT(DISTINCT in a HAVING clause:
SELECT PacjentId, Name, Surname
FROM A
WHERE DateOfVisit >= '2019-01-01' AND DateOfVisit < '2020-01-01'
GROUP BY PacjentId, Name, Surname
HAVING COUNT(DISTINCT SpecializationId) >= 3
Your table design appears to be denormalized: A represents Visits and B represents Specializations, therefore there should be another Patients table containing Name, Surname

Get count of two different values but not same values

I have the following table format.
**ID Name Start Date End Date**
1 ABC 1/1/2015 12/31/2015
1 XYZ 4/1/2015 8/31//2015
1 DEF 1/1/2012 12/31/2012
2 ABC 1/23/2011 1/23/2012
2 ABC 1/31/2012 1/31/2013
3 DEF 2/12/2015 5/30/2015
3 XYZ 4/1/2015 6/01/2015
4 DEF 3/1/2015 12/31/2015
4 DEF 4/1/2015 6/30/2015
I need the count of ID's having Different Name which lies in date range of May 2015
Expected Results
ID COUNT
1 2
3 2
P.S - ID 4 also lies in the date range of MAY 2015, but the Name is same i.e DEF. So I need only ID's 1 and 3 but not 4.
Thank You in advance and appreciated for your efforts.
I imagine your sample data doesn't match your desired results, but I think this is what you're looking for using conditional aggregation:
select id, count(*)
from yourtable
group by id
having sum(case when '5/1/2015' between startdate and enddate then 1 else 0 end) > 1
and count(distinct name) = count(name)
SQL Fiddle Demo
The sum aggregation in the having clause is making sure there are multiple records in between that date. The count clause in the having clause is making sure there aren't any duplicates.
declare
#startdate datetime = '20150501',
#enddate datetime = '20150531'
select t.id, count(distinct t.name)
from mytable t
where t.startdate <= #enddate and t.enddate >= #startdate
group by t.id
having count(distinct t.name) > 1

SQL Group By Help Required

I have a table named People in the following format:
Date | Name.
When I count the people by Grouping By Name with
Select Date, Name, count(*)
From People
Group By Date, Name;
Will give the following
Date Name count(*)
10 Peter 25
10 John 30
10 Mark 25
11 Peter 15
11 John 10
11 Mark 5
But I would like the following result:
Date Peter John Mark
10 25 30 25
11 15 10 5
Is this possible? This is a simple example of a more complicated database. If someone helps me in solving this problem I will use the concept to implement it in my table
Thanks!
Select Date
, count(case when Name = 'Peter' then 1 else null end)
, count(case when Name = 'John' then 1 else null end)
, count(case when Name = 'Mark' then 1 else null end)
From People
Group By Date;
another option different from turbanoff's if, for some reason, you find yourself in a situation that you cant apply a group by:
Select distinct(P.Date),
(select count(*) from People where date=p.date and name='Peter') as Peter,
(select count(*) from People where date=p.date and name='John') as John,
(select count(*) from People where date=p.date and name='Mark') as Mark
From People P