从现有查询中取消显示

时间:2014-02-18 10:13:12

标签: sql sql-server sql-server-2012 unpivot

我有以下查询:

select'Amount' as Amount,
('£'+ CAST(SUM(rc.[Fee Charge] +rc.[Fee Charge VAT] +rc.ExtraCharges+rc.ExtraChargesVAT+rc.OtherCharges+rc.OtherChargesVAT+rc.WaitingCharge+rc.[WaitingCharge VAT])AS nvarchar(50))) AS [CompletedTurnover],
('£'+ CAST(SUM(rin.[Fee Charge] +rin.[Fee Charge VAT] +rin.ExtraCharges+rin.ExtraChargesVAT+rin.OtherCharges+rin.OtherChargesVAT+rin.WaitingCharge+rin.[WaitingCharge VAT])AS nvarchar(50))) AS [In Progress Turnover],
('£'+ CAST(SUM(run.[Fee Charge] +run.[Fee Charge VAT] +run.ExtraCharges+rc.ExtraChargesVAT+run.OtherCharges+run.OtherChargesVAT+run.WaitingCharge+run.[WaitingCharge VAT])AS nvarchar(50))) AS [Unallocated Turnover],
123 as [Credit Note Value]


from tblreservation R 
left join tblreservation rc on R.ReservationsID = rc.reservationsid and rc.Completed = 1
left join tblreservation rin on R.reservationsid = rin.reservationsid and rin.InProgress = 1
left join tblreservation run on Run.ReservationsID = r.ReservationsID and run.completed = 0 and run.inprogress = 0

这会返回如下数据:

CompletedTurnover      In progress Turnover      Unallocated Turnover       Credit Note Value    
1202039920             23998858945               9384585845                 123

这是预期的。但是,我需要以下输出,并且我正在使用枢轴进行一些努力。

  Completed Turnover    1202039920             
  In Progress Turnover  23998858945               
  Unallocated Turnover  9384585845                 
  Credit Note Value     123

非常感谢任何帮助。

4 个答案:

答案 0 :(得分:4)

将列转换为行的过程实际上称为UNPIVOT。你可以用几种不同的方式做到这一点。

UNPIVOT:功能:

;with cte as
(
  select'Amount' as Amount,
    ('£'+ CAST(SUM(rc.[Fee Charge] +rc.[Fee Charge VAT] +rc.ExtraCharges+rc.ExtraChargesVAT+rc.OtherCharges+rc.OtherChargesVAT+rc.WaitingCharge+rc.[WaitingCharge VAT])AS nvarchar(50))) AS [CompletedTurnover],
    ('£'+ CAST(SUM(rin.[Fee Charge] +rin.[Fee Charge VAT] +rin.ExtraCharges+rin.ExtraChargesVAT+rin.OtherCharges+rin.OtherChargesVAT+rin.WaitingCharge+rin.[WaitingCharge VAT])AS nvarchar(50))) AS [In Progress Turnover],
    ('£'+ CAST(SUM(run.[Fee Charge] +run.[Fee Charge VAT] +run.ExtraCharges+rc.ExtraChargesVAT+run.OtherCharges+run.OtherChargesVAT+run.WaitingCharge+run.[WaitingCharge VAT])AS nvarchar(50))) AS [Unallocated Turnover],
    123 as [Credit Note Value]
  from tblreservation R 
  left join tblreservation rc 
    on R.ReservationsID = rc.reservationsid 
    and rc.Completed = 1
  left join tblreservation rin 
    on R.reservationsid = rin.reservationsid 
    and rin.InProgress = 1
  left join tblreservation run 
    on Run.ReservationsID = r.ReservationsID 
    and run.completed = 0 
    and run.inprogress = 0
)
select col, value
from cte 
unpivot
(
  value
  for col in (CompletedTurnover, [In Progress Turnover],
              [Unallocated Turnover], [Credit Note Value])
) u;

CROSS APPLY WITH VALUES:

;with cte as
(
  select'Amount' as Amount,
    ('£'+ CAST(SUM(rc.[Fee Charge] +rc.[Fee Charge VAT] +rc.ExtraCharges+rc.ExtraChargesVAT+rc.OtherCharges+rc.OtherChargesVAT+rc.WaitingCharge+rc.[WaitingCharge VAT])AS nvarchar(50))) AS [CompletedTurnover],
    ('£'+ CAST(SUM(rin.[Fee Charge] +rin.[Fee Charge VAT] +rin.ExtraCharges+rin.ExtraChargesVAT+rin.OtherCharges+rin.OtherChargesVAT+rin.WaitingCharge+rin.[WaitingCharge VAT])AS nvarchar(50))) AS [In Progress Turnover],
    ('£'+ CAST(SUM(run.[Fee Charge] +run.[Fee Charge VAT] +run.ExtraCharges+rc.ExtraChargesVAT+run.OtherCharges+run.OtherChargesVAT+run.WaitingCharge+run.[WaitingCharge VAT])AS nvarchar(50))) AS [Unallocated Turnover],
    123 as [Credit Note Value]
  from tblreservation R 
  left join tblreservation rc 
    on R.ReservationsID = rc.reservationsid 
    and rc.Completed = 1
  left join tblreservation rin 
    on R.reservationsid = rin.reservationsid 
    and rin.InProgress = 1
  left join tblreservation run 
    on Run.ReservationsID = r.ReservationsID 
    and run.completed = 0 
    and run.inprogress = 0
)
select col, value
from cte 
cross apply
(
  values
    ('CompletedTurnover', CompletedTurnover),
    ('In Progress Turnover', [In Progress Turnover]),
    ('Unallocated Turnover', [Unallocated Turnover]),
    ('Credit Note Value', [Credit Note Value])
) c (col, value)

