简体   繁体   中英

SQL count records in from multiple tables Group by UsedID

I have some forms. It contains different usedId as a foreign key. One user has multiple forms. Now I want to see one user submitted how many forms.

I want the output:

Login Id, FormName, NumberOfForms

Shakil -> BurnOut -> 10

Shakil -> CIP -> 8

Shakil -> heating -> 20

But at first, I need the output. then decoration.

I'm trying multiple ways to solve it.

select  COUNT(*) as NumberOfForms  from Client_BurnOuts  b
inner join PatientPortalLogins l on l.PatientApplicationId = b.PatientApplicationId
where l.PatientApplicationId = 10
union all 

select COUNT(*) as NumberOfForms  from Client_EmergencyAssistances  b
inner join PatientPortalLogins l on l.PatientApplicationId = b.PatientApplicationId
where l.PatientApplicationId = 10
union all 

select COUNT(*) as NumberOfForms  from Client_DukeEnergyFoundations  b
inner join PatientPortalLogins l on l.PatientApplicationId = b.PatientApplicationId
where l.PatientApplicationId = 10
union all
 
select COUNT(*) as NumberOfForms  from Client_CIPEnergyCrisises  b
inner join PatientPortalLogins l on l.PatientApplicationId = b.PatientApplicationId
where l.PatientApplicationId = 10
;

Another ways

SELECT l.LoginID, 
COUNT(cb.PatientApplicationId) as BurnOuts  ,
COUNT(ea.PatientApplicationId) as EmergencyAssistances, 
COUNT(du.PatientApplicationId) as DukeEnergyFoundations, 
COUNT(cip.PatientApplicationId) as CIP
FROM PatientPortalLogins l

inner join Client_CIPEnergyCrisises cip ON cip.PatientApplicationId = l.PatientApplicationId
inner join Client_DukeEnergyFoundations du ON du.PatientApplicationId = l.PatientApplicationId 
inner join Client_EmergencyAssistances ea ON ea.PatientApplicationId = l.PatientApplicationId 
inner join Client_BurnOuts cb ON cb.PatientApplicationId = l.PatientApplicationId 
where l.PatientApplicationId = 10
GROUP BY l.LoginID, cip.PatientApplicationId, cb.PatientApplicationId;

But I can't get the proper results. I have a total five tables, PatientPortalLogins table contains only the user name, usedId..... And four tables contain the different values,
I just want to see which user submit how many pages. Example used1 submit 2 Client_CIPEnergyCrisises forms, 1 EmergencyAssistances forms, 5 DukeEnergyFoundations forms

Finally, I can solve my problem. If you have multiple tables then we can use them.

select l.LoginID, 'Client_BurnOuts' as Client_BurnOuts,  COUNT(*) as NumberOfForms  
from Client_BurnOuts  b
inner join PatientPortalLogins l on l.PatientApplicationId = b.PatientApplicationId
where l.PatientApplicationId = 10
GROUP BY l.LoginID
union all 

select l.LoginID, 'Client_EmergencyAssistances' as Client_EmergencyAssistances,  COUNT(*) as NumberOfForms  
from Client_EmergencyAssistances  b
inner join PatientPortalLogins l on l.PatientApplicationId = b.PatientApplicationId
where l.PatientApplicationId = 10
GROUP BY l.LoginID
union all 

select l.LoginID, 'Client_DukeEnergyFoundations' as Client_DukeEnergyFoundations, COUNT(*) as NumberOfForms  
from Client_DukeEnergyFoundations  b
inner join PatientPortalLogins l on l.PatientApplicationId = b.PatientApplicationId
where l.PatientApplicationId = 10
GROUP BY l.LoginID
union all 

select l.LoginID, 'Client_CIPEnergyCrisises' as Client_CIPEnergyCrisises, COUNT(*) as NumberOfForms  
from Client_CIPEnergyCrisises  b
inner join PatientPortalLogins l on l.PatientApplicationId = b.PatientApplicationId
where l.PatientApplicationId = 10
GROUP BY l.LoginID
;

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM