How pivot month name in SQL Server after a date - sql

I have a table in SQL Server with these columns:
id int pk
date datetime
value numeric
This is my select query
SELECT
O.Date AS DATE,
O.Value AS VALUE
FROM
Orders O
WHERE
YEAR(Date) = #Year
and this is my data
I want this output:
JAN FEB MAR APR MAY JUNE JULY AUG SEPT OCT NOV DEC
-----------------------------------------------------------------------
600 1200 600 600 0 0 0 0 0 0 0 0

Doesn't need a subquery. Something like this should work
select sum(case when dt.mo=1 then o.[Value] else 0 end) 'Jan',
sum(case when dt.mo=2 then o.[Value] else 0 end) 'Feb',
sum(case when dt.mo=3 then o.[Value] else 0 end) 'Mar',
sum(case when dt.mo=4 then o.[Value] else 0 end) 'Apr',
sum(case when dt.mo=5 then o.[Value] else 0 end) 'May',
sum(case when dt.mo=6 then o.[Value] else 0 end) 'Jun',
sum(case when dt.mo=7 then o.[Value] else 0 end) 'Jul',
sum(case when dt.mo=8 then o.[Value] else 0 end) 'Aug',
sum(case when dt.mo=9 then o.[Value] else 0 end) 'Sep',
sum(case when dt.mo=10 then o.[Value] else 0 end) 'Oct',
sum(case when dt.mo=11 then o.[Value] else 0 end) 'Nov',
sum(case when dt.mo=12 then o.[Value] else 0 end) 'Dec'
from Orders o
cross apply (select month(o.[Date]) mo) dt
where year(o.[DAte])=#Year;

You may try this-
Select
max(case when [Month] = 'Jan' then Value end) As "JAN"
,max(case when [Month] = 'Feb' then Value end) As "FEB"
,.....
,max(case when [Month] = 'Dec' then Value end) As "DEC"
from
(SELECT
Format(O.Date, 'MMM') as [Month],
Max(O.Value) AS VALUE
FROM Orders O
WHERE YEAR(Date) = #Year
Group by
Format(O.Date, 'MMM'))T;

Related

SQL - Calculate Customer's Percentage of Total Orders by Month

I'm practicing SQL on this site: https://www.w3schools.com/sql/trysqlserver.asp?filename=trysql_func_sqlserver_substring
, and am trying to calculate the % of total monthly orders by customer ID. So for example, if customer 10 had 3 orders in January, and there were 33 orders total in January, then customer 10's result in January would be 3/33 = 9.09%. I want each row to be a customer ID, and a column for each month.
Basically, I want to convert this:
Into this:
I can get the totals by month, but am having trouble getting the percentages.
I'm using this code:
SELECT d.CustomerID,
SUM(CASE WHEN Month = 01 THEN NumOrders ELSE 0 END) AS Jan,
SUM(CASE WHEN Month = 02 THEN NumOrders ELSE 0 END) AS Feb,
SUM(CASE WHEN Month = 03 THEN NumOrders ELSE 0 END) AS Mar,
SUM(CASE WHEN Month = 04 THEN NumOrders ELSE 0 END) AS Apr,
SUM(CASE WHEN Month = 05 THEN NumOrders ELSE 0 END) AS May,
SUM(CASE WHEN Month = 06 THEN NumOrders ELSE 0 END) AS Jun,
SUM(CASE WHEN Month = 07 THEN NumOrders ELSE 0 END) AS Jul,
SUM(CASE WHEN Month = 08 THEN NumOrders ELSE 0 END) AS Aug,
SUM(CASE WHEN Month = 09 THEN NumOrders ELSE 0 END) AS Sep,
SUM(CASE WHEN Month = 10 THEN NumOrders ELSE 0 END) AS Oct,
SUM(CASE WHEN Month = 11 THEN NumOrders ELSE 0 END) AS Nov,
SUM(CASE WHEN Month = 12 THEN NumOrders ELSE 0 END) AS [Dec],
SUM(NumOrders) AS Total
FROM(
SELECT CustomerID,
DATEPART(mm,OrderDate) AS Month,
COUNT(OrderID) AS NumOrders
FROM Orders
GROUP BY CustomerID,
DATEPART(mm,OrderDate)
) d
GROUP BY d.CustomerID
WITH ROLLUP
I've tried using this code like this to calculate the percentages, but am not getting it to work out.
SUM(CASE WHEN Month = 01 THEN NumOrders ELSE 0 END) / CAST( SUM(NumOrders) OVER (PARTITION BY Month) AS FLOAT) AS JanPct,
This is pretty basic in Excel, and seems like it should be in SQL too, so I feel like I'm missing something obvious.
Try this
Create table #tmp (CustId INT, Jan int, Feb Int, March int)
insert into #tmp VALUES
(10,4,3,5),
(11,3,1,7),
(12,6,2,6),
(13,5,4,4);
Select * from #tmp
select CustId,
CEILING(CAST(Jan As FLOAT)/CAST(SUM(Jan) OVER() AS FLOAT)*100) As Jan,
CEILING(CAST(Feb As FLOAT)/CAST(SUM(Feb) OVER() AS FLOAT)*100) As Feb,
CEILING(CAST(March As FLOAT)/CAST(SUM(March) OVER() AS FLOAT)*100) As March
from #tmp
drop table #tmp
if you want % symbol, convert to varchar and append %
Eg:
CONVERT(VARCHAR(5),CEILING(CAST(Jan As FLOAT)/CAST(SUM(Jan) OVER() AS FLOAT)*100))+'%'
I wasn't able to make rollup work with PIVOT, so here is the long solution.
DECLARE #t table(OrderId INT identity(1,1), OrderDate date, CustomerID INT)
INSERT #t values('2017-01-01', 1),('2017-01-01', 1),('2017-02-01', 1),('2017-01-01', 2)
;WITH CTE as
(
SELECT DISTINCT
CAST(ROUND(count(*) over(partition by CustomerID, Month(OrderDate))*100./ count(*)
over(partition by month(OrderDate)), 0) as INT) Pct,
Month(OrderDate) Mon,
CustomerID
FROM #t
)
SELECT
CustomerID,
SUM(CASE WHEN Mon = 1 THEN Pct ELSE 0 END) AS Jan,
SUM(CASE WHEN Mon = 2 THEN Pct ELSE 0 END) AS Feb,
SUM(CASE WHEN Mon = 3 THEN Pct ELSE 0 END) AS Mar,
SUM(CASE WHEN Mon = 4 THEN Pct ELSE 0 END) AS Apr,
SUM(CASE WHEN Mon = 5 THEN Pct ELSE 0 END) AS May,
SUM(CASE WHEN Mon = 6 THEN Pct ELSE 0 END) AS Jun,
SUM(CASE WHEN Mon = 7 THEN Pct ELSE 0 END) AS Jul,
SUM(CASE WHEN Mon = 8 THEN Pct ELSE 0 END) AS Aug,
SUM(CASE WHEN Mon = 9 THEN Pct ELSE 0 END) AS Sep,
SUM(CASE WHEN Mon = 10 THEN Pct ELSE 0 END) AS Oct,
SUM(CASE WHEN Mon = 11 THEN Pct ELSE 0 END) AS Nov,
SUM(CASE WHEN Mon = 12 THEN Pct ELSE 0 END) AS [Dec]
FROM CTE
GROUP BY ROLLUP (CustomerID)
Just Use below code instead of selecting COUNT(OrderID) AS NumOrders in your below subquery
CONVERT(numeric(10,2), count(Orderid) * 100.0/ (select count(Orderid) from [Orders])) as NumOrders

SQL Server - Arranging select query results by Month

