Year months as Column and Fetch data - sql

I have a table like this in SQL Server 2008
SalesMonth SalesPerson TotalAmount
----------- ----------- -----------
3 Ram 10000
3 Rajesh 25000
4 Rajesh 8500
6 Ram 12000
6 Anand 7000
11 Ram 6500
Results should be .....
SalesPerson Jan Feb Mar Apr Jun Jul Aug Sep Oct Nov Dec
Ram 0 0 10000 0 12000 0 0 0 0 6500 0
Rajesh 0 0 25000 8500 0 0 0 0 0 0 0
Anand 0 0 0 0 7000 0 0 0 0 0 0
Is it possible to get through sql query. If so, please help me...

Try to use PIVOT
SELECT SalesPerson,
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 JUN,
ISNULL([7],0) as JUL,
ISNULL([8],0) as AUG,
ISNULL([9],0) as SEP,
ISNULL([10],0) as OCT,
ISNULL([11],0) as NOV,
ISNULL([12],0) as DEC
FROM t
PIVOT
( SUM(TotalAmount)
FOR SalesMonth IN
([1],[2],[3],[4],[5],[6],[7],[8],[9],[10],[11],[12])
) AS PivotTable
ORDER BY SalesPerson;
SQLFiddle demo

You will need to use the SQL Server's PIVOT operator:
SELECT SalesPerson, [Jan], [Feb], [Mar], [Apr], [May], [Jun], [Jul], [Aug], [Sep], [Oct], [Nov], [Dec]
FROM (
SELECT MonthName.SalesMonthName, Sales.SalesPerson, Sales.TotalAmount
FROM Sales
INNER JOIN MonthName ON ( MonthName.MonthNumber = Sales.SalesMonth )
) AS SourceTable
PIVOT (
Sum(TotalAmount)
FOR SalesMonthName IN ([Jan], [Feb], [Mar], [Apr], [May], [Jun], [Jul], [Aug], [Sep], [Oct], [Nov], [Dec])
) AS PivotTable
Also, since PIVOT aggregates data, the example above uses the Sum() function. (You could just as easily use Avg(), or a different aggregation function, based upon your needs.)
Your output will be NULL for all elements that do not have values.
NOTE: In the example above, I made an assumption that there is a fictional table (MonthName) that translates month numbers (1, 2,...) to month names (Jan, Feb,...), since your data does not show the 'jump' from month numbers to month names. If you do not want to do this, replace [Jan], [Feb], ..., [Nov], [Dec] with [1], [2], ..., [11], [12] in the SQL statement above (and, of course, remove the INNER JOIN).

SELECT SalesPerson,
[1] as jan, [2] as feb, [3] as mar, [4] as apr, [5] as may, [6] as jun, [7] as jul
, [8] as aug,[9] as sept,[10] as oct,[11] as nov,[12] as decm
FROM
table1
PIVOT
(
sum(TotalAmount)
FOR SalesMonth IN ([1],[2],[3],[4],[5],[6],[7],[8],[9],[10],[11],[12])
) AS PivotTable;

Related

Is it possible to build this table using SQL?

