Question

I have the following query:

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

This returns data like so:

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

This is as expected. However, I need the following output and I'm struggling a bit using pivots.

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

Any help would be greatly appreciated.

Was it helpful?

Solution 2

You can use a union to get the results you need:

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 ....

you probably want to look at using in conjunction with a CTE

OTHER TIPS

This process to convert columns into rows is actually called an UNPIVOT. You can do this a few different ways.

UNPIVOT: function:

;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)

Try this,,,,,

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]

You probably don't need the joins since the aggregate results come from the same table. You could instead use conditional aggregation

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

and then unpivot the results using either of the methods in @bluefeet's answer:

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
;

On the other hand, if InProgress and Completed are always either 1 or 0 and they have consistent values in that they cannot both be 1, you could group the results like this:

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

The next step would be to turn the combinations of Completed and InProgress into proper captions. One method is to use 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

And then you would just add the constant Credit Note Value with a UNION ALL:

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
;
Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow
scroll top