I have the following query (and several other very similar ones) which gathers volumes of data (in this case calls) and then arranges it by month.
It works fine and outputs the results I need, but I have a feeling there must be a more practical/shorter way of doing this:
SELECT
sum(case when datepart(mm,calldate) = '1' THEN 1 ELSE 0 END) AS 'JAN',
sum(case when datepart(mm,calldate) = '2' THEN 1 ELSE 0 END) AS 'FEB',
sum(case when datepart(mm,calldate) = '3' THEN 1 ELSE 0 END) AS 'MAR',
sum(case when datepart(mm,calldate) = '4' THEN 1 ELSE 0 END) AS 'APR',
sum(case when datepart(mm,calldate) = '5' THEN 1 ELSE 0 END) AS 'MAY',
sum(case when datepart(mm,calldate) = '6' THEN 1 ELSE 0 END) AS 'JUN',
sum(case when datepart(mm,calldate) = '7' THEN 1 ELSE 0 END) AS 'JUL',
sum(case when datepart(mm,calldate) = '8' THEN 1 ELSE 0 END) AS 'AUG',
sum(case when datepart(mm,calldate) = '9' THEN 1 ELSE 0 END) AS 'SEP',
sum(case when datepart(mm,calldate) = '10' THEN 1 ELSE 0 END) AS 'OCT',
sum(case when datepart(mm,calldate) = '11' THEN 1 ELSE 0 END) AS 'NOV',
sum(case when datepart(mm,calldate) = '12' THEN 1 ELSE 0 END) AS 'DEC'
FROM [Telephony].[dbo].[tbl_Outbound]
WHERE source like '132%'
and duration > 300
union all
SELECT
sum(case when datepart(mm,calldate) = '1' THEN 1 ELSE 0 END) AS 'JAN',
sum(case when datepart(mm,calldate) = '2' THEN 1 ELSE 0 END) AS 'FEB',
sum(case when datepart(mm,calldate) = '3' THEN 1 ELSE 0 END) AS 'MAR',
sum(case when datepart(mm,calldate) = '4' THEN 1 ELSE 0 END) AS 'APR',
sum(case when datepart(mm,calldate) = '5' THEN 1 ELSE 0 END) AS 'MAY',
sum(case when datepart(mm,calldate) = '6' THEN 1 ELSE 0 END) AS 'JUN',
sum(case when datepart(mm,calldate) = '7' THEN 1 ELSE 0 END) AS 'JUL',
sum(case when datepart(mm,calldate) = '8' THEN 1 ELSE 0 END) AS 'AUG',
sum(case when datepart(mm,calldate) = '9' THEN 1 ELSE 0 END) AS 'SEP',
sum(case when datepart(mm,calldate) = '10' THEN 1 ELSE 0 END) AS 'OCT',
sum(case when datepart(mm,calldate) = '11' THEN 1 ELSE 0 END) AS 'NOV',
sum(case when datepart(mm,calldate) = '12' THEN 1 ELSE 0 END) AS 'DEC'
FROM [Telephony].[dbo].[tbl_Outbound]
WHERE source like '132%'
Thanks for any tips you can give :)
I believe the following would work. This uses SQL Server's Pivot Functionality:
SELECT rowName,[January],[February],[March],[April],[May],[June],[July],[August],[September],[October],[November],[December]
FROM (
SELECT
"Duration>300" as rowName,
datename(month, calldate) as MonthName,
Count(*) as recordCount
FROM
[Telephony].[dbo].[tbl_Outbound]
WHERE source like '132%'
and duration > 300
UNION ALL
SELECT
"AllDurations" as rowName,
datename(month, calldate) as MonthName,
Count(*) as recordCount
FROM
[Telephony].[dbo].[tbl_Outbound]
WHERE source like '132%'
) as SourceTable
PIVOT
(
Sum(recordCount)
FOR MonthName in ([January],[February],[March],[April],[May],[June],[July],[August],[September],[October],[November],[December])
) AS PivotTable

SQL Grand Total last ROW