I have 2 tables, a Sales table and a Payment table structured like the below.
The 2 are joined using the ContractID column. What I want to see is a matrix that shows me at the top, the sum of (sold amount) per monthyear. Then on the left, I want to see the payment dates by month year, and any payments that have been made. My ideal output would look like the below.
The yellow line being the total sold by month-year, and the green lines being all the payments that have been made from the payments table. I don't really know where to start with this one, does anyone have any advice on how to achieve this? I am going to unpivot the sold table first to get my dates across the top, just pondering the next step to pull this table together?
If I didn't understand wrong, it should be like this.
WITH PaymentMatrix
AS
(
SELECT
PaymentMonth,
SoldAmount,
[1] AS Jan,
[2] AS Feb,
[3] AS Mrz,
[4] AS Apr,
[5] AS Mai,
[6] AS Jun,
[7] AS Jul,
[8] AS Aug,
[9] AS Sep,
[10] AS Okt,
[11] AS Nov,
[12] AS Dez
FROM
(
Select
MONTH(S.SoldDate) as SoldMonth,
MONTH(P.PaymentDate) as PaymentMonth,
SUM(S.SoldAmount) as SoldAmount,
SUM(P.PaymentAmount) as PaymentAmount
from Sales S
INNER JOIN Payment P ON S.ContractID = P.ContractID
GROUP BY
MONTH(S.SoldDate),
MONTH(P.PaymentDate)
) source
PIVOT
(
SUM(PaymentAmount)
FOR SoldMonth
IN ( [1], [2], [3], [4], [5], [6], [7], [8], [9], [10], [11], [12] )
) AS pvtMonth
)
SELECT
PaymentMonth,
SUM(SoldAmount) AS Sold,
sum(Jan)as Jan , sum(Feb) as Feb, sum(Mrz) as Mrz, sum(Apr) as Apr, sum(Mai) as Mai,
sum(Jun)as Jun , sum(Jul) as Jul, sum(Aug) as Aug, sum(Sep) as Sep, sum(Okt) as Okt,
sum(Nov) as Nov, sum(Dez) as Dez
FROM PaymentMatrix
GROUP BY PaymentMonth
Fidler Sample
Sample Image
I suggest using conditional aggregation and a union.
Since the PIVOT syntax is more limited.
SELECT [Sold], [Jan-22], [Feb-22], [Mar-22]
FROM
(
SELECT 0 as Seq, 'Paid' AS [Sold]
, SUM(CASE WHEN FORMAT([Sold Date],'MMM-yy') = 'Jan-22'
THEN [Sold Amount] ELSE 0 END) AS [Jan-22]
, SUM(CASE WHEN FORMAT([Sold Date],'MMM-yy') = 'Feb-22'
THEN [Sold Amount] ELSE 0 END) AS [Feb-22]
, SUM(CASE WHEN FORMAT([Sold Date],'MMM-yy') = 'Mar-22'
THEN [Sold Amount] ELSE 0 END) AS [Mar-22]
FROM Sales
UNION ALL
SELECT m.Seq, m.PaymentMonth
, SUM(CASE WHEN SoldMonth = 'Jan-22' THEN PaymentAmount ELSE 0 END) AS [Jan-22]
, SUM(CASE WHEN SoldMonth = 'Feb-22' THEN PaymentAmount ELSE 0 END) AS [Feb-22]
, SUM(CASE WHEN SoldMonth = 'Mar-22' THEN PaymentAmount ELSE 0 END) AS [Mar-22]
FROM (VALUES
(1,'Jan-22'),
(2,'Feb-22'),
(3,'Mar-22')
) m(Seq, PaymentMonth)
LEFT JOIN (
SELECT ContractID
, FORMAT(EOMONTH([Payment Date]), 'MMM-yy') AS PaymentMonth
, SUM([Payment Amount]) AS PaymentAmount
FROM Payment
GROUP BY ContractID, EOMONTH([Payment Date])
) p ON p.PaymentMonth = m.PaymentMonth
LEFT JOIN (
SELECT ContractID
, FORMAT(MAX([Sold Date]), 'MMM-yy') AS SoldMonth
, SUM([Sold Amount]) AS SoldAmount
FROM Sales
GROUP BY ContractID
) s ON s.ContractID = p.ContractID
GROUP BY m.Seq, m.PaymentMonth
) q
ORDER BY Seq;
Sold
Jan-22
Feb-22
Mar-22
Paid
2500
100
0
Jan-22
300
0
0
Feb-22
400
50
0
Mar-22
0
0
0
Test on db<>fiddle here

How to insert a TOTAL column in this SQL Pivot query?

I have a SQL Pivot query which works fine. However, I need to add a TOTAL column in the output (which will be the SUM of all the months).
My query (extract) stands as follows:
...
SELECT [Year],[Property], 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 [MthName], [Year], [RN], [Property] from CTE1
UNION ALL
SELECT [MthName], [Year], [RN], [Property] from CTE2
)x
PIVOT(SUM(x.[RN])
FOR x.[MthName] IN ([Jan], [Feb], [Mar], [Apr], [May], [Jun], [Jul], [Aug], [Sep], [Oct], [Nov], [Dec])) AS PVTTable
ORDER BY [Property], [Year] DESC
An extract of the output of this query is shown below:
Year Property Jan Feb Mar Apr May Jun Jul Aug Sep Oct Nov Dec
2018 XYZ 3261 2955 3045 2118 1917 869 1772 1598 834 1254 537 415
2017 XYZ ...
I need to add a 'Total' column at the end of this output. How can I do this?
Add this part at the last in your Select query:
(ISNULL([Jan],0)+ ISNULL([Feb],0) + ISNULL([Mar],0)+ ISNULL([Apr],0)+ ISNULL([May],0)+ ISNULL([Jun],0) + ISNULL([Jul],0)+ ISNULL([Aug],0)+ ISNULL([Sep],0)+ ISNULL([Oct],0) + ISNULL([Nov],0)+ ISNULL([Dec],0)) as [Total]
I would use conditional aggregation approach instead of using cte with PIVOT operation
select Year,
Property,
SUM(case when MthName = 'Jan' then RN else 0 end) Jan,
SUM(case when MthName = 'Feb' then RN else 0 end) Feb,
SUM(case when MthName = 'Mar' then RN else 0 end) Mar,
...
SUM(RN) Total
from table t
group by year, Property

