MYSQL - MAX() and GROUP BY() not results as expected - mysql

after running an sql
SELECT
m.stdClassID,
m.Percentage,
sc.ClassID,
sc.Form,
sc.FormName,
sc.Year,
sc.stdName
FROM stdremark m
INNER JOIN (
SELECT
sc.ClassID,
c.Form,
c.FormName,
sc.stdClassID,
c.Year,
CONCAT(s.StdFname,' ',s.StdLname) as stdName
FROM tblstdclass sc
INNER JOIN tblclass c
ON sc.ClassID=c.ClassID
INNER JOIN tblstudents s
ON sc.StdID = s.StdID
WHERE c.Year=2018
) sc ON m.stdClassID=sc.stdClassID
WHERE m.Term=3`
I come up with this result
I want to get the maximum percentage of each student grouped by the Form column
when I try to get Max() and group by()
from the sql:
SELECT
m.stdClassID,
MAX(m.Percentage),
sc.ClassID,
sc.Form,
sc.FormName,
sc.Year,
sc.stdName
FROM stdremark m
INNER JOIN (
SELECT
sc.ClassID,
c.Form,
c.FormName,
sc.stdClassID,
c.Year,
CONCAT(s.StdFname,' ',s.StdLname) as stdName
FROM tblstdclass sc
INNER JOIN tblclass c
ON sc.ClassID=c.ClassID
INNER JOIN tblstudents s
ON sc.StdID = s.StdID
WHERE c.Year=2018
) sc ON m.stdClassID=sc.stdClassID
WHERE m.Term=3
GROUP BY sc.Form`
the expected result should have been
17 71 28 upper Science 2018 Jerry Maguire

Could be an issue with MySQL allowing partial grouping. Try to extend your GROUP BY expression to group on all columns, except m.Percentage.
...
GROUP BY m.stdClassID,
sc.ClassID,
sc.Form,
sc.FormName,
sc.Year,
sc.stdName
Other DBMS would force you to do so anyway.

Related

Is there a method of counting an attribute that is in a GROUP BY clause?

I need have created a select statement to list out all the customers that have been to multiple merchants below.
I want to create another statement to display how many of those customers have been to each merchant.
What is the optimal method of approaching this problem?
Lists out all customers that have been to multiple merchants.
WITH valentinesDayMerchant AS (
SELECT m.MerchantId, m.MerchantGroupId, m.WebsiteName
FROM Merchant m
INNER JOIN OpeningHours oh ON m.MerchantId = oh.MerchantId AND oh.DayOfWeek = 'TUE'
LEFT JOIN devices.DeviceConnectionState AS dcs ON dcs.MerchantId = oh.MerchantId
WHERE MerchantStatus = '-' AND (m.PrinterType IN ('V','O') OR dcs.State = 1 OR dcs.StateTransitionDateTime > '2023-01-23')
)
SELECT DISTINCT ul.UserLoginId, ul.FullName, ul.EmailAddress, ul.Mobile
FROM dbo.UserLogin AS ul
INNER JOIN dbo.Patron AS p ON p.UserLoginId = ul.UserLoginId
INNER JOIN valentinesDayMerchant AS m ON (m.MerchantId = ul.ReferringMerchantId OR m.MerchantId IN (SELECT pml.MerchantId FROM dbo.PatronMerchantLink AS pml WHERE pml.PatronId = p.PatronId AND ISNULL(pml.IsBanned, 0) = 0))
LEFT JOIN (
SELECT mg.MerchantGroupId, mg.MerchantGroupName, groupHost.HostName [GroupHostName]
FROM dbo.MerchantGroup AS mg
INNER JOIN dbo.Merchant AS parent ON parent.MerchantId = mg.ParentMerchantId
INNER JOIN dbo.HttpHostName AS groupHost ON groupHost.MerchantID = parent.MerchantId AND groupHost.Priority = 0
) mGroup ON mGroup.MerchantGroupId = m.MerchantGroupId
LEFT JOIN (
SELECT po.PatronId, MAX(po.OrderDateTime) [LastOrder]
FROM dbo.PatronsOrder AS po
GROUP BY po.PatronId
) orders ON orders.PatronId = p.PatronId
INNER JOIN dbo.HttpHostName AS hhn ON hhn.MerchantID = m.MerchantId AND hhn.Priority = 1
WHERE ul.UserLoginId NOT IN (1,2,100,372) AND ul.UserStatus <> 'D' AND (
ISNULL(orders.LastOrder, '2000-01-01') > '2020-01-01' OR ul.RegistrationDate > '2022-01-01'
)
GROUP BY ul.UserLoginId, ul.FullName, ul.EmailAddress, ul.Mobile
HAVING COUNT(m.MerchantId) > 1
Methods I have tried include adding the merchant name to a group by and displaying the count of the customers, however this does not work as I cannot have anything related to the Merchant in the GROUP BY, or I wouldn't be able to use HAVING clause to identify the customers that have been to multiple merchants. I have also tried selecting all the merchants and counting the distinct customers which doesn't work as it takes into account all the customers, not specifically the customers that have been to multiple merchants only.

