如何将多个命名列组合为一行和它的总和

时间:2015-07-30 13:13:37

标签: sql sql-server sql-server-2008 sql-server-2005

我需要在SQL中使用查询来组合同一个表中的多个行。

L_Life_Amount  M_LifeAmount    L_Health_Amount    M_Health_amount
100                 200             300                 400
200                 300             400                 600
300                 400             500                 700

我需要带下面的结果集

InsuranceType  L_Amount     M_Amount    L_amount-M_amount   
Life amount    600          900         -300
Health amount   1200        1700        -500

Total           1800        2600        -800

在这里,我需要sum L_life_Amount(L_Amount)的{​​{1}}和sum的值,并将其显示为生命金额

我需要对M_Life_amount(M_Amount)sum L_Helath_Amount(L_Amount)进行sum,并将其显示为健康金额

最后,我在M_Health_Amount(M_Amount)之间制作差异,并总计它。

3 个答案:

答案 0 :(得分:1)

使用Cross Applytable valued constructor 取消隐藏数据然后进行数学计算

像这样的东西

;with cte as
(
SELECT insurancetype, 
       l_amount=Sum(l_amount), 
       m_amount=Sum(m_amount), 
       [l_amount-m_amount]=Sum([l_amount-m_amount]) 
FROM   yourtable 
       CROSS apply (VALUES (l_life_amount,m_lifeamount,
                            l_life_amount - m_lifeamount,'Life amount'),
                           (l_health_amount,m_health_amount,
                            l_health_amount - m_health_amount,'Health amount')) cs(l_amount, m_amount, [L_amount-M_amount],insurancetype) 
GROUP  BY insurancetype 
)
select insurancetype,
       l_amount,
       m_amount,
       [l_amount-m_amount]
From CTE
UNION ALL
Select 'Total',
       sum(l_amount),
       sum(m_amount),
       sum([l_amount-m_amount])
From CTE

SQLFIDDLE DEMO

答案 1 :(得分:0)

CREATE VIEW Life AS
SELECT 'Life amount' AS InsuranceType,
       sum(L_Life_Amount) AS L_Amount,
       sum(M_Life_Amount) AS M_Amount,
       sum(L_Life_Amount) - sum(M_Life_Amount) AS L_amount-M_Amount
FROM <tablename>

CREATE VIEW Health AS
SELECT 'Health amount' AS InsuranceType,
       sum(L_Health_Amount) AS L_Amount,
       sum(M_Health_Amount) AS M_Amount,
       sum(L_Health_Amount) - sum(M_Health_Amount) AS L_amount-M_Amount
FROM <tablename>

CREATE VIEW Total AS
SELECT 'Total' AS InsuranceType,
       sum(L_Amount),
       sum(M_Amount),
       sum(L_Amount-M_Amount),
FROM Life UNION Health

SELECT * FROM Life UNION Health UNION Total

(不确定语法是否正确......)

答案 2 :(得分:0)

我们可以借助CTECTE

中的联合来获得此解决方案
declare @temp table

(
L_Life_Amount int,
M_LifeAmount    int,
L_Health_Amount int,
M_Health_amount int
);


insert into @temp values (100, 200, 300, 400)
insert into @temp values (200, 300, 400, 600)
insert into @temp values (300, 400, 500, 700)


;with cte as(
select 'Life amount' as InsuranceType , sum(L_Life_Amount) L_Amount ,SUM(M_LifeAmount) M_Amount,sum(L_Life_Amount)- SUM(M_LifeAmount) as [L_amount-M_amount]  from @temp
union
select 'Health amount' as InsuranceType , sum(L_Health_Amount) L_Amount ,SUM(M_Health_amount) M_Amount, sum(L_Health_Amount)-SUM(M_Health_amount)  from @temp) 

select * from cte
union
select 'Total' , sum(L_Amount), sum(M_Amount),sum([L_amount-M_amount]) from cte