I don't think you can assume the order in SQL without using a temporary table.
To be sure I would add sequence number fields to the CHARGE record with 1,2,3,4 then sort by that. You might try nesting in parenthesis to see if that will force the order of evaluation but I am not sure that is guaranteed to work for all time (if it even does).
If you have the ability to adjust the class definitions remove the ICD9 codes from the CHARGE class to their own and replace them with a one to many reference. Then you can both allow for more (or less) than 4 codes and control the order explicitly with a sequence field or implicitly by knowing that Cache always increments the ID when you insert a new record and sort by ID.
If you can't change the charge record then you may have to write a stored procedure that returns the needed result set. I have not done it but there are instructions in the documentation if you search 'Defining and Using Stored Procedures'. Then from SQL you can just CALL your stored procedure.
In any event here is some dummy classes I mocked up using a reference to a ICD9List Class.
s tSC=##class(ICD9).TestData()
k idCharge
s tSC=##class(Charge).AddCharge("007.3",1,.idCharge)
s tSC=##class(Charge).AddCharge("002.0",2,.idCharge)
s tSC=##class(Charge).AddCharge("001",3,.idCharge)
s tSC=##class(Charge).AddCharge("002.9",4,.idCharge)
k idCharge
s tSC=##class(Charge).AddCharge("002.0",1,.idCharge)
s tSC=##class(Charge).AddCharge("002.9",2,.idCharge)
s tSC=##class(Charge).AddCharge("007.3",3,.idCharge)
s tSC=##class(Charge).AddCharge("001",4,.idCharge)
Then the SQL is:
Select refICD9->ID,refICD9->Code,refICD9->ShortDescription From ICD9List
Where refCharge=8
Order By Sequence
Class User.Charge Extends (%Persistent, %XML.Adaptor)
{
Relationship refICD9List As User.ICD9List [ Cardinality = many, Inverse = refCharge ];
Property Comment As %String;
ClassMethod AddCharge(Code As %String, Sequence As %Integer, ByRef idCharge As Charge) As %Integer
{
If '$D(idCharge) {
Set objCharge=##class(Charge).%New()
Set objCharge.Comment="Auto Created"
Do objCharge.%Save()
Set idCharge=objCharge.%Id()
}
&sql(Select ID Into :idICD9 From ICD9 Where Code=:Code)
If SQLCODE=100 Quit $$$ERROR($$$GeneralError,"ICD9 Code "_Code_" missing")
If SQLCODE'=0 Quit $$$ERROR($$$GeneralError,"Unexpected SQL Error (select) "_SQLCODE)
Set objICD9List=##class(ICD9List).%New()
Set objICD9List.Code=Code
Set objICD9List.Sequence=Sequence
Set objICD9List.refCharge=##class(Charge).%OpenId(idCharge)
Set objICD9List.refICD9=##class(ICD9).%OpenId(idICD9)
Do objICD9List.%Save()
Quit $$$OK
}
}
Class User.ICD9 Extends (%Persistent, %XML.Adaptor)
{
Property Code As %String [ Required ];
Property ShortDescription As %String [ Required ];
Index CodeIndex On Code [ Unique ];
Relationship refICD9List As User.ICD9List [ Cardinality = many, Inverse = refICD9 ];
ClassMethod AddCode(Code As %String, ShortDescription As %String) As %Status
{
Set objICD9=##class(ICD9).%New()
Set objICD9.Code=Code
Set objICD9.ShortDescription=ShortDescription
Do objICD9.%Save()
Quit $$$OK
}
ClassMethod TestData() As %Status
{
Set tSC=##class(ICD9).AddCode("002.9","PARATYPHOID FEVER UNSPEC")
Set tSC=##class(ICD9).AddCode("007.3","INTESTINAL TRICHOMONIASIS")
Set tSC=##class(ICD9).AddCode("002.0","TYPHOID FEVER")
Set tSC=##class(ICD9).AddCode("001","CHOLERA*")
Quit $$$OK
}
}
Class User.ICD9List Extends (%Persistent, %XML.Adaptor)
{
Property Code As %String;
Property Sequence As %Integer;
Relationship refCharge As User.Charge [ Cardinality = one, Inverse = refICD9List ];
Index refChargeIndex On refCharge;
Relationship refICD9 As User.ICD9 [ Cardinality = one, Inverse = refICD9List ];
}
Lawrence
________________________________
http://www.linkedin.com/in/vneerav <
http://www.linkedin.com/in/vneerav>
------------------------------------------------------
Samuel Goldwyn <
http://www.brainyquote.com/quotes/authors/s/samuel_goldwyn.html> - "I'm willing to admit that I may not always be right, but I am never wrong."
--
You received this message because you are subscribed to the Google Groups "InterSystems: Ensemble in Healthcare Community" group.
To post to this group, send email to
Ensemble-in...@googlegroups.com
To unsubscribe from this group, send email to
Ensemble-in-Healt...@googlegroups.com
For more options, visit this group at
http://groups.google.com/group/Ensemble-in-Healthcare?hl=en <
http://groups.google.com/group/Ensemble-in-Healthcare?hl=en>