答案 1 :(得分:1)

您可以使用联合来获得所需的结果:

select 'Completed turnover' Description, 
        (  '£'+ CAST(SUM(rc.[Fee Charge] +
                     rc.[Fee Charge VAT] + 
                     rc.ExtraCharges+
                     rc.ExtraChargesVAT+
                     rc.OtherCharges+
                     rc.OtherChargesVAT+
                     rc.WaitingCharge+
                     rc.[WaitingCharge VAT]
                   )AS nvarchar(50))) value
from ....

union all

select 'In Progress turnover', .....
from ....

union all

select 'Unallocated Turnover', .....
from ....

您可能希望结合使用CTE

答案 2 :(得分:0)

试试这个,,,,,

select'Amount' as Amount
union all
select
('£'+ CAST(SUM(rc.[Fee Charge] +rc.[Fee Charge VAT] +rc.ExtraCharges+rc.ExtraChargesVAT+rc.OtherCharges+rc.OtherChargesVAT+rc.WaitingCharge+rc.[WaitingCharge VAT])AS nvarchar(50))) AS [CompletedTurnover]
from tblreservation rc where Completed=1
union all

select
('£'+ CAST(SUM(rin.[Fee Charge] +rin.[Fee Charge VAT] +rin.ExtraCharges+rin.ExtraChargesVAT+rin.OtherCharges+rin.OtherChargesVAT+rin.WaitingCharge+rin.[WaitingCharge VAT])AS nvarchar(50))) AS [In Progress Turnover]
from tblreservation rin  where InProgress=1
union all
select
('£'+ CAST(SUM(run.[Fee Charge] +run.[Fee Charge VAT] +run.ExtraCharges+rc.ExtraChargesVAT+run.OtherCharges+run.OtherChargesVAT+run.WaitingCharge+run.[WaitingCharge VAT])AS nvarchar(50))) AS [Unallocated Turnover]
from tblreservation run where InProgress=0 and Completed=0
union all
 select 123 as [Credit Note Value]

答案 3 :(得分:0)

您可能不需要联接,因为聚合结果来自同一个表。您可以改为使用条件聚合

SELECT
  SUM(CASE WHEN Completed = 1 THEN
    [Fee Charge] + [Fee Charge VAT] + ExtraCharges + ExtraChargesVAT +
    OtherCharges + OtherChargesVAT + WaitingCharge + [WaitingCharge VAT]
  END) AS [Completed Turnover],
  SUM(CASE WHEN InProgress = 1 THEN
    ...
  END) AS [In-progress Turnover],
  SUM(CASE WHEN Completed = 0 AND InProgress = 0 THEN
    ...
  END) AS [Unallocated Turnover],
  123 AS [Credit Note Value]
FROM tblreservation

然后使用@ bluefeet的答案中的任何一种方法取消结果:

SELECT
  Caption,
  Value
FROM (
  SELECT
    SUM(CASE WHEN Completed = 1 THEN
      [Fee Charge] + [Fee Charge VAT] + ExtraCharges + ExtraChargesVAT +
      OtherCharges + OtherChargesVAT + WaitingCharge + [WaitingCharge VAT]
    END) AS [Completed Turnover],
    SUM(CASE WHEN InProgress = 1 THEN
      ...
    END) AS [In-progress Turnover],
    SUM(CASE WHEN Completed = 0 AND InProgress = 0 THEN
      ...
    END) AS [Unallocated Turnover],
    123 AS [Credit Note Value]
  FROM tblreservation
) AS s
UNPIVOT (
  Value FOR Caption IN (
    [Completed Turnover], [In-progress Turnover],
    [Unallocated Turnover], [Credit Note Value]
  )
) AS u
;

另一方面,如果InProgressCompleted始终为1或0并且它们具有一致的值,因此它们不能都是1,您可以将结果分组如下:

SELECT
  Completed,
  InProgress,
  SUM(
    [Fee Charge] + [Fee Charge VAT] + ExtraCharges + ExtraChargesVAT +
    OtherCharges + OtherChargesVAT + WaitingCharge + [WaitingCharge VAT]
  ) AS Value
FROM tblreservation
GROUP BY
  Completed,
  InProgress

下一步是将CompletedInProgress的组合转换为适当的标题。一种方法是使用CASE:

SELECT
  CASE
    WHEN Completed  = 1 THEN 'Completed Turnover'
    WHEN InProgress = 1 THEN 'In-progress Turnover'
    ELSE                     'Unallocated Turnover'
  END AS Caption
  SUM(
    [Fee Charge] + [Fee Charge VAT] + ExtraCharges + ExtraChargesVAT +
    OtherCharges + OtherChargesVAT + WaitingCharge + [WaitingCharge VAT]
  ) AS Value
FROM tblreservation
GROUP BY
  Completed,
  InProgress

然后你只需要用UNION ALL添加常量Credit Note Value

SELECT
  CASE
    WHEN Completed  = 1 THEN 'Completed Turnover'
    WHEN InProgress = 1 THEN 'In-progress Turnover'
    ELSE                     'Unallocated Turnover'
  END AS Caption
  SUM(
    [Fee Charge] + [Fee Charge VAT] + ExtraCharges + ExtraChargesVAT +
    OtherCharges + OtherChargesVAT + WaitingCharge + [WaitingCharge VAT]
  ) AS Value
FROM tblreservation
GROUP BY
  Completed,
  InProgress

UNION ALL

SELECT
  'Credit Note Value',
  123
;