<?xml version="1.0" encoding="utf-8" ?>
<select>
  <selectstr>
SELECT ISNULL(u_factory_profitrate.deptid, v_temp_1.deptid) AS deptid
	,ISNULL(u_factory_profitrate.bednet_or_mattress, @kind) AS bednet_or_mattress
	,ISNULL(u_factory_profitrate.bednettypeid_mattresstypeid, v_temp_1.bednettypeid) AS bednettypeid_mattresstypeid
	,ISNULL(u_factory_profitrate.profitrate, 0) AS profitrate
	,u_factory_profitrate.createtime
	,u_factory_profitrate.createby
	,v_temp_1.deptname
	,v_temp_1.typename
FROM u_factory_profitrate
INNER JOIN (
	SELECT u_dept.deptid
		,v_temp.bednettypeid
		,u_dept.deptname
		,v_temp.typename
	FROM u_dept
	INNER JOIN (
		SELECT 1 AS kind
			,u_mattress_type.mattresstypeid AS bednettypeid
			,u_mattress_type.typename
		FROM u_mattress_type
		
		UNION ALL
		
		SELECT 0 AS kind
			,u_bednet_type.bednettypeid
			,u_bednet_type.typename
		FROM u_bednet_type
		) v_temp ON @kind = v_temp.kind
	) v_temp_1 ON u_factory_profitrate.deptid = v_temp_1.deptid
	AND u_factory_profitrate.bednettypeid_mattresstypeid = v_temp_1.bednettypeid
  </selectstr>
  <where>
	<when>
		ISNULL(u_factory_profitrate.bednet_or_mattress,@kind) = @kind
	</when>
	<when notnull="@arg_deptid">
		u_factory_profitrate.deptid = @arg_deptid
	</when>
	<when notnull="@arg_bednettypeid_mattresstypeid">
		u_factory_profitrate.bednettypeid_mattresstypeid = @arg_bednettypeid_mattresstypeid
	</when>
  </where>
  <orderstr>
  </orderstr>
  <displayfields>
  </displayfields>
</select>