Is there a way I could get the grand total of each month. I've looked into Rollup but cant seem to figure it out.
Query I have is
SELECT t.city,f.fname,f.lname,
SUM(CASE datepart(month,ddate) WHEN 1 THEN 1 ELSE 0 END) AS 'January',
SUM(CASE datepart(month,ddate) WHEN 2 THEN 1 ELSE 0 END) AS 'February',
SUM(CASE datepart(month,ddate) WHEN 3 THEN 1 ELSE 0 END) AS 'March',
SUM(CASE datepart(month,ddate) WHEN 4 THEN 1 ELSE 0 END) AS 'April',
SUM(CASE datepart(month,ddate) WHEN 5 THEN 1 ELSE 0 END) AS 'May',
SUM(CASE datepart(month,ddate) WHEN 6 THEN 1 ELSE 0 END) AS 'June',
SUM(CASE datepart(month,ddate) WHEN 7 THEN 1 ELSE 0 END) AS 'July',
SUM(CASE datepart(month,ddate) WHEN 8 THEN 1 ELSE 0 END) AS 'August',
SUM(CASE datepart(month,ddate) WHEN 9 THEN 1 ELSE 0 END) AS 'September',
SUM(CASE datepart(month,ddate) WHEN 10 THEN 1 ELSE 0 END) AS 'October',
SUM(CASE datepart(month,ddate) WHEN 11 THEN 1 ELSE 0 END) AS 'November',
SUM(CASE datepart(month,ddate) WHEN 12 THEN 1 ELSE 0 END) AS 'December',
SUM(CASE datepart(year,ddate) WHEN 2014 THEN 1 ELSE 0 END) AS 'TOTAL'
from world T
INNER JOIN sales F ON T.ID=F.ID
where t.city = ROME
group by t.city,f.fname,f.lname
Output example
t.city, f.fname, f.lname January total
ROME John Doe 5 5
Grand Total 5 5
Did you try this?
group by GROUPING SETS((t.city, f.fname, f.lname), ())
To get Grand Total you'll need to change the select as well.
And, as a note: use single quotes only for string and date constants. Using them for column identifiers can lead to confusing and problems. Either drop the quotes altogether or use square braces or double quotes.
EDIT:
Without group by extensions or CTEs this is a pain. There is a trick to doing it with minimal modifications:
SELECT (case when which = 'normal' then t.city else 'Grand Total' end),
(case when which = 'normal' then f.fname end),
(case when which = 'normal' then f.lname end),
SUM(CASE datepart(month,ddate) WHEN 1 THEN 1 ELSE 0 END) AS 'January',
SUM(CASE datepart(month,ddate) WHEN 2 THEN 1 ELSE 0 END) AS 'February',
SUM(CASE datepart(month,ddate) WHEN 3 THEN 1 ELSE 0 END) AS 'March',
SUM(CASE datepart(month,ddate) WHEN 4 THEN 1 ELSE 0 END) AS 'April',
SUM(CASE datepart(month,ddate) WHEN 5 THEN 1 ELSE 0 END) AS 'May',
SUM(CASE datepart(month,ddate) WHEN 6 THEN 1 ELSE 0 END) AS 'June',
SUM(CASE datepart(month,ddate) WHEN 7 THEN 1 ELSE 0 END) AS 'July',
SUM(CASE datepart(month,ddate) WHEN 8 THEN 1 ELSE 0 END) AS 'August',
SUM(CASE datepart(month,ddate) WHEN 9 THEN 1 ELSE 0 END) AS 'September',
SUM(CASE datepart(month,ddate) WHEN 10 THEN 1 ELSE 0 END) AS 'October',
SUM(CASE datepart(month,ddate) WHEN 11 THEN 1 ELSE 0 END) AS 'November',
SUM(CASE datepart(month,ddate) WHEN 12 THEN 1 ELSE 0 END) AS 'December',
SUM(CASE datepart(year,ddate) WHEN 2014 THEN 1 ELSE 0 END) AS 'TOTAL'
from world T INNER JOIN
sales F
ON T.ID=F.ID cross join
(select 'normal' as which union all select 'total') as which
where t.city = ROME
group by (case when which = 'normal' then t.city else 'Grand Total' end),
(case when which = 'normal' then f.fname end),
(case when which = 'normal' then f.lname end);
(I'm not reformatting the rest of the query, but you should not use single quotes for column identifiers. Only use single quotes for string and date constants.)
Edit: Using GROUP BY ROLLUP(grouping columns) this will calculate the sum of the aggregated columns. By default it will return 'NULL' for all grouped columns but you can put an ISNULL wrapper around to get rid of it, or return a specific value.
SELECT ISNULL(t.city, 'Grand Total') AS [City],f.fname AS [Fname],f.lname AS [Lname],
SUM(CASE datepart(month,ddate) WHEN 1 THEN 1 ELSE 0 END) AS [January],
SUM(CASE datepart(month,ddate) WHEN 2 THEN 1 ELSE 0 END) AS [February],
SUM(CASE datepart(month,ddate) WHEN 3 THEN 1 ELSE 0 END) AS [March],
SUM(CASE datepart(month,ddate) WHEN 4 THEN 1 ELSE 0 END) AS [April],
SUM(CASE datepart(month,ddate) WHEN 5 THEN 1 ELSE 0 END) AS [May],
SUM(CASE datepart(month,ddate) WHEN 6 THEN 1 ELSE 0 END) AS [June],
SUM(CASE datepart(month,ddate) WHEN 7 THEN 1 ELSE 0 END) AS [July],
SUM(CASE datepart(month,ddate) WHEN 8 THEN 1 ELSE 0 END) AS [August],
SUM(CASE datepart(month,ddate) WHEN 9 THEN 1 ELSE 0 END) AS [September],
SUM(CASE datepart(month,ddate) WHEN 10 THEN 1 ELSE 0 END) AS [October],
SUM(CASE datepart(month,ddate) WHEN 11 THEN 1 ELSE 0 END) AS [November],
SUM(CASE datepart(month,ddate) WHEN 12 THEN 1 ELSE 0 END) AS [December],
SUM(CASE datepart(year,ddate) WHEN 2014 THEN 1 ELSE 0 END) AS [TOTAL]
FROM world T
INNER JOIN sales F ON T.ID=F.ID
WHERE t.city = ROME
GROUP BY ROLLUP(t.city,f.fname,f.lname)

SQLSRV: group by one column and sum by month

I have a difficult query to do =)
the Stops table have the following columns
Id, Area, SubArea, Date, StopNumber, DownTime
I would like to have a result like this: (I'll try to describe..)
Area, SubArea, SUM(StopNumber appened in january) as GEN,
SUM(StopNumber appened in february) as FEB, etc..
FROM Stops
GROUP BY Area, SubArea
Could anyone help me?
thank you :)
If you like the approach I suggested you in the comment, you could try something like this:
SELECT Area, SubArea, MONTH(Date) AS Month, SUM(StopNumber) AS NumStops
FROM Stops
GROUP BY Area, SubArea, MONTH(Date)
This should work:
SELECT Area, SubArea,
SUM(CASE Month(Date) WHEN 1 THEN 1 ELSE 0 END) AS Jan,
SUM(CASE Month(Date) WHEN 2 THEN 1 ELSE 0 END) AS Feb,
SUM(CASE Month(Date) WHEN 3 THEN 1 ELSE 0 END) AS Mar,
SUM(CASE Month(Date) WHEN 4 THEN 1 ELSE 0 END) AS Apr,
SUM(CASE Month(Date) WHEN 5 THEN 1 ELSE 0 END) AS May,
SUM(CASE Month(Date) WHEN 6 THEN 1 ELSE 0 END) AS Jun,
SUM(CASE Month(Date) WHEN 7 THEN 1 ELSE 0 END) AS Jul,
SUM(CASE Month(Date) WHEN 8 THEN 1 ELSE 0 END) AS Aug,
SUM(CASE Month(Date) WHEN 9 THEN 1 ELSE 0 END) AS Sep,
SUM(CASE Month(Date) WHEN 10 THEN 1 ELSE 0 END) AS Oct,
SUM(CASE Month(Date) WHEN 11 THEN 1 ELSE 0 END) AS Nov,
SUM(CASE Month(Date) WHEN 12 THEN 1 ELSE 0 END) AS [Dec]
FROM Stops
GROUP BY Area, SubArea

