SQL query to count incidents by year by month - sql

I have one table called Incidents. I want a SQL query that returns the count of incidents split out by year, by month. The start year is 2010, however, the end year will be variable.
Example Incidents table:
DateLogged IncidentRef
----------- ------------
2015-04-05 1
2014-06-04 2
2013-01-01 3
2012-12-10 4
2011-10-15 5
2010-10-01 6
2012-12-11 7
2011-10-10 8
2010-10-10 9
Query Returns:
Year Jan Fev Mar Abr Mai Jun Jul Ago Set Out Nov Dez
2010 0 0 0 0 0 0 0 0 0 2 0 0
2011 0 0 0 0 0 0 0 0 0 2 0 0
2012 0 0 0 0 0 0 0 0 0 0 0 2
2013 1 0 0 0 0 0 0 0 0 0 0 0
2014 0 0 0 0 0 1 0 0 0 0 0 0
2015 0 0 0 1 0 0 0 0 0 0 0 0
How can I do that query? Where do I start?

One option is to use conditional aggregation:
select year(datelogged),
sum(case when month(datelogged) = 1 then 1 else 0 end) Jan,
sum(case when month(datelogged) = 2 then 1 else 0 end) Feb,
...,
sum(case when month(datelogged) = 12 then 1 else 0 end) Dec
from yourtable
group by year(datelogged)
SQL Fiddle Demo

With pivoting:
;with cte as(select year(date) y, month(date) m, ref from table)
select * from cte
pivot(count(ref) for m in([1],[2],...,[12]))p

Another way to do without a pivot you could use a join:
with years as
(
SELECT 2010 as y
UNION ALL
SELECT 2011
UNION ALL
SELECT 2012
UNION ALL
SELECT 2013
UNION ALL
SELECT 2014
UNION ALL
SELECT 2015
)
select years.y,
sum(case when month(DateLogged) = 1 then 1 else 0) as jan,
sum(case when month(DateLogged) = 2 then 1 else 0) as feb,
sum(case when month(DateLogged) = 3 then 1 else 0) as mar,
sum(case when month(DateLogged) = 4 then 1 else 0) as apr,
-- ...
sum(case when month(DateLogged) = 12 then 1 else 0) as dec,
from years y
left join incidents i on y.y = year(i.DateLogged)
group by y.DateLogged
If you want year to be "dynamic" you have the CTE like this
with years as
(
SELECT DISTINCT year(i.DateLogged) FROM incidents
)
but this has the same drawback as the sgeddes solution -- years with no values don't show up.

Using classic PIVOT:
Data:
CREATE TABLE #Incidents(
DateLogged DATE NOT NULL PRIMARY KEY
,IncidentRef INTEGER NOT NULL );
INSERT INTO #Incidents(DateLogged,IncidentRef) VALUES
('2015-04-05',1),('2014-06-04',2),('2013-01-01',3),
('2012-12-10',4),('2011-10-15',5),('2010-10-01',6),
('2012-12-11',7),('2011-10-10',8),('2010-10-10',9);
Query:
;WITH cte AS
(
SELECT [year] = YEAR(DateLogged),
[month] = CASE MONTH(DateLogged)
WHEN 1 THEN 'Jan' WHEN 2 THEN 'Fev'
WHEN 3 THEN 'Mar' WHEN 4 THEN 'Abr'
WHEN 5 THEN 'Mai' WHEN 6 THEN 'Jun'
WHEN 7 THEN 'Jul' WHEN 8 THEN 'Ago'
WHEN 9 THEN 'Set' WHEN 10 THEN 'Out'
WHEN 11 THEN 'Nov' WHEN 12 THEN 'Dez'
END,
IncidentRef
FROM #Incidents
)
SELECT [Year],Jan, Fev, Mar, Abr, Mai, Jun, Jul, Ago, [Set], Out, Nov, Dez
FROM cte
PIVOT (
COUNT(IncidentRef)
FOR [month] IN (Jan, Fev, Mar, Abr, Mai, Jun,Jul, Ago, [Set], Out, Nov,Dez)
) AS piv;
LiveDemo
SQL Server 2012+
Using CHOOSE added UNION to cte to ensure to get zeros for missing years:
;WITH cte AS
(
SELECT [year] = YEAR(DateLogged),
[month] = CHOOSE(MONTH(DateLogged),'Jan', 'Fev', 'Mar', 'Abr', 'Mai', 'Jun', 'Jul', 'Ago', 'Set', 'Out', 'Nov', 'Dez'),
IncidentRef
FROM #Incidents
UNION ALL
SELECT [year], NULL, NULL
FROM (VALUES (2010),(2011),(2012),(2013),(2014),(2015)) AS t([year])
)
SELECT [Year],Jan, Fev, Mar, Abr, Mai, Jun, Jul, Ago, [Set], Out, Nov, Dez
FROM cte
PIVOT (
COUNT(IncidentRef)
FOR [month] IN (Jan, Fev, Mar, Abr, Mai, Jun,Jul, Ago, [Set], Out, Nov,Dez)
) AS piv
ORDER BY [Year];
LiveDemo2

