听起来您想将数据从列中转为行。如果是这种情况,那么您应该能够在 SQL中使用UNPIVOT函数。
查询的基本结构将是:
select col, value
from yourtable
unpivot
(
value
for col in (sumDeposits, sumWithdrawals, sumFees) -- your columns here
)unpiv;
如果您有不同数据类型的数据,则必须将所有列的数据转换为相同类型:
select col, value
from
(
select sumDeposits,
cast(sumWithdrawals as numeric(10, 2)) sumWithdrawals,
sumFees
from yourtable
) d
unpivot
(
value
for col in (sumDeposits, sumWithdrawals, sumFees)
) unpiv;
这种类型的查询与使用 aUNION ALL
对同一个表的多次调用完全相同:
select 'sumDeposits' col, sumDeposits as value
from yourtable
union all
select 'sumWithdrawals' col, sumWithdrawals as value
from yourtable
union all
select 'sumFees' col, sumFees as value
from yourtable