Case Statement Null Issues - sql-server-2012

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.

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

SQL query two PIVOT columns combine

select pvtMonth.CardName, [1] as [Jan Sales], [2] as [Feb Sales], [3] as [Mar Sales], [4] as [Apr Sales], [5] as [May Sales],
[6] as [Jun Sales], [7] as [Jul Sales], [8] as [Aug Sales], [9] as [Sep Sales], [10] as [Oct Sales], [11] as [Nov Sales], [12] as [Dec Sales] from
(
select X.CardName, SUM(X.[Total Sales S$]) as [Sales $] , X.Month from Data X group by X.CardName ,X.Month
) X PIVOT
(
sum(X.[Sales $])
FOR [Month]
IN ( [1], [2], [3], [4], [5], [6], [7], [8], [9], [10], [11], [12] )
) AS pvtMonth order by pvtMonth.CardName asc
now i need add one more called "Jan Gp" the column name is "X.Gp". How to update my query. I need the result set like below
I would recommend using conditional aggregation instead of the pivot syntax. It is much more flexible (and at least as efficient). You seem to want something like:
select
cardName,
sum(case when month = 1 then [Total Sales S$] end) [Jan Sales $],
sum(case when month = 1 then [Total Sales SGP] end) [Jan Sales GP],
sum(case when month = 2 then [Total Sales S$] end) [Feb Sales $],
sum(case when month = 2 then [Total Sales SGP] end) [Feb Sales GP],
...
from data
group by cardName

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

SQL To Calculate "To Date" Values With Month Columns

SQL Afficianados,
There has got to be a better way than the road I am going down...
SQL Fiddle HERE
Using SQL Server 2008. In short, I have a table with months as columns. i.e.:
CREATE TABLE MyData (MyID VARCHAR(10),
JAN MONEY, FEB MONEY, MAR MONEY, APR MONEY, MAY MONEY, JUN MONEY,
JUL MONEY, AUG MONEY, SEP MONEY, OCT MONEY, NOV MONEY, DEC MONEY);
Given a month value of N (as integer, where 1 = JAN and 12 = DEC), I want to calculate a "To Date" value from the first month to the Nth month.
So, given this simple data:
INSERT INTO MyData (MyID, JAN, FEB, MAR, APR, MAY, JUN, JUL, AUG, SEP, OCT, NOV, DEC)
SELECT 'Rec1', 1, 2, 3, 4, 5, 6, 7, 8, 9, 10, 11, 12
UNION ALL SELECT 'Rec2', 1, 2, 3, 4, 5, 6, 7, 8, 9, 10, 11, 12
UNION ALL SELECT 'Rec3', 1, 2, 3, 4, 5, 6, 7, 8, 9, 10, 11, 12
;
I want to pass in any month value and add up the proper month columns. Given the sample data above, here is a chart of expected results based on the value of the month passed in...
Month Value | Expected Result |
------------+------------------
1 | 3
2 | 9
3 | 18
4 | 30
5 | 45
6 | 63
7 | 84
8 | 108
9 | 135
10 | 165
11 | 198
12 | 234
I know I could do this with a CASE statement like this:
DECLARE #v_Month INT = 2
SELECT CASE
WHEN #v_Month = 1 THEN SUM(JAN)
WHEN #v_Month = 2 THEN SUM(JAN) + SUM(FEB)
WHEN #v_Month = 2 THEN SUM(JAN) + SUM(FEB) + SUM(MAR)
--You get the idea. The pattern would continue for the rest of the months.
ELSE 0
END AS ToDateSum
FROM MyData
But is there a better way? Teach me, oh great ones of SQL Server.
You can use unpivot to change the data to a more amenable shape. It might make more sense to store it this way in the first place.
declare #v_Month int = 2;
with u as (
select
myid, [month], amount
from (
select
myid, jan [1], feb [2], mar [3], apr [4], may [5], jun [6],
jul [7], aug [8], sep [9], oct [10], nov [11], dec [12]
from
MyData
) p
unpivot (
amount for [month] in ([1], [2], [3], [4], [5], [6], [7], [8], [9], [10], [11], [12])
) u
) select
sum(amount)
from
u
where
[month] <= #v_Month;
Example SQLFiddle
You can simplify this a bit, if you want to keep the data in the same structure:
select
sum(amount)
from (
select
myid, jan [1], feb [2], mar [3], apr [4], may [5], jun [6],
jul [7], aug [8], sep [9], oct [10], nov [11], dec [12]
from
MyData
) p
unpivot (
amount for [month] in ([1], [2], [3], [4], [5], [6], [7], [8], [9], [10], [11], [12])
) u
where
[month] <= #v_Month;
Something like this will work:
declare #MyDesiredMonth int = 3
with normalized_view as
(
select MyId , Month = 1 , Value = Jan from MyData
UNION ALL select MyId , Month = 2 , Value = Feb from MyData
UNION ALL ...
UNION ALL select MyId , Month = 11 , Value = Nov from MyData
UNION ALL select MyId , Month = 12 , Value = Dec from MyData
)
select MonthlyTotal = sum(Value)
from normalized_view t
where t.Month = #myDesiredMonth
Alternatively, something like this:
declare #MyDesiredMonth int = 3
select MonthlyTotal = sum( case #MyDesiredMonth
when 1 then t.Jan
when 2 then t.Feb
...
when 11 then t.Nov
when 12 then t.Dec
end
)
from MyTable t
Either one should work with the execution plan likely winding up similar.

Year months as Column and Fetch data

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;