How to replace NULL with zero in the output of this pivot query? [duplicate]

This question already has answers here:
How to replace (null) values with 0 output in PIVOT
(7 answers)
Closed 5 years ago.
I am using SQL Server 2012 and I have the following SQL query:
SELECT *
FROM
(SELECT [Mth], [Year], [Amount], [Market]
FROM CTE1
UNION ALL
SELECT [Mth], [Year], [Amount], [Market]
FROM CTE2) x
PIVOT
(SUM(x.[Amount)
FOR x.[Mth] IN ([Jan], [Feb], [Mar], [Apr], [May], [Jun], [Jul], [Aug], [Sep], [Oct], [Nov], [Dec])
) AS PVTTable
Extract of output is as follows:
Year Market Jan Feb Mar Apr May Jun Jul Aug Sep Oct Nov Dec
2017 France 11 20 NULL 16 NULL 8 NULL NULL NULL NULL 5 9
I want to replace the NULL with zero. I have tried ISNULL([Mth],0) in my SELECT queries but it's not working.
It's SUM(x.[Amount]) that has the value NULL here, not [mth]. I'm not sure you can, however, use an ISNULL in the PIVOT (no Sample data with which to actually check (and I tend to use do a cross pivot, rather than using PIVOT). If you can't you'll need to put the ISNULL in your outer SELECT statemeent:
SELECT [Year], Market,
ISNULL(Jan,0) AS Jan,
ISNULL(Feb,0) AS Feb, ...

How to use pivot and have 2 different aggregate functions?

I have a table with 1 date column. I want to group the dates by year and month so that I have a matrix such as:
Year Jan Feb Mar...Dec Total
2015.....
2016 10 15 10... 10 115
2017.....
Is this possible to achieve using the PIVOT function, and how exactly do I use it to achieve the above?
You can achieve without PIVOT
SELECT
DATEPART(yyyy,t.the_date) as year,
SUM(CASE WHEN DATEPART(mm,t.the_date)=1 THEN 1 ELSE 0 END) as Jan,
SUM(CASE WHEN DATEPART(mm,t.the_date)=2 THEN 1 ELSE 0 END) as Feb,
...
SUM(CASE WHEN DATEPART(mm,t.the_date)=12 THEN 1 ELSE 0 END) as Dec,
COUNT(*) as Total
FROM the_table t
GROUP BY DATEPART(yyyy,t.the_date)
try using this query : -
SELECT *
FROM (
SELECT
year(yourDate) as [year],left(datename(month,yourDate),3)as [month],
Amount
FROM YourTableName
) as s
PIVOT
(
SUM(Amount)
FOR [month] IN (jan, feb, mar, apr,
may, jun, jul, aug, sep, oct, nov, dec)
)AS pvt

Case Statement Null Issues

I always get null value issues whenever I try to use a case statement on a data from the same column.
Data in database:
Month Sum
Jan 1000
Feb 2000
Mar 3000
Desired Result:
Jan Feb Mar
1000 2000 3000
When I tried using case statement I was running into null issue and was getting results like below:
Jan Feb Mar
1000 Null Null
Null 2000 Null
Null Null 3000
Here is the code that creates null value issue.
select AccountID,
sum(Case when DATEPART(month,EndDateTime) = 10 then Budget End) period1,
sum(case when DATEPART(month,EndDateTime) = 11 then Budget end) period2,
sum(case when DATEPART(month,EndDateTime) = 12 then Budget End) period3,
Description,
from Budget
where DATEPART(month,EndDateTime) in ('10','11','12')
group by AccountID,Description,EndDateTime
order by AccountID,Description,EndDateTime;
Using Pivot function I was able to generate the desired result.
SELECT
AccountID,
[1] AS Jan,
[2] AS Feb,
[3] AS Mar,
[4] AS Apr,
[5] AS May,
[6] AS Jun,
[7] AS Jul,
[8] AS Aug,
[9] AS Sep,
[10] AS Oct,
[11] AS Nov,
[12] AS Dec
FROM
(Select
AccountID,
Budget,
MONTH(EndDateTime) as TMonth
from
dbo.Budget) source
PIVOT
(
SUM(Budget)
FOR TMonth
IN ( [1], [2], [3], [4], [5], [6], [7], [8], [9], [10], [11], [12] )
) AS pvtMonth
The current issue I am now having is that I need to add one more sum function Sum(BudgetNet). Any suggestions?
Thanks.
For the type of result that you are wanting, it looks like you should be using PIVOT instead of CASE.