select 仓库, 卡片号, sum(入库金额+出库金额) as 总金额, sum(入库金额) as 入库金额, sum(出库金额) as 出库金额 from ( select 仓库,卡片号,sum(金额) as 入库金额, 0 as 出库金额 from A where 进出标志='+' group by 仓库,卡片号 union select 仓库,卡片号,0 as 入库金额, sum(金额) as 出库金额 from A where 进出标志='-' group by 仓库,卡片号 ) BB group by 仓库,卡片号
select 仓库, 卡片号, 进出标志, sum(金额) as 入库金额, sum(金额) as 出库金额, 入库金额+出库金额 group by rollup(仓库,卡片号,进出标志)
卡片号,
sum(入库金额+出库金额) as 总金额,
sum(入库金额) as 入库金额,
sum(出库金额) as 出库金额
from (
select 仓库,卡片号,sum(金额) as 入库金额, 0 as 出库金额 from A where 进出标志='+' group by 仓库,卡片号
union
select 仓库,卡片号,0 as 入库金额, sum(金额) as 出库金额 from A where 进出标志='-' group by 仓库,卡片号
) BB group by 仓库,卡片号
卡片号,
进出标志,
sum(金额) as 入库金额,
sum(金额) as 出库金额,
入库金额+出库金额
group by rollup(仓库,卡片号,进出标志)