Jump to content
View in the app

A better way to browse. Learn more.

FMForums.com

A full-screen app on your home screen with push notifications, badges and more.

To install this app on iOS and iPadOS
  1. Tap the Share icon in Safari
  2. Scroll the menu and tap Add to Home Screen.
  3. Tap Add in the top-right corner.
To install this app on Android
  1. Tap the 3-dot menu (⋮) in the top-right corner of the browser.
  2. Tap Add to Home screen or Install app.
  3. Confirm by tapping Install.

Execute SQL, Select Count Subquery

Featured Replies

  • Newbies

 Hello, I have developed a FM database over the last 6 years and this is the first time I have not been able to solve a problem searching the forums. 

Yet seemingly simple, I have not been able to create a working SELECT COUNT () statement (or alternative)  to count the subquery listed below. There is many references to MySQL; however, they fail with FM execute SQL.

Any help would be appreciated      

Let ( [

hospital = Dashboard::g~SQL~Hospital ;
year = Dashboard::g~SQL~Year ;
month = Dashboard::g~SQL~Month ;
$$YearList = Dashboard::g~SQL~Year ] ;

ExecuteSQL ( "SELECT COUNT (b.cEncURN)
	FROM Encounters b
	WHERE b.cInitialHospitalNameShort = ? 
	AND b.InitialCategory = 'Code Blue' 
	AND b.cYear = ? 
	AND b.cMonthName = ? 
	GROUP BY b.cEncURN 
	HAVING COUNT ( b.cEncURN ) > 1"

	; "" ;  ; hospital ; year ; month ) )
    
 Result:
 2
 3
 3
 2
 2
 2
 Wanted Result
 6

 

drop the SELECT COUNT and wrap the result in ValueCount().

  • Author
  • Newbies
Let ( [

hospital = Dashboard::g~SQL~Hospital ;
year = Dashboard::g~SQL~Year ;
month = Dashboard::g~SQL~Month ;
$$YearList = Dashboard::g~SQL~Year ] ;

ExecuteSQL ( 
	"SELECT 
		(SELECT COUNT (b.cEncURN) 
		FROM Encounters b 
		WHERE (Year(b.EncStart)*100+Month(EncStart)) = a.cYearMonth 
		AND b.cInitialHospitalNameShort = ?
		AND b.InitialCategory = 'Code Blue'
		GROUP BY b.cEncURN
		HAVING COUNT (*) > 1)
	FROM Months a 
	WHERE a.cYear = ? 
	GROUP BY a.cYearMonth" 

	; "" ; "" ; hospital ; year ) )

Thanks for the reply bcooney,

I have investigated that option and it works well. However, it can only be used to count an individual month. Unfortunately, the query is used to populate the y value in a chart giving me the totals of each month with 0 values if there is no values for that month (see full query above). 

Obviously the two (2) GROUP BY and HAVING COUNT are my problem. If I take the subquery GROUP BY and HAVING COUNT statements out the query works well. However, only gives me the total Code Blues per month

I am trying to count patients (URN) that have multiple medical emergencies (Code Blue) in a year listed by month with 0 value if null

Edited by RNPaul

  • Author
  • Newbies

Thanks bcooney, from a quick look, this might solve my problem.

Create an account or sign in to comment

Important Information

By using this site, you agree to our Terms of Use.

Configure browser push notifications

Chrome (Android)
  1. Tap the lock icon next to the address bar.
  2. Tap Permissions → Notifications.
  3. Adjust your preference.
Chrome (Desktop)
  1. Click the padlock icon in the address bar.
  2. Select Site settings.
  3. Find Notifications and adjust your preference.