SELECT
case when grouping(dm_gjdl.name)=1 then '总计' else Isnull(dm_gjdl.name, 'UNKNOWN') end AS dlmc,
case when flag='0' then '自有' when flag='1' then '转租' when dm_gjdl.name is Null then '' else '小计' end AS flag,
SUM(kc_dlkc.kchj) AS kchj,
SUM(kc_dlkc.zkkc) AS zkkc,
SUM(kc_dlkc.zykc) AS zykc,
SUM(kc_dlkc.zyin) AS zyin,
SUM(kc_dlkc.zyout) AS zyout
FROM kc_dlkc INNER JOIN
dm_gjdl ON kc_dlkc.dlid = dm_gjdl.id
GROUP BY dm_gjdl.name,flag WITH rollup