I’ve given the full SQL, so a little noisy I’m afraid.
SELECT "customerforename",
"customersurname",
"customertitle",
"customerhometelephone",
"customerworktelephone",
"customermobiletelephone",
"customeremail",
"customercategory",
"customerdateofbirth",
"customeraddressline1",
"customeraddressline2",
"customeraddressline3",
"customeraddressline4",
"customeraddressline5",
"customerpostcode",
"locationname",
"practitionertitle",
"practitionerforename",
"practitionersurname",
"caseid",
"casecustomerid",
"casepractitionerid",
"casecreatedbyuserid",
"casedischargedbyuserid",
"casemedicalcontactid",
"casedateofadmission",
"casereferralreason",
"casediagnosis",
"casediagnosiscode",
"casedateofdischarge",
"casepatientoutcome",
"casepractitioneroutcome",
"casedischargestatus",
"casenumberofappointments",
"casereference",
"caseadmissioncode",
"casereviewdate",
"casereferraldate",
"casebodysite",
"casebodyside",
"casetimepresent",
"casetimepresentunit",
"caseinjurycause",
"casepreexistingconditions",
"caseadmissionpainscale",
"caseadmissionletterdate",
"casedischargepainscale",
"casedischargeletterdate",
"caseclinicnotes",
"caseagreedfunctionaloutcome",
"caseclosed",
"casecloseduser",
"casecloseddatetime",
"casepatienttype",
"caseauditnotes",
"caseauditdate",
"casestandardpathwayfollowed",
"caselocationid",
"caseofinterest",
"casefirstappointmentid",
"caseservicetypeid",
"caselinkedcaseid",
"casestatus",
"caseautosaved",
"casedata",
"caseadmissiontemplateid",
"casedischargetemplateid",
"caseadmissionhighlight",
"casedischargehighlight",
"casetype",
(SELECT Count(appointmentid)
FROM tblappointment appointment
WHERE appointment.appointmentcaseid = caseid) AS "appointmentCount",
(SELECT Count(alertid)
FROM tblalert alert
WHERE alert.alertobjectid = customerid
AND ( alert.alertdiscriminatortype = 'P' )
AND alert.alertcomplete = 0) AS "alerts",
(SELECT Count(*)
FROM tblappointment
LEFT OUTER JOIN tbltreatment
ON treatmentappointmentid = appointmentid
AND treatmentcaseid = appointmentcaseid
WHERE appointmentstartdatetime < Getdate()
AND treatmentid IS NULL
AND appointmentcaseid = tblcase.caseid) AS
"appointmentsWithoutConsultations",
(SELECT Count(*)
FROM tbltreatment
WHERE treatmentcaseid = caseid
AND ( Isnull(treatmentclosed, 0) <> 1
AND treatmentdatetime < Getdate() )) AS
"unclosedConsultations",
(SELECT TOP 1 treatmentdatetime
FROM tbltreatment
WHERE treatmentcaseid = caseid
ORDER BY treatmentdatetime DESC) AS
"LastConsultationDate",
(SELECT statusText = CASE casestatus
WHEN 1 THEN 'Awaiting First Appointment'
WHEN 3 THEN 'In Treatment'
WHEN 7 THEN 'On Hold'
WHEN 8 THEN 'Discharged'
WHEN 12 THEN 'Closed - Awaiting Review'
ELSE 'Closed and Reviewed'
END) AS "StatusText",
COALESCE((SELECT
Stuff((SELECT N'; ' + ( authcode + ': '
+ Cast(authmaxtreatments - authusedtreatments AS
VARCHAR
)
+ ' remaining' )
FROM tblauthcode
WHERE authcaseid = caseid
FOR xml
path(''), type).value('text()[1]', 'nvarchar(max)'), 1, 2, N'')), ' ')
AS "auth",
COALESCE((SELECT Stuff((SELECT N', ' + groupname
FROM tblcustomergroup
WHERE groupid IN(SELECT authgroupid
FROM tblauthcode
WHERE authcaseid = caseid)
FOR xml path(''), type).value('text()[1]',
'nvarchar(max)'), 1, 2
, N'')), ' ') AS "groupName",
COALESCE((SELECT Stuff((SELECT N', ' + Cast(groupid AS VARCHAR)
FROM tblcustomergroup
WHERE groupid IN(SELECT authgroupid
FROM tblauthcode
WHERE authcaseid = caseid)
FOR xml path(''), type).value('text()[1]',
'nvarchar(max)'), 1, 2
, N'')), ' ') AS "groupId"
FROM "tblcase"
LEFT OUTER JOIN "tblcustomer"
ON "tblcustomer"."customerid" = "tblcase"."casecustomerid"
LEFT OUTER JOIN "tbllocation"
ON "tbllocation"."locationid" = "tblcase"."caselocationid"
LEFT OUTER JOIN "tblpractitioner"
ON "tblpractitioner"."practitionerid" =
"tblcase"."casepractitionerid"
WHERE ( "caselocationid" IN ( 1, 2, 3, 4,
5, 6, 7, 8,
9, 10, 1040, 1041,
1042, 1043, 1044, 1045,
2045, 2046 )
AND "casepractitionerid" IN ( 5 )
AND "unclosedConsultations" > 2
)
And the class for the visit, slightly abbreviated…
namespace Services.Framework
{
public class BzSqlExpressionVisitor<T> : SqlServerExpressionVisitor<T>
{
private List<string> _subQueryColumns = new List<string>();
public virtual SqlExpressionVisitor<T> Subquery<TKey>(Expression<Func<T, TKey>> fields)
{
_subQueryColumns.Add(Visit(fields).ToString());
return this;
}
public override string ToSelectStatement()
{
var subQuery = _subQueryColumns.Join(", ");
if (!string.IsNullOrEmpty(subQuery))
SelectExpression = SelectExpression.Insert(SelectExpression.LastIndexOf("\nFROM"), ", " + _subQueryColumns.Join(", "));
/* unrelated stuff */
return selectString;
}
}