Count records for every month in a year

I have a table with total no of 1000 records in it.It has the following structure:
EMP_ID EMP_NAME PHONE_NO ARR_DATE
1 A 545454 2012/03/12
I want to calculate no of records for every month in year-2012
Is there any way that should solve my issue in a single shot?
I tried:
select count(*)
from table_emp
where year(ARR_DATE) = '2012' and month(ARR_DATE) = '01'
SELECT COUNT(*)
FROM table_emp
WHERE YEAR(ARR_DATE) = '2012'
GROUP BY MONTH(ARR_DATE)
This will give you the count per month for 2012;
SELECT MONTH(ARR_DATE) MONTH, COUNT(*) COUNT
FROM table_emp
WHERE YEAR(arr_date)=2012
GROUP BY MONTH(ARR_DATE);
Demo here.
Try This query:
SELECT
SUM(CASE datepart(month,ARR_DATE) WHEN 1 THEN 1 ELSE 0 END) AS 'January',
SUM(CASE datepart(month,ARR_DATE) WHEN 2 THEN 1 ELSE 0 END) AS 'February',
SUM(CASE datepart(month,ARR_DATE) WHEN 3 THEN 1 ELSE 0 END) AS 'March',
SUM(CASE datepart(month,ARR_DATE) WHEN 4 THEN 1 ELSE 0 END) AS 'April',
SUM(CASE datepart(month,ARR_DATE) WHEN 5 THEN 1 ELSE 0 END) AS 'May',
SUM(CASE datepart(month,ARR_DATE) WHEN 6 THEN 1 ELSE 0 END) AS 'June',
SUM(CASE datepart(month,ARR_DATE) WHEN 7 THEN 1 ELSE 0 END) AS 'July',
SUM(CASE datepart(month,ARR_DATE) WHEN 8 THEN 1 ELSE 0 END) AS 'August',
SUM(CASE datepart(month,ARR_DATE) WHEN 9 THEN 1 ELSE 0 END) AS 'September',
SUM(CASE datepart(month,ARR_DATE) WHEN 10 THEN 1 ELSE 0 END) AS 'October',
SUM(CASE datepart(month,ARR_DATE) WHEN 11 THEN 1 ELSE 0 END) AS 'November',
SUM(CASE datepart(month,ARR_DATE) WHEN 12 THEN 1 ELSE 0 END) AS 'December',
SUM(CASE datepart(year,ARR_DATE) WHEN 2012 THEN 1 ELSE 0 END) AS 'TOTAL'
FROM
sometable
WHERE
ARR_DATE BETWEEN '2012/01/01' AND '2012/12/31'
select count(*)
from table_emp
where DATEPART(YEAR, ARR_DATE) = '2012' AND DATEPART(MONTH, ARR_DATE) = '01'