select
t.检修单位,
t.日期,
count(*) as 未结工单
from (
SELECT
case when a.field0045 in('条件1','条件2') then '组合' else a.field0045 end AS 单位,
LEFT(convert(char(10),a.field0012,126),7) AS 日期
FROM dbo.test01 a
left JOIN dbo.test02 b on a.field0020 = CAST(b.ID as nvarchar(255))
WHERE 1=1
AND finishedflag = 0
AND state = 1
AND a.field0010 IN(SELECT NAME FROM dbo.test02 where LZ = '001' AND LEFT(CODE,1 ) IN ('F'))
AND b.NAME IN(SELECT NAME FROM dbo.test02 where LZ = '001' AND LEFT(CODE,1 ) IN ('F'))
) t
GROUP BY t.单位,t.日期