Mistři v programování na #hovnokod

SQL #372

SELECT A.Id AS AppraiserId, 
	(SELECT A2.FieldValueInt AS SubcontractorId
	FROM sm_forms_FieldValues A2
	WHERE 
		A2.InstanceId = (
			SELECT 	FormInstanceId
			FROM	sm_genericdatabase_Instances
			WHERE	ModuleId=1736 AND Title='Appraisers'
			)
		AND 
		A2.FieldId = (
			SELECT Id FROM sm_forms_Fields 
			WHERE InstanceId= A2.InstanceId 
			AND Title = 'Subcontractor'	
			)
		AND
		A2.ValueId=A.Id
	) AS SubcontractorId,
	
	(SELECT A3.FieldValueInt
	FROM sm_forms_FieldValues A3
	WHERE 
		InstanceId = (
			SELECT 	FormInstanceId
			FROM	sm_genericdatabase_Instances
			WHERE	ModuleId=1736 AND Title='Appraisers'
			)
		AND 
		FieldId = (
			SELECT Id FROM sm_forms_Fields 
			WHERE InstanceId= A3.InstanceId 
			AND Title = 'Heavy Equipment'	
			)
		AND
		A3.ValueId= A.Id
	) AS HeavyEquipment,
	
	(SELECT A3.FieldValueInt
	FROM sm_forms_FieldValues A3
	WHERE 
		InstanceId = (
			SELECT 	FormInstanceId
			FROM	sm_genericdatabase_Instances
			WHERE	ModuleId=1736 AND Title='Appraisers'
			)
		AND 
		FieldId = (
			SELECT Id FROM sm_forms_Fields 
			WHERE InstanceId= A3.InstanceId 
			AND Title = 'Name'	
			)
		AND
		A3.ValueId= A.Id
	) AS Name
	
	
	FROM sm_forms_Values A
	where InstanceId = (
			SELECT 	FormInstanceId
			FROM	sm_genericdatabase_Instances
			WHERE	ModuleId=1736 AND Title='Appraisers'
			)
	ORDER BY 2

HovnoKod,