Related

SQL Pivot table not returining expected results

This is the first time I have had to use a SQL pivot table, so I'm not very proficient in getting results with the method, my pivot table is not returning the results I expected and I don't know why...
Here is my instruction:
"Create a sql that will show the total number create [ddateCreated] of patients per branch for each month for 2020 - 2021.
Display Required fields BranchName , Year , Jan , Feb, Mar, Apr, May , Jun , Jul ,Aug , Sept , Oct , Nov , Dec."
so I came up with the below query:
select * from (
select datename(month, b.dDateCreated) as [Month] ,
sName as BranchName ,
datename(Year, b.dDateCreated) as [Year],
p.ipkPatientID from Branch b
inner join Patients p
on p.ifkBranchID=b.ipkBranchID
where (b.dDateCreated BETWEEN '2020-01-01 00:00:00.000'AND '2021-12-31 23:59:59.999')
) as Src
pivot(
count(ipkPatientID)
for [Month] in ([Jan],
[Feb],
[Mar],
[Apr],
[May],
[Jun],
[Jul],
[Aug],
[Sept],
[Oct],
[Nov],
[Dec])
) as Pivot_Table
Here are the results:
BranchName Year Jan Feb Mar Apr May Jun Jul Aug Sept Oct Nov Dec
Cdldttd dd Fhdftwt wndfplpgy 2020 0 0 0 0 0 0 0 0 0 0 0 0
Ddjdlppmdnt 2020 0 0 0 0 14 0 0 0 0 0 0 0
Ddpn Cdhpnfp 2020 0 0 0 0 0 0 0 0 0 0 0 0
dlmfdn Lpnw Lfchtdnwnhg 2020 0 0 0 0 0 0 0 0 0 0 0 0
dlmfdn Lpnw Mwffkdng 2020 0 0 0 0 0 0 0 0 0 0 0 0
Dnhwwnjflld 2020 0 0 0 0 0 0 0 0 0 0 0 0
dthpwd 2020 0 0 0 0 0 0 0 0 0 0 0 0
fnc and wttpcfwtdt 2020 0 0 0 0 5 0 0 0 0 0 0 0
Fwdhfd Gldn 2020 0 0 0 0 0 0 0 0 0 0 0 0
Hdwhdwt pfffcd 2020 0 0 0 0 0 0 0 0 0 0 0 0
Hpmd jftft 2020 0 0 0 0 91 0 0 0 0 0 0 0
I can't understand why it shows 0's everywhere? i have tried to refactor this query mulptiple times but I cant come right... Also note how there is little data in the Month of May, for some odd reason.
Look at the below query, which is the source of the pivot table:
select datename(month, b.dDateCreated) as [Month] ,
sName as BranchName ,
datename(Year, b.dDateCreated) as [Year],
p.ipkPatientID from Branch b
inner join Patients p
on p.ifkBranchID=b.ipkBranchID
where (b.dDateCreated BETWEEN '2020-01-01 00:00:00.000'AND '2021-12-31 23:59:59.999')
These results look correct?
and there is data for every month... So how would I count the patient id's for every month and display it?
Pretty sure this will correct your issue
Select *
From (
select left(datename(month, b.dDateCreated),3) as [Month] , -- Notice Jan,Feb,Mar
sName as BranchName ,
datename(Year, b.dDateCreated) as [Year],
p.ipkPatientID
from Branch b
inner join Patients p on p.ifkBranchID=b.ipkBranchID
where b.dDateCreated BETWEEN '2020-01-01 00:00:00.000'AND '2021-12-31 23:59:59.997'
) as Src
Pivot( count(ipkPatientID) for [Month] in ([Jan],
[Feb],
[Mar],
[Apr],
[May],
[Jun],
[Jul],
[Aug],
[Sep], -- Notice Sep not Sept
[Oct],
[Nov],
[Dec] ) ) as Pivot_Table
I would recommend pivoting with conditional aggregation rather than using the vendor-specific pivot query. I find that the pivot syntax is less intuitive ; it is also less flexible, and of course not easily portable across databases.
Using conditional aggregation, and starting from your existing working query, you would pivot like so:
select b.sName as BranchName, x.[Year],
sum(case when x.[Month] = 'Jan' then 1 else 0 end) as Jan
sum(case when x.[Month] = 'Feb' then 1 else 0 end) as Feb
-- ... repeat for the following months...
sum(case when x.[Month] = 'Dec' then 1 else 0 end) as Dec
from Branch b
inner join Patients p on p.ifkBranchID = b.ipkBranchID
cross apply ( values (datename(year, b.dDateCreated), datename(month, b.dDateCreated) ) d([Month], [Year])
where b.dDateCreated >= '2020-01-01' and b.dDateCreated < '2022-01-01'
group by b.ifkBranchID, b.sName, x.[Year]
Side notes:
filtering dates is often simpler than with half-open intervals than with between
you probably want to group by the primary key of the branch table (which I assumed is ifkBranchID) ; different branches may have the same name.
I used outer apply to compute the datenames just once rather than repeating the expressions all over the queqry
it is good practice to prefix all columns in the query with the table they belong to

How do i transform calendar year column to multiple year to months column based on months column and calendar year columns

I have data like this
id MoYear CalenderYear jan feb mar dec
1 2017 2017 1 2 4 0
1 2017 2018 1 0 6 10
2 2018 2018 80 5 8 22
3 2017 2018 30 12 0 3
Now i want ouput like this
id MOyear jan_17 feb_17 mar_17 dec_17 jan_18 feb_18 mar_18 dec_18
1 2017 1 2 4 0 1 0 6 10
2 2018 null null null null 80 5 8 22
3 2017 null null null null 30 12 0 3
I have calendar year column and months columns, based on the calendar year and months column i need to make multiple year-months columns.
I can get to the solution by unpivoting and then after back to pivot. But, the data is so large it takes a lot of memory. The performance is very bad.
Not sure if this will be better approach but you can achieve your output using case statement as well if you don't want to do pivot/unpivot.
Data Creation:
select 1 as ID, 2017 as MOYEar, 2017 as calenderyear, 1 as Jan, 2 as feb,
4 as mar, 0 as dece into #temp union all
select 1 as ID, 2017 as MOYEar, 2018 as calenderyear, 1 as Jan, 0 as feb,
6 as mar, 10 as dece union all
select 2 as ID, 2018 as MOYEar, 2018 as calenderyear, 80 as Jan, 5 as feb,
8 as mar, 22 as dece union all
select 3 as ID, 2017 as MOYEar, 2018 as calenderyear, 30 as Jan, 12 as feb,
0 as mar, 3 as dece
Query:
Select ID, MOYEar, max(case when calenderyear = '2017' then Jan else null end) as Jan_17,
max(case when calenderyear = '2017' then Feb else null end ) as Feb_17,
max(case when calenderyear = '2017' then Mar else null end ) as Mar_17,
max(case when calenderyear = '2017' then Dece else null end) as Dece_17,
max(case when calenderyear = '2018' then Jan else null end ) as Jan_18,
max(case when calenderyear = '2018' then Feb else null end ) as Feb_18,
max(case when calenderyear = '2018' then Mar else null end ) as Mar_18,
max(case when calenderyear = '2018' then Dece else null end) as Dece_18 from #temp
Group by ID, MOYEar
Output:
ID MOYEar Jan_17 Feb_17 Mar_17 Dece_17 Jan_18 Feb_18 Mar_18 Dece_18
1 2017 1 2 4 0 1 0 6 10
3 2017 NULL NULL NULL NULL 30 12 0 3
2 2018 NULL NULL NULL NULL 80 5 8 22

SQL I want to get the following output

I have a sales table
Date_s sales_man product qty
1-Jan-18 xx 01 30
1-Jan-18 xx 01 20
1-Jan-18 xy 01 20
1-Feb-18 xz 02 10
5-Feb-18 xz 02 30
1-Feb-18 xx 01 10
1-Feb-18 xx 01 40
1-Mar-18 xy 03 20
I want to get the following output data format as
Product sales_man Jan Feb Mar
01 xx 50 10 0
01 xy 20 0 0
02 xx 0 0 0
02 xy 0 0 0
02 xz 0 0 0
03 xy 0 0 20
You can use aggregation. Something like this:
select product, salesperson,
sum(case when extract(month from dates) = 1 then qty else 0 end) as jan,
sum(case when extract(month from dates) = 2 then qty else 0 end) as feb
from t
group by product, salesperson;
This uses ANSI SQL date functions, because your database tag is not clear. Date operations can differ by database.
Also, when looking at data by month, typically, you want to take the year into account as well (by filtering or aggregating on the year).
If i got Your problem right, you can try:
SELECT * into #TempTable FROM
(select product, salesman, qty, FORMAT(Dates, 'MMM') as Months from sales
) AS s
PIVOT
(
SUM(qty)
FOR Months in (Jan, Feb)
) AS Pvt
select product, salesman, isnull(Jan, 0) as Jan, isnull(Feb, 0) as Feb from
#TempTable order by product
drop table #TempTable
If you can change your date format from '1-Jan-18' to '1-01-18', then you can do below way
check sql fiddle
http://sqlfiddle.com/#!9/6d3824/2
If you consider below date format
'1-01-18',
'1-01-18',
'1-02-18';
then you can do this query
select product, salesman,
sum(case when extract(month from dates) = 1 then qty else 0 end) as jan,
sum(case when extract(month from dates) = 2 then qty else 0 end) as feb
from sales
group by product, salesman;
Result
product salesman jan feb
1 xx 30 0
1 xz 0 20
2 xy 10 0
For all month
http://sqlfiddle.com/#!9/6d3824/9
for all do query like this,
select product, salesman,
sum(case when extract(month from dates) = 1 then qty else 0 end) as jan,
sum(case when extract(month from dates) = 2 then qty else 0 end) as feb,
sum(case when extract(month from dates) = 3 then qty else 0 end) as mar,
sum(case when extract(month from dates) = 4 then qty else 0 end) as apr,
sum(case when extract(month from dates) = 5 then qty else 0 end) as may,
sum(case when extract(month from dates) = 6 then qty else 0 end) as jun,
sum(case when extract(month from dates) = 7 then qty else 0 end) as jul,
sum(case when extract(month from dates) = 8 then qty else 0 end) as aug,
sum(case when extract(month from dates) = 9 then qty else 0 end) as sep,
sum(case when extract(month from dates) = 10 then qty else 0 end) as oct,
sum(case when extract(month from dates) = 11 then qty else 0 end) as nov,
sum(case when extract(month from dates) = 12 then qty else 0 end) as dece
from sales
group by product, salesman;
Result would be
product salesman jan feb mar apr may jun jul aug sep oct nov dece
1 xx 30 0 0 0 0 0 0 0 0 0 0 0
1 xz 0 20 0 0 0 0 0 0 0 0 0 0
2 xy 10 0 0 0 0 0 0 0 0 0 0 0

Splits one column to multiple column according to data

I have a table which contains data of std attendance of one year
AttID Present absent. leave sick month StdRegNo
1. 23 1 0 0 JAN. 1
2. 25 0 0 0 JAN. 2
3. 23 0 0 0 MAR. 1
4. 21 3 0 1 MAR. 2
SO ON.......
I want result in such a view as bellow:
StdReq month P A L S month P A L S
1. Jan. 23 1 0 0 Mar 23 0 0 0
2. Jan. 25 0 0 0 Mar 21 3 0 1
I need this view for 12 months how can I do this? please help me
You can use a query like this:
select StdRegNo
-- January info
,max(case when [month] = 'JAN' then Present end) JAN_P
,max(case when [month] = 'JAN' then [absent] end) JAN_A
,max(case when [month] = 'JAN' then leave end) JAN_L
,max(case when [month] = 'JAN' then sick end) JAN_S
-- March info
,max(case when [month] = 'MAR' then Present end) MAR_P
,max(case when [month] = 'MAR' then [absent] end) MAR_A
,max(case when [month] = 'MAR' then leave end) MAR_L
,max(case when [month] = 'MAR' then sick end) MAR_S
-- And so on ...
from yourTable
group by StdRegNo;

SQL Server 2005 GROUP BY and COUNT query for each month

I have a SQL Server 2005 table named Rentals:
RentalID
Book
Date
I want to return, using a query, for each book, how many rentals were in each month for a given year.
The results should look something like this:
+--------------------------------+-----+-----+-----+
| Book | Jan | Feb | Mar |
+--------------------------------+-----+-----+-----+
| Isaac Asimov - Foundation | 2 | 5 | 3 |
| H.G. Wells - War of the Worlds | 4 | 3 | 1 |
| Frank Herbert - Dune | 7 | 4 | 6 |
+--------------------------------+-----+-----+-----+
My query so far:
SELECT
Book,
(SELECT COUNT(*) FROM Rentals WHERE month(Date)=1 AND year(Date)=2011),
(SELECT COUNT(*) FROM Rentals WHERE month(Date)=2 AND year(Date)=2011),
(SELECT COUNT(*) FROM Rentals WHERE month(Date)=3 AND year(Date)=2011),
(SELECT COUNT(*) FROM Rentals WHERE month(Date)=4 AND year(Date)=2011),
(SELECT COUNT(*) FROM Rentals WHERE month(Date)=5 AND year(Date)=2011),
(SELECT COUNT(*) FROM Rentals WHERE month(Date)=6 AND year(Date)=2011),
(SELECT COUNT(*) FROM Rentals WHERE month(Date)=7 AND year(Date)=2011),
(SELECT COUNT(*) FROM Rentals WHERE month(Date)=8 AND year(Date)=2011),
(SELECT COUNT(*) FROM Rentals WHERE month(Date)=9 AND year(Date)=2011),
(SELECT COUNT(*) FROM Rentals WHERE month(Date)=10 AND year(Date)=2011),
(SELECT COUNT(*) FROM Rentals WHERE month(Date)=11 AND year(Date)=2011),
(SELECT COUNT(*) FROM Rentals WHERE month(Date)=12 AND year(Date)=2011)
FROM Rentals
GROUP BY Book
This can be written much simpler by using a CASE expression inside of an aggregate function. This process is called PIVOT:
select book,
sum(case when month(Date) = 1 then 1 else 0 end) Jan,
sum(case when month(Date) = 2 then 1 else 0 end) Feb,
sum(case when month(Date) = 3 then 1 else 0 end) Mar,
sum(case when month(Date) = 4 then 1 else 0 end) Apr,
sum(case when month(Date) = 5 then 1 else 0 end) May,
sum(case when month(Date) = 6 then 1 else 0 end) Jun,
sum(case when month(Date) = 7 then 1 else 0 end) Jul,
sum(case when month(Date) = 8 then 1 else 0 end) Aug,
sum(case when month(Date) = 9 then 1 else 0 end) Sep,
sum(case when month(Date) = 10 then 1 else 0 end) Oct,
sum(case when month(Date) = 11 then 1 else 0 end) Nov,
sum(case when month(Date) = 12 then 1 else 0 end) Dec
from Rentals
where year(date) = 2011
group by book;
See SQL Fiddle with Demo. Instead of querying the table multiple times for each column, you use conditional aggregation to get the count for each book during the month and year.
If you use pivot the code is much easier to maintain,
SELECT
BOOK,
[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
BOOK ,
DATEPART(MONTH,[DATE]) AS PER
FROM
Rentals
WHERE
DATEPART(YEAR,[DATE]) = 2014
) AS P PIVOT
(
COUNT(PER) FOR PER IN ([1],[2],[3],[4],[5],[6],[7],[8],[9],[10],[11],[12])
) AS DATA
Simple.