Count based on another count - MySQL

I have the following Set of tables and I want to get all the services, age ranges and genders and next to each three of them the count of recipients corresponding to them.
So far I came up with the statement below, and it works.
select s.service_name,s.service_id,g.gender_id,ag.age_range_id,count(r.recipient_id) Temp
from tbl_services s inner join tbl_gender g
inner join tbl_age_range ag
left join tbl_recipient_services rs on rs.service_id=s.service_id
left join tbl_recipient r on r.gender_id=g.gender_id and rs.recipient_id=r.recipient_id
and (year(sysdate())-year(r.recipient_birth_date) >= min_age and year(sysdate())-year(r.recipient_birth_date) <= max_age)
group by s.service_id,g.gander_id,ag.age_range_id
But I need to only count the recipients who have 2 or more services in the tbl_recipient_services and I'm stuck again.
Anyone has an idea to fix that?
First, your query would be clearer with cross joins:
select s.service_name, s.service_id, g.gender_id, ag.age_range_id,
count(r.recipient_id) as all_recipients
from tbl_services s cross join
tbl_gender g cross join
tbl_age_range ag left join
tbl_recipient_services rs
on rs.service_id = s.service_id left join
tbl_recipient r
on r.gender_id = g.gender_id and
rs.recipient_id = r.recipient_id and
year(sysdate()) - year(r.recipient_birth_date) >= min_age and
year(sysdate()) - year(r.recipient_birth_date) <= max_age)
group by s.service_id, g.gender_id, ag.age_range_id;
This returns all combinations of service/gender/age even if there are no matches.
Let's add another column. To do so, we'll use a subquery to count the number of services per recipient and then include that in the count:
select s.service_name, s.service_id, g.gender_id, ag.age_range_id,
count(r.recipient_id) as num_recipients,
sum(rs2.num_services >= 2) as num_recipients_2plus
from tbl_services s cross join
tbl_gender g cross join
tbl_age_range ag left join
tbl_recipient_services rs
on rs.service_id = s.service_id left join
tbl_recipient r
on r.gender_id = g.gender_id and
rs.recipient_id = r.recipient_id and
year(sysdate()) - year(r.recipient_birth_date) >= min_age and
year(sysdate()) - year(r.recipient_birth_date) <= max_age) left join
(select rs2.recipient_id, count(*) as num_services
from tbl_recipient_services rs2
group by rs2.recipient_id
) rs2
on rs2.recipient_id = r.recipient_id
group by s.service_id, g.gender_id, ag.age_range_id;
If you don't want the additional column, you can change the left joins to inner joins and remove the count(r.recipient_id).

SQL Query - Count inside GROUP BY no result

I want to count the patient diagnosis per municipality and consultation per municipality:
so it should be:
diagnosis per municipality + consultation per municipality
SELECT COUNT(consultations.id) +
(SELECT COUNT(patientdiagnosis.id)
FROM consultations
LEFT JOIN patientdiagnosis
ON patientdiagnosis.consultation_id = consultations.id
LEFT JOIN patients
ON consultations.patient_id = patients.id
LEFT JOIN rcitymun
ON patients.municipality = rcitymun.citycode
/*GROUP BY PER MUNICIPALITY SHOULD BE HERE*/
) as encounters, rcitymun.cityname
FROM consultations
LEFT JOIN patients
ON consultations.patient_id = patients.id
LEFT JOIN rcitymun
ON patients.municipality = rcitymun.citycode
GROUP BY patients.municipality;
current output:
encounters municipality
10323 BATAC
10423 NUEVA ERA
the encounter data is huge because it's counting all of the diagnosis instead of per municipality
what i want is to count the diagnosis per municipality.
desired output is something like this:
encounters municipality
105 BATAC
70 NUEVA ERA
It may be possible to reduce this by one subquery, but often it is best to start with independently grouped subqueries.
SELECT
rcitymun.cityname
, SUM(c.consult_count) consult_count
, SUM(d.diag_count) diag_count
FROM patients
INNER JOIN rcitymun ON patients.municipality = rcitymun.citycode
LEFT JOIN (
SELECT
consultations.patient_id
, COUNT(*) consult_count
FROM consultations
GROUP BY
consultations.patient_id
) c ON patients.id = c.patient_id
LEFT JOIN (
SELECT
consultations.patient_id
, COUNT(*) diag_count
FROM consultations
INNER JOIN patientdiagnosis ON patientdiagnosis.consultation_id = consultations.id
GROUP BY
consultations.patient_id
) d ON patients.id = d.patient_id
GROUP BY
rcitymun.cityname

