I've created two sql queries, the first sums Qty of 12 months and the second sums value over 12 months. Im trying to merge these into one query, but no matter what I try it errors.
The expect output would be 24 columns on one row e.g. Jan, JanQty, Feb, FebQty, Mar, MarQty etc.
Any help would be great!
First Query (Qty):
SELECT
SUM(JanQty) as 'JanQty',
SUM(FebQty) as 'FebQty',
SUM(MarQty) as 'MarQty',
SUM(AprQty) as 'AprQty',
SUM(MayQty) as 'MayQty',
SUM(JuneQty) as 'JuneQty',
SUM(JulyQty) as 'JulyQty',
SUM(AugQty) as 'AugQty',
SUM(SeptQty) as 'SeptQty',
SUM(OctQty) as 'OctQty',
SUM(NovQty) as 'NovQty',
SUM(DecQty) as 'DecQty'
from (
SELECT
ISNULL([1],0) as JanQty,
ISNULL([2],0) as FebQty,
ISNULL([3],0) as MarQty,
ISNULL([4],0) as AprQty,
ISNULL([5],0) as MayQty,
ISNULL([6],0) as JuneQty,
ISNULL([7],0) as JulyQty,
ISNULL([8],0) as AugQty,
ISNULL([9],0) as SeptQty,
ISNULL([10],0) as OctQty,
ISNULL([11],0) as NovQty,
ISNULL([12],0) as DecQty
from
(select SUM(T0.Quantity) as QtyBal,
MONTH(T1.DocDate) as Month
from INV1 T0
inner join OINV T1 on t0.DocEntry = t1.DocEntry
where t1.DocDate BETWEEN '20140101' AND '20141231' and
year(T1.DocDate) = 2014
group by t0.Quantity, t1.DocDate) s
Pivot
(SUM(QtyBal) FOR Month IN ([1],[2],[3],[4],[5],[6],[7],[8],[9],[10],[11],[12])) p
Union ALL
SELECT
ISNULL([1],0) as JanQty,
ISNULL([2],0) as FebQty,
ISNULL([3],0) as MarQty,
ISNULL([4],0) as AprQty,
ISNULL([5],0) as MayQty,
ISNULL([6],0) as JuneQty,
ISNULL([7],0) as JulyQty,
ISNULL([8],0) as AugQty,
ISNULL([9],0) as SeptQty,
ISNULL([10],0) as OctQty,
ISNULL([11],0) as NovQty,
ISNULL([12],0) as DecQty
from
(select SUM(-T0.Quantity) as QtyBal,
MONTH(T1.DocDate) as Month
from RIN1 T0
inner join ORIN T1 on t0.DocEntry = t1.DocEntry
where t1.DocDate BETWEEN '20140101' AND '20141231' and
year(T1.DocDate) = 2014
group by -t0.Quantity, t1.DocDate) s
Pivot
(SUM(QtyBal) FOR Month IN ([1],[2],[3],[4],[5],[6],[7],[8],[9],[10],[11],[12])) p
) sq
Second Query (Value):
SELECT
SUM(Jan) as 'Jan',
SUM(Feb) as 'Feb',
SUM(Mar) as 'Mar',
SUM(Apr) as 'Apr',
SUM(May) as 'May',
SUM(June) as 'June',
SUM(July) as 'July',
SUM(Aug) as 'Aug',
SUM(Sept) as 'Sept',
SUM(oct) as 'Oct',
SUM(nov) as 'Nov',
SUM(Dec) as 'Dec'
from (
SELECT
ISNULL([1],0) as Jan,
ISNULL([2],0) as Feb,
ISNULL([3],0) as Mar,
ISNULL([4],0) as Apr,
ISNULL([5],0) as May,
ISNULL([6],0) as June,
ISNULL([7],0) as July,
ISNULL([8],0) as Aug,
ISNULL([9],0) as Sept,
ISNULL([10],0) as Oct,
ISNULL([11],0) as Nov,
ISNULL([12],0) as Dec
from
(select SUM(T0.LineTotal) as Bal,
MONTH(T1.DocDate) as Month
from INV1 T0
inner join OINV T1 on t0.DocEntry = t1.DocEntry
where t1.DocDate BETWEEN '20140101' AND '20141231' and
year(T1.DocDate) = 2014
group by t0.LineTotal, t1.DocDate) s
Pivot
(SUM(Bal) FOR Month IN ([1],[2],[3],[4],[5],[6],[7],[8],[9],[10],[11],[12])) p
Union ALL
SELECT
ISNULL([1],0) as Jan,
ISNULL([2],0) as Feb,
ISNULL([3],0) as Mar,
ISNULL([4],0) as Apr,
ISNULL([5],0) as May,
ISNULL([6],0) as June,
ISNULL([7],0) as July,
ISNULL([8],0) as Aug,
ISNULL([9],0) as Sept,
ISNULL([10],0) as Oct,
ISNULL([11],0) as Nov,
ISNULL([12],0) as Dec
from
(select SUM(-T0.LineTotal) as Bal,
MONTH(T1.DocDate) as Month
from RIN1 T0
inner join ORIN T1 on t0.DocEntry = t1.DocEntry
where t1.DocDate BETWEEN '20140101' AND '20141231' and
year(T1.DocDate) = 2014
group by -t0.LineTotal, t1.DocDate) s
Pivot
(SUM(Bal) FOR Month IN ([1],[2],[3],[4],[5],[6],[7],[8],[9],[10],[11],[12])) p
) sq
Try using a CROSS JOIN:
SELECT
Jan,
JanQty
FROM
(
SELECT
SUM(JanQty) as 'JanQty',
SUM(FebQty) as 'FebQty',
SUM(MarQty) as 'MarQty',
SUM(AprQty) as 'AprQty',
SUM(MayQty) as 'MayQty',
SUM(JuneQty) as 'JuneQty',
SUM(JulyQty) as 'JulyQty',
SUM(AugQty) as 'AugQty',
SUM(SeptQty) as 'SeptQty',
SUM(OctQty) as 'OctQty',
SUM(NovQty) as 'NovQty',
SUM(DecQty) as 'DecQty'
FROM
(
SELECT
ISNULL([1],0) as JanQty,
ISNULL([2],0) as FebQty,
ISNULL([3],0) as MarQty,
ISNULL([4],0) as AprQty,
ISNULL([5],0) as MayQty,
ISNULL([6],0) as JuneQty,
ISNULL([7],0) as JulyQty,
ISNULL([8],0) as AugQty,
ISNULL([9],0) as SeptQty,
ISNULL([10],0) as OctQty,
ISNULL([11],0) as NovQty,
ISNULL([12],0) as DecQty
FROM
(
SELECT
SUM(T0.Quantity) as QtyBal,
MONTH(T1.DocDate) as Month
FROM
INV1 T0
inner join
OINV T1 on t0.DocEntry = t1.DocEntry
WHERE
t1.DocDate BETWEEN '20140101' AND '20141231' and
year(T1.DocDate) = 2014
GROUP BY t0.Quantity, t1.DocDate
) s
PIVOT
(
SUM(QtyBal) FOR
Month IN ([1],[2],[3],[4],[5],[6],[7],[8],[9],[10],[11],[12])
) p
UNION ALL
SELECT
ISNULL([1],0) as JanQty,
ISNULL([2],0) as FebQty,
ISNULL([3],0) as MarQty,
ISNULL([4],0) as AprQty,
ISNULL([5],0) as MayQty,
ISNULL([6],0) as JuneQty,
ISNULL([7],0) as JulyQty,
ISNULL([8],0) as AugQty,
ISNULL([9],0) as SeptQty,
ISNULL([10],0) as OctQty,
ISNULL([11],0) as NovQty,
ISNULL([12],0) as DecQty
from
(
select
SUM(-T0.Quantity) as QtyBal,
MONTH(T1.DocDate) as Month
from
RIN1 T0
inner join
ORIN T1 on t0.DocEntry = t1.DocEntry
where
t1.DocDate BETWEEN '20140101' AND '20141231' and
year(T1.DocDate) = 2014
group by -t0.Quantity, t1.DocDate) s
Pivot
(
SUM(QtyBal) FOR
Month IN ([1],[2],[3],[4],[5],[6],[7],[8],[9],[10],[11],[12])
) p
) sqa
) qty
CROSS JOIN
(
SELECT
SUM(Jan) as 'Jan',
SUM(Feb) as 'Feb',
SUM(Mar) as 'Mar',
SUM(Apr) as 'Apr',
SUM(May) as 'May',
SUM(June) as 'June',
SUM(July) as 'July',
SUM(Aug) as 'Aug',
SUM(Sept) as 'Sept',
SUM(oct) as 'Oct',
SUM(nov) as 'Nov',
SUM(Dec) as 'Dec'
FROM
(
SELECT
ISNULL([1],0) as Jan,
ISNULL([2],0) as Feb,
ISNULL([3],0) as Mar,
ISNULL([4],0) as Apr,
ISNULL([5],0) as May,
ISNULL([6],0) as June,
ISNULL([7],0) as July,
ISNULL([8],0) as Aug,
ISNULL([9],0) as Sept,
ISNULL([10],0) as Oct,
ISNULL([11],0) as Nov,
ISNULL([12],0) as Dec
FROM
(
SELECT
SUM(T0.LineTotal) as Bal,
MONTH(T1.DocDate) as Month
FROM
INV1 T0
inner join
OINV T1 on t0.DocEntry = t1.DocEntry
WHERE
t1.DocDate BETWEEN '20140101' AND '20141231' and
year(T1.DocDate) = 2014
GROUP BY t0.LineTotal, t1.DocDate
) s
PIVOT
(
SUM(Bal) FOR
Month IN ([1],[2],[3],[4],[5],[6],[7],[8],[9],[10],[11],[12])
) p
UNION ALL
SELECT
ISNULL([1],0) as Jan,
ISNULL([2],0) as Feb,
ISNULL([3],0) as Mar,
ISNULL([4],0) as Apr,
ISNULL([5],0) as May,
ISNULL([6],0) as June,
ISNULL([7],0) as July,
ISNULL([8],0) as Aug,
ISNULL([9],0) as Sept,
ISNULL([10],0) as Oct,
ISNULL([11],0) as Nov,
ISNULL([12],0) as Dec
from
(
select
SUM(-T0.LineTotal) as Bal,
MONTH(T1.DocDate) as Month
from
RIN1 T0
inner join
ORIN T1 on t0.DocEntry = t1.DocEntry
where
t1.DocDate BETWEEN '20140101' AND '20141231' and
year(T1.DocDate) = 2014
group by -t0.LineTotal, t1.DocDate
) s
Pivot
(
SUM(Bal) FOR
Month IN ([1],[2],[3],[4],[5],[6],[7],[8],[9],[10],[11],[12])
) p
) sqb
) Bal
Related
I'm new with MS SQL. How will I simplify this query in the case statement?
The part where the case statement happen. I want to count unique new accounts
Example:
'JAN' 234 - Accounts 'FEB' 20 - New Accounts result will be (234 +
20 = 254) 254 will appear in 'FEB'
SELECT DISTINCT T0.Year,
T0.[Channel YTD],
T0.Month,
CASE [Month]
WHEN 'JAN' THEN (SELECT COUNT(DISTINCT([Buying Accounts])) FROM VW_CHPerMonth_ChannelYTD WHERE Year = T0.Year AND Month IN ('JAN') AND [Combined Name] = T0.[Combined Name])
WHEN 'FEB' THEN (SELECT COUNT(DISTINCT([Buying Accounts])) FROM VW_CHPerMonth_ChannelYTD WHERE Year = T0.Year AND Month IN ('JAN','FEB') AND [Combined Name] = T0.[Combined Name])
WHEN 'MAR' THEN (SELECT COUNT(DISTINCT([Buying Accounts])) FROM VW_CHPerMonth_ChannelYTD WHERE Year = T0.Year AND Month IN ('JAN','FEB','MAR') AND [Combined Name] = T0.[Combined Name])
WHEN 'APR' THEN (SELECT COUNT(DISTINCT([Buying Accounts])) FROM VW_CHPerMonth_ChannelYTD WHERE Year = T0.Year AND Month IN ('JAN','FEB','MAR','APR') AND [Combined Name] = T0.[Combined Name])
WHEN 'MAY' THEN (SELECT COUNT(DISTINCT([Buying Accounts])) FROM VW_CHPerMonth_ChannelYTD WHERE Year = T0.Year AND Month IN ('JAN','FEB','MAR','APR','MAY') AND [Combined Name] = T0.[Combined Name])
WHEN 'JUN' THEN (SELECT COUNT(DISTINCT([Buying Accounts])) FROM VW_CHPerMonth_ChannelYTD WHERE Year = T0.Year AND Month IN ('JAN','FEB','MAR','APR','MAY','JUN') AND [Combined Name] = T0.[Combined Name])
WHEN 'JUL' THEN (SELECT COUNT(DISTINCT([Buying Accounts])) FROM VW_CHPerMonth_ChannelYTD WHERE Year = T0.Year AND Month IN ('JAN','FEB','MAR','APR','MAY','JUN','JUL') AND [Combined Name] = T0.[Combined Name])
WHEN 'AUG' THEN (SELECT COUNT(DISTINCT([Buying Accounts])) FROM VW_CHPerMonth_ChannelYTD WHERE Year = T0.Year AND Month IN ('JAN','FEB','MAR','APR','MAY','JUN','JUL','AUG') AND [Combined Name] = T0.[Combined Name])
WHEN 'SEP' THEN (SELECT COUNT(DISTINCT([Buying Accounts])) FROM VW_CHPerMonth_ChannelYTD WHERE Year = T0.Year AND Month IN ('JAN','FEB','MAR','APR','MAY','JUN','JUL','AUG','SEP') AND [Combined Name] = T0.[Combined Name])
WHEN 'OCT' THEN (SELECT COUNT(DISTINCT([Buying Accounts])) FROM VW_CHPerMonth_ChannelYTD WHERE Year = T0.Year AND Month IN ('JAN','FEB','MAR','APR','MAY','JUN','JUL','AUG','SEP','OCT') AND [Combined Name] = T0.[Combined Name])
WHEN 'NOV' THEN (SELECT COUNT(DISTINCT([Buying Accounts])) FROM VW_CHPerMonth_ChannelYTD WHERE Year = T0.Year AND Month IN ('JAN','FEB','MAR','APR','MAY','JUN','JUL','AUG','SEP','OCT','NOV') AND [Combined Name] = T0.[Combined Name])
WHEN 'DEC' THEN (SELECT COUNT(DISTINCT([Buying Accounts])) FROM VW_CHPerMonth_ChannelYTD WHERE Year = T0.Year AND Month IN ('JAN','FEB','MAR','APR','MAY','JUN','JUL','AUG','SEP','OCT','NOV','DEC') AND [Combined Name] = T0.[Combined Name])
ELSE 0
END AS [Buying Account YTD],
T0.[TPP Channel Type],
T0.[Combined Name]
FROM VW_CHPerMonth_ChannelYTD T0
WHERE Year =2021
AND [Buying Accounts] IS NOT NULL
You can use window aggregates to do this. Unfortunately, there is no COUNT (DISTINCT window aggregate, but we can simulate it with DENSE_RANK and MAX.
You need to order by the month number. Since you don't appear to have such a column, you need to fabricate it.
SELECT DISTINCT
T0.Year,
T0.[Channel YTD],
T0.Month,
MAX(rnk) over (PARTITION BY [Combined Name], [Year]) AS [Buying Account YTD],
T0.[TPP Channel Type],
T0.[Combined Name]
FROM (
SELECT *,
DENSE_RANK() OVER (PARTITION BY [Combined Name], [Year] ORDER BY [Buying Accounts]) rnk
FROM VW_CHPerMonth_ChannelYTD T0
CROSS APPLY (
SELECT CASE [Month]
WHEN JAN THEN 1
WHEN FEB THEN 2
WHEN MAR THEN 3
WHEN APR THEN 4
WHEN MAY THEN 5
WHEN JUN THEN 6
WHEN JUL THEN 7
WHEN AUG THEN 8
WHEN SEP THEN 9
WHEN OCT THEN 10
WHEN NOV THEN 11
ELSE 12
END
) v(MonthNumber)
WHERE Year = 2021
AND [Buying Accounts] IS NOT NULL
) T0
I am trying to fetch Year wise and month wise sales like
select * from
(
select year(InvDt) as [Year], left(datename(Month,InvDt), 3) as [Month],
InvAmnt as Amount
from tblInvoice where TenantId =-xxxxxxxx
) as Inv
pivot
(sum(Amount) for [Month] in(Jan, Feb, Mar, Apr, May, Jun, Jul, Aug, Sep,
Oct, Nov, [Dec])) as pvt
But here so many null is coming so I want to replace these null with zero.
please help me.
create table tblInvoice (InvDt date, TenantId int, InvAmnt numeric)
insert into tblInvoice values ('20180601',1,1),('20180601',1,1),('20180601',1,1),('20180501',1,1), ('20180401',1,1)
SELECT Year
, COALESCE(Jan ,0) Jan
, COALESCE(Feb ,0) Feb
, COALESCE(Mar ,0) Mar
, COALESCE(Apr ,0) Apr
, COALESCE(May ,0) May
, COALESCE(Jun ,0) Jun
, COALESCE(Jul ,0) Jul
, COALESCE(Aug ,0) Aug
, COALESCE(Sep ,0) Sep
, COALESCE(Oct ,0) Oct
, COALESCE(Nov ,0) Nov
, COALESCE(Dec ,0) Dec
FROM ( SELECT YEAR(InvDt) AS Year
, LEFT(DATENAME(MONTH, InvDt), 3) AS Month
, InvAmnt AS Amount
FROM tblInvoice
WHERE TenantId = 1) AS Inv
PIVOT ( SUM(Amount)
FOR Month IN (Jan, Feb, Mar, Apr, May, Jun, Jul, Aug, Sep, Oct, Nov, Dec)) AS pvt;
So Answer is this but it's too long, something we must have another solution but I am not getting right now
select [Year], isnull(Jan, 0) as Jan,isnull(Feb,0) as Feb, isnull(Mar, 0) as
Mar, isnull(Apr, 0) as Apr, isnull(May,0) as May,
isnull(Jun,0) as Jun, isnull(Jul,0) as Jul, isnull(Aug,0) as Aug,
isnull(Sep,0) as Sep,isnull(Oct,0) as Oct, isnull(Nov,0) as Nov,
isnull([Dec],0) as Dec
from
(
select year(InvDt) as [Year], left(datename(Month,InvDt), 3) as [Month],
InvAmnt as Amount
from tblInvoice where TenantId =-xxxxxxx
) as Inv
pivot
(sum(Amount) for [Month] in(Jan, Feb, Mar, Apr, May, Jun, Jul, Aug, Sep,
Oct, Nov, [Dec])) as pvt
Thanks Mazhar by your solution my mind was opened and works this.
Just add a coalesce statement in a subquery to replace nulls on zero:
select * from
(
select year(InvDt) as [Year], left(datename(Month,InvDt), 3) as [Month],
coalesce(InvAmnt, 0) as Amount
from tblInvoice where TenantId =-xxxxxxxx
) as Inv
pivot
(sum(Amount) for [Month] in(Jan, Feb, Mar, Apr, May, Jun, Jul, Aug, Sep,
Oct, Nov, [Dec])) as pvt)
I'm working with PIVOT in SQL, something I've only started learning about this week. I'm trying to get a grand total for each Director as an added column at the side. I've looked online and seen its a popular query, but the ways of doing it seem really confusing and a little over-kill for what I need. Can someone show me how I'd add a grand total column at the end of each row that just adds up the monthly numbers for each Director?
SELECT *
FROM (
SELECT
DimDirectoR.Full_Name, YEAR(dim_Date.Date) as [year],left(datename(month,dim_Date.Date),3)as [month],
Fact_Stream.Device_Type_ID as Amount
FROM Fact_Stream INNER JOIN DimDirector ON Fact_Stream.Director_ID=DimDirector.Director_ID
INNER JOIN dim_Date
on Fact_Stream.Request_View_Date = dim_Date.ID
) as Directors
PIVOT
(
COUNT(Amount)
FOR [month] IN (jan, feb, mar, apr,
may, jun, jul, aug, sep, oct, nov, dec)
)AS DirectorsMonthlyStreams
ORDER BY [year], Full_Name
Current output:
I simply want to have a column which says 'Total' at the end of each row displaying the total amount for that directors year...
SQL Server 2012
You can just do a sum in the top level select statement:
SELECT *, jan+feb+mar+apr+may+jun+jul+aug+sep+oct+nov+dec Total
FROM (
SELECT
DimDirectoR.Full_Name, YEAR(dim_Date.Date) as [year],left(datename(month,dim_Date.Date),3)as [month],
Fact_Stream.Device_Type_ID as Amount
FROM Fact_Stream INNER JOIN DimDirector ON Fact_Stream.Director_ID=DimDirector.Director_ID
INNER JOIN dim_Date
on Fact_Stream.Request_View_Date = dim_Date.ID
) as Directors
PIVOT
(
COUNT(Amount)
FOR [month] IN (jan, feb, mar, apr,
may, jun, jul, aug, sep, oct, nov, dec)
)AS DirectorsMonthlyStreams
ORDER BY [year], Full_Name
You could do something like this as well:
SQL - Pivot with Grand Total Column and Row
I have t_street_name,t_vrm ,t_date_time_issued,ticket_no as columns in a table , i want to get how many tickets have been issued on all streets on all months I am eager to know if there are muilple tickets issued to same vrm on particular street on any given month. This was my query but group by command is not working.
SELECT t_vrm,t_streeet_name,JAN, FEB, MARCH,
APRIL, MAY, JUNE,JULY, AUG, SEPT, OCT, NOV, DECEMBER,
JAN+ FEB+ MARCH+
APRIL+ MAY+ JUNE+JULY+ AUG+ SEPT+ OCT+ NOV+ DECEMBER
AS row_total
FROM
( SELECT t_street_name,t_zone_name,t_vrm, SUM(t1) AS JAN, SUM(t2) AS FEB, SUM(t3) AS MARCH,
SUM(t4) AS APRIL, SUM(t5) AS MAY, SUM(t6) AS JUNE,
SUM(t7) AS JULY, SUM(t8) AS AUG, SUM(t9) AS SEPT,
SUM(t10) AS OCT, SUM(t11) AS NOV, SUM(t12) AS DECEMBER
FROM
(
SELECT b.*,
CASE WHEN DATEPART(mm,t_date_time_issued) BETWEEN '01' AND '01' THEN 1 ELSE 0 END AS t1,
CASE WHEN DATEPART(mm,t_date_time_issued) BETWEEN '02' AND '02' THEN 1 ELSE 0 END AS t2,
CASE WHEN DATEPART(mm,t_date_time_issued) BETWEEN '03' AND '03' THEN 1 ELSE 0 END AS t3,
CASE WHEN DATEPART(mm,t_date_time_issued) BETWEEN '04' AND '04' THEN 1 ELSE 0 END AS t4,
CASE WHEN DATEPART(mm,t_date_time_issued) BETWEEN '05' AND '05' THEN 1 ELSE 0 END AS t5,
CASE WHEN DATEPART(mm,t_date_time_issued) BETWEEN '06' AND '06' THEN 1 ELSE 0 END AS t6,
CASE WHEN DATEPART(mm,t_date_time_issued) BETWEEN '07' AND '07' THEN 1 ELSE 0 END AS t7,
CASE WHEN DATEPART(mm,t_date_time_issued) BETWEEN '08' AND '08' THEN 1 ELSE 0 END AS t8,
CASE WHEN DATEPART(mm,t_date_time_issued) BETWEEN '09' AND '09' THEN 1 ELSE 0 END AS t9,
CASE WHEN DATEPART(mm,t_date_time_issued) BETWEEN '10' AND '10' THEN 1 ELSE 0 END AS t10,
CASE WHEN DATEPART(mm,t_date_time_issued) BETWEEN '11' AND '11' THEN 1 ELSE 0 END AS t11,
CASE WHEN DATEPART(mm,t_date_time_issued) BETWEEN '12' AND '12' THEN 1 ELSE 0 END AS t12
FROM
(
SELECT t_street_name,t_zone_name,t_vrm,t_date_time_issued FROM tickets
)b
)d
group by t_vrm,t_street_name,t_zone_name )qry1
so i want to query the table where it shows the sum of cost by month and the year should be dynamic, i wrote this query but it doesnt sum the cost.
SELECT *
FROM (
SELECT TOP 100 2003 + ROW_NUMBER() OVER (ORDER BY PostingDate) AS Yr, SUM(Amount) as Total
FROM table1
GROUP BY PostingDate
) Years
WHERE Yr <= YEAR(GETDATE())
it should be
month cost year
jan 12.00 2011
feb 10.00 2011
then another column for year 2010. Is this possible
..
SELECT
CASE
WHEN DATEPART(month, PostingDate) = 1 then 'Jan',
WHEN DATEPART(month, PostingDate) = 2 then 'Feb',
WHEN DATEPART(month, PostingDate) = 3 then 'March',
WHEN DATEPART(month, PostingDate) = 4 then 'April',
WHEN DATEPART(month, PostingDate) = 5 then 'May',
WHEN DATEPART(month, PostingDate) = 6 then 'Jun',
WHEN DATEPART(month, PostingDate) = 7 then 'Jul',
WHEN DATEPART(month, PostingDate) = 8 then 'Aug',
WHEN DATEPART(month, PostingDate) = 9 then 'Sep',
WHEN DATEPART(month, PostingDate) = 10 then 'Oct',
WHEN DATEPART(month, PostingDate) = 11 then 'Nov',
ELSE 'DEC' as [Month],
SUM(Amount) as Total,
DATEPART(year, PostingDate) as [Year]
FROM
Table1
WHERE
DATEPART(year, PostingDate) <= DATEPART(year, GETDATE())
GROUP BY
DATEPART(year, PostingDate) as [Year],
DATEPART(month, PostingDate) as [Month]
Note: untested just done in notepad :/