I'm trying to list how many were hired in each month. I'm not sure where I went wrong but I get 23 rows and get duplicate months.
SELECT DISTINCT COUNT(E.BusinessEntityID) AS NumberOfEmployees,
CASE
WHEN DATEPART(MONTH,E.HireDate) = 1 THEN 'Janurary'
WHEN DATEPART(MONTH,E.HireDate) = 2 THEN 'Feburary'
WHEN DATEPART(MONTH,E.HireDate) = 3 THEN 'March'
WHEN DATEPART(MONTH,E.HireDate) = 4 THEN 'April'
WHEN DATEPART(MONTH,E.HireDate) = 5 THEN 'May'
WHEN DATEPART(MONTH,E.HireDate) = 6 THEN 'June'
WHEN DATEPART(MONTH,E.HireDate) = 7 THEN 'July'
WHEN DATEPART(MONTH,E.HireDate) = 8 THEN 'August'
WHEN DATEPART(MONTH,E.HireDate) = 9 THEN 'September'
WHEN DATEPART(MONTH,E.HireDate) = 10 THEN 'October'
WHEN DATEPART(MONTH,E.HireDate) = 11 THEN 'November'
WHEN DATEPART(MONTH,E.HireDate) = 12 THEN 'December'
ELSE 'Unknown'
END AS Month
FROM HumanResources.Employee AS E
GROUP BY
E.HireDate;
use DATENAME function for month name and group by for month-wise count
SELECT COUNT(E.BusinessEntityID) AS NumberOfEmployees,
DATENAME(mm,E.HireDate) from t
group by DATENAME(mm,E.HireDate)
But I'm not sure you need year or not ,cause if you need year then it will also come to selection and group by
have you tried to_char(e.HireDate, month) as hire_month
select count(e.businessentityid) empcount, to_char(e.HireDate,'Month') monthname from employee e group by to_char(e.HireDate,'Month')
order by to_date( monthname, 'Month' )
Related
I have two tables that count records and displays the count by month.
I want to show both counts on the same table, but I'm not sure how to combine them.
I just want to add the "IM Count" column to the first table, to the right of "CR Count."
Below is the code for the first table. The second table is similar code, but it draws its data from another table. My main issue is that I don't have a column that matches between tables. (The Year and Month columns technically do match, but only because they are both counts per month.)
Use sm
select
year(planned_start) Year,
Case
When month(planned_start) = 1 then 'January'
When month(planned_start) = 2 then 'February'
When month(planned_start) = 3 then 'March'
When month(planned_start) = 4 then 'April'
When month(planned_start) = 5 then 'May'
When month(planned_start) = 6 then 'June'
When month(planned_start) = 7 then 'July'
When month(planned_start) = 8 then 'August'
When month(planned_start) = 9 then 'September'
When month(planned_start) = 10 then 'October'
When month(planned_start) = 11 then 'November'
When month(planned_start) = 12 then 'December'
end as Month,
count(*) 'CR Count'
from dbo.cm3rm1
where planned_start between dateadd(Year,-1,getdate()) and getdate()
and
category !='OAS Normal'
group by year(planned_start), month(planned_start)
order by year(planned_start), month(planned_start)
Well, for the "idea" :
SELECT tmp_CR.Year,
Case
When tmp_CR.month = 1 then 'January'
When tmp_CR.month = 2 then 'February'
When tmp_CR.month = 3 then 'March'
When tmp_CR.month = 4 then 'April'
When tmp_CR.month = 5 then 'May'
When tmp_CR.month = 6 then 'June'
When tmp_CR.month = 7 then 'July'
When tmp_CR.month = 8 then 'August'
When tmp_CR.month = 9 then 'September'
When tmp_CR.month = 10 then 'October'
When tmp_CR.month = 11 then 'November'
When tmp_CR.month = 12 then 'December' end as month,
tmp_CR.CR_Count,
tmp_IM.IM_Count
FROM (
select
year(planned_start) Year,
month(planned_start) as Month,
count(*) as CR_Count
from dbo.TABLE_1
where planned_start between dateadd(Year,-1,getdate()) and getdate()
and
category !='OAS Normal'
group by year(planned_start), month(planned_start)
) tmp_CR
INNER JOIN (
select
year(planned_start) Year,
month(planned_start) as Month, as Month,
count(*) as IM_Count
from dbo.TABLE_2
where planned_start between dateadd(Year,-1,getdate()) and getdate()
and
category !='OAS Normal'
group by year(planned_start), month(planned_start)
) tmp_IM ON tmp_CR.year = tmp_IM.year and tmp_CR.month = tmp_IM.month
ORDER BY tmp_CR.Year, tmp_CR.month
I need the records order by month on school year, ex
August
September
October
November
December
January
February
March
April
May
June
To get that order i found this order by but is returning the records with numbers and i need the name of the month. How can i get the name of the month without affecting the order?
SELECT CASE
When G.MO = 8 then 'August'
When G.MO = 9 then 'September'
When G.MO = 10 then 'October'
When G.MO = 11 then 'November'
When G.MO = 12 then 'December'
When G.MO = 1 then 'January'
When G.MO = 2 then 'February'
When G.MO = 3 then 'March'
When G.MO = 4 then 'April'
When G.MO = 5 then 'May'
When G.MO = 6 then 'June'
When G.MO = 7 then 'July'
end as [Month]
,...
from TABLE
group by [Month]
order by convert(nvarchar,(CASE WHEN CAST(LEFT(G.MO, 2) AS int) >= 8 THEN CAST(LEFT(G.MO, 2) AS int) - 8 ELSE CAST(LEFT(G.MO, 2) AS int) + 4 END
Usual way to rotate a sequence is mod function.
ordre by (g.mod+4)%12
--(8+4)%12=0
--(9+4)%12=1
--...
--(7+4)%12=11
Just use a simple comparison to put the months after august first:
order by (case when g.mo >= 8 then 1 else 2 end),
g.mo
Also, you can simplify the naming by doing:
select datename(month, cast(replace('2000-#mon-01', '#mon', g.mo) as date))
The cast() is actually not necessary, but I much prefer explicit conversion to implicit conversion.
I have a single table that I need to create three sum's based off the month on an invoice.
The sums are calculated using the first three letters of an 'invoice_number' field, Then adding all the 'Invoice_Amount' for that specific invoice type.
Example of data
Invoice_Number - MSP-1111
Invoice Amount - $2100.00
I want to calculate this based off months so it formats as example
Year | Month | Total MSP Invoices| Total PS Invoices | Total App Invoice
I am able to get the months, however the total is the same for all months, not specific to that month. Below is the query I am using.
I want to display the last 12 months.
select year(date_invoice) as Year,
Case month(date_invoice)
When 1 Then 'Jan'
When 2 Then 'Feb'
When 3 Then 'March'
When 4 Then 'April'
When 5 Then 'May'
When 6 Then 'June'
When 7 Then 'July'
When 8 Then 'Aug'
When 9 Then 'Sept'
When 10 Then 'Oct'
When 11 Then 'Nov'
When 12 then 'Dec'
End
as Month,
(Select sum(invoice_amount) from invoices where Invoice_number like 'MSP%') as 'MSP',
(Select sum(invoice_amount) from invoices where Invoice_number like 'PS%') as'PS',
(Select sum(invoice_amount) from invoices where Invoice_number like 'APP%') as 'App'
from invoices
Where convert(nvarchar(50), date_invoice,100) > DATEADD(month, -12, getdate())
Group by year(date_invoice), month(date_invoice)
Order by year(date_invoice), month(date_invoice)
Any help would be awesome!
You should be able to use the case statement in the group by which would group it right
select year(date_invoice) as Year,
Case month(date_invoice)
When 1 Then 'Jan'
When 2 Then 'Feb'
When 3 Then 'March'
When 4 Then 'April'
When 5 Then 'May'
When 6 Then 'June'
When 7 Then 'July'
When 8 Then 'Aug'
When 9 Then 'Sept'
When 10 Then 'Oct'
When 11 Then 'Nov'
When 12 then 'Dec'
End
as Month,
(Select sum(invoice_amount) from invoices where Invoice_number like 'MSP%') as 'MSP',
(Select sum(invoice_amount) from invoices where Invoice_number like 'PS%') as'PS',
(Select sum(invoice_amount) from invoices where Invoice_number like 'APP%') as 'App'
from invoices
Where convert(nvarchar(50), date_invoice,100) > DATEADD(month, -12, getdate())
Group by year(date_invoice), Case month(date_invoice)
When 1 Then 'Jan'
When 2 Then 'Feb'
When 3 Then 'March'
When 4 Then 'April'
When 5 Then 'May'
When 6 Then 'June'
When 7 Then 'July'
When 8 Then 'Aug'
When 9 Then 'Sept'
When 10 Then 'Oct'
When 11 Then 'Nov'
When 12 then 'Dec'
End
Order by year(date_invoice), month(date_invoice)
Have a derived table where you put the CASE expression to get month names.
Use CASE expressions to do conditional SUM's.
select year, month,
sum(case when Invoice_number like 'MSP%' then invoice_amount end) as 'MSP',
sum(case when Invoice_number like 'PS%' then invoice_amount end) as 'PS',
sum(case when Invoice_number like 'APP%' then invoice_amount end) as 'APP'
from
(
select date_invoice,
Invoice_number
year(date_invoice) as Year,
case month(date_invoice)
When 1 Then 'Jan'
When 2 Then 'Feb'
When 3 Then 'March'
When 4 Then 'April'
When 5 Then 'May'
When 6 Then 'June'
When 7 Then 'July'
When 8 Then 'Aug'
When 9 Then 'Sept'
When 10 Then 'Oct'
When 11 Then 'Nov'
When 12 then 'Dec'
End as Month,
invoice_amount
from invoices
where convert(nvarchar(50), date_invoice,100) > DATEADD(month, -12, getdate())
) as dt
Group by year, month
Order by year, month
select year, month,
sum(case when Invoice_number like 'MSP%' then invoice_amount end) as 'MSP',
sum(case when Invoice_number like 'PS%' then invoice_amount end) as 'PS',
sum(case when Invoice_number like 'APP%' then invoice_amount end) as 'APP'
from
(
select date_invoice,
Invoice_number
year(date_invoice) as Year,
case month(date_invoice)
When 1 Then 'Jan'
When 2 Then 'Feb'
When 3 Then 'March'
When 4 Then 'April'
When 5 Then 'May'
When 6 Then 'June'
When 7 Then 'July'
When 8 Then 'Aug'
When 9 Then 'Sept'
When 10 Then 'Oct'
When 11 Then 'Nov'
When 12 then 'Dec'
End as Month,
invoice_amount
from invoices
where convert(nvarchar(50), date_invoice,100) > DATEADD(month, -12, getdate())
) as dt
Group by year, month
Order by year, month
My SQL expression which works fine displays results for months and it will be OK for this year.But after one year I know it will include results from this year. How to show results only from current year?
select case month(timestamp_iso(STATUSDATE))
when 1 then 'January'
when 2 then 'February'
when 3 then 'March'
when 4 then 'April'
when 5 then 'May'
when 6 then 'Jun'
when 7 then 'July'
when 8 then 'August'
when 9 then 'September'
when 10 then 'October'
when 11 then 'November'
when 12 then 'December'
end as Month,
count (case when service='ADSL' then 1 end) as ADSL,
count (*) as ALL
from INCIDENT
group by month(timestamp_iso(STATUSDATE))
Thanks
add a whereclause to the end:
where year(STATUSDATE) = year(current_timestamp)
Just add a where clause!
And you can use monthname to get what you're doing in the case:
select MONTHNAME(timestamp_iso(STATUSDATE)) as Month,
count (case when service='ADSL' then 1 end) as ADSL,
count (*) as ALL
from INCIDENT
where year(STATUSDATE) = year(current_timestamp)
group by month(timestamp_iso(STATUSDATE))
For more info, check THIS or THIS.
I have table:
STATUS DEFECT DATE
CLOSED IP 01.01.2012
CLOSED TV 03.03.2012
CLOSED ADSL 05.05.2012
CLOSED ADSL 11.01.2012
CLOSED TV 15.01.2012
NEW TV
NEW TV
I want to group this by months with count for each specific DEFECT. Status which is considered is CLOSED
Resulting table I would like to be:
MONTH TV ADSL IP
January 1 1 1
March 1 0 0
May 0 1 0
I am using db2 database so the part for displaying months which works is:
select case month(timestamp_iso(DATE))
when 1 then 'January'
when 2 then 'February'
when 3 then 'March'
when 4 then 'April'
when 5 then 'May'
when 6 then 'Jun'
when 7 then 'July'
when 8 then 'August'
when 9 then 'September'
when 10 then 'October'
when 11 then 'November'
when 12 then 'December'
end as Month
from TABLE
where STATUS='CLOSED'
group by month(timestamp_iso(DATE))
order by month(timestamp_iso(DATE))
So I just need to add this part for counting.Thanks
You might count defect categories using another case statement that would restrict count to defects in one category:
select case month(timestamp_iso(DATE))
when 1 then 'January'
when 2 then 'February'
when 3 then 'March'
when 4 then 'April'
when 5 then 'May'
when 6 then 'Jun'
when 7 then 'July'
when 8 then 'August'
when 9 then 'September'
when 10 then 'October'
when 11 then 'November'
when 12 then 'December'
end as Month,
count (case when defect = 'TV' then 1 end) TV,
count (case when defect = 'ADSL' then 1 end) ADSL,
count (case when defect = 'IP' then 1 end) IP
from TABLE
where STATUS='CLOSED'
group by month(timestamp_iso(DATE))
order by month(timestamp_iso(DATE))