How to deduct in group by query in sum in access

I have query like this ::
SELECT account.AccountNumber, account.NAME, Sum(agro.price * agro.qty) AS Expr1
FROM ((account
INNER JOIN data ON account.AccountNumber = data.acno)
INNER JOIN agro ON agro.BillNo = data.BillNo)
WHERE data.db='true'
GROUP BY account.AccountNumber, account.NAME;
I want to deduct another groupby query output in to Sum(agro.price * agro.qty) this
the another group by query is SELECT Sum(rs),acno
FROM jma group by acno;
i want to deduct Sum(agro.price * agro.qty)-Sum(rs) how its work please help me solve this
If I am understanding you correctly the following query may work for you:
SELECT subQ.AccountNumber, subQ.NAME, (subQ.subSum - jmaSum.jSum) AS FinalSum
FROM
(
SELECT a.AccountNumber, a.NAME, Sum(ag.price * ag.qty) AS subSum
FROM (account AS a
INNER JOIN data AS d ON a.AccountNumber = d.acno)
INNER JOIN agro AS ag ON ag.BillNo = d.BillNo
WHERE d.db = 'true'
GROUP BY a.AccountNumber, a.NAME
) AS subQ
LEFT JOIN
(
SELECT Sum(j.rs) AS jSum, j.acno
FROM jma AS j
GROUP BY j.acno
) AS jmaSum ON subQ.AccountNumber = jmaSum.acno

Trying to add one last SUM() column to my query in SQL Server 2008

I have the first query which is producing correct results. What I need is I need to add the sum of values as a last column grouped by surveyid. I can't insert Sum(c.value) into the first query because it is an aggregate function. I have the correct query as my second query below. I know there's pivot functionality but not sure if it can be used here. I do realize that there will be repetition but that's okay.
'first query
SELECT
A.PATIENTID, B.STUDENTNUMBER, c.surveyid,
convert(varchar, A.CreatedDate, 107),
C.QuestionID, C.Value, D.Question
FROM
dbo.Survey A, dbo.Patient B, [dbo].[SurveyQuestionAnswer] C, [dbo].[LookupQuestions] D
WHERE
A.PATIENTID = B.ID
and c.SurveyID = A.ID
and c.QuestionID = d.ID
and c.questionid <> 10
ORDER BY
A.PATIENTID
'second query
select
c.surveyid,SUM(c.value) as scores
from
dbo.SurveyQuestionAnswer c
group by
c.SurveyID
order by
SurveyID '---not important
You can use SUM if you add the OVER clause. In this case:
SELECT
A.PATIENTID, B.STUDENTNUMBER, c.surveyid,
convert(varchar, A.CreatedDate, 107),
C.QuestionID, C.Value, D.Question,
SUM(c.Value) OVER(PARTITION BY c.surveyid) scores
FROM
dbo.Survey A
INNER JOIN dbo.Patient B
ON A.PATIENTID = B.ID
INNER JOIN [dbo].[SurveyQuestionAnswer] C
ON c.SurveyID = A.ID
INNER JOIN [dbo].[LookupQuestions] D
ON c.QuestionID = d.ID
WHERE
c.questionid <> 10
ORDER BY
A.PATIENTID
You could use something like this:
SELECT
s.PATIENTID, p.STUDENTNUMBER, sqa.surveyid,
CONVERT(varchar, s.CreatedDate, 107),
sqa.QuestionID, sqa.Value, lq.Question,
Scores = (SELECT SUM(Value) FROM dbo.SurveyQuestionAnswer s2 WHERE s2.SurveyID = s.ID)
FROM
dbo.Survey s
INNER JOIN
dbo.Patient p ON s.PatientID = p.ID
INNER JOIN
[dbo].[SurveyQuestionAnswer] sqa ON sqa.SurveyID = s.ID
INNER JOIN
[dbo].[LookupQuestions] lq ON sqa.QuestionID = lq.ID
WHERE
sqa.questionid <> 10
ORDER BY
s.PATIENTID
By having a subquery with the SUM(...) you should be able to get that sum as a single value and you don't need to use any grouping function