SQL MULTIPLE COUNTS, GROUP BY MONTHS - sql

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))

Related

SQL Server AVG returning 0 for every row

I'm trying to make a somewhat complex query on SQL Server. Had to do a select from a select in order to group by aliases. The problem is that the sum and the count return correct values, but the average returns always zero. if I calculate it as sum(quer.score)/count(quer.score) it also returns all zeros.
What am I doing wrong?
Thanks in advance!
SELECT quer.month, quer.item_type, SUM(quer.score) AS sum_values,COUNT(quer.score) as count_values, avg(quer.score) as final_value FROM (
SELECT
CASE
WHEN MONTH(inte.date) = 1 THEN 'January'
WHEN MONTH(inte.date) = 2 THEN 'February'
WHEN MONTH(inte.date) = 3 THEN 'March'
WHEN MONTH(inte.date) = 4 THEN 'April'
WHEN MONTH(inte.date) = 5 THEN 'May'
WHEN MONTH(inte.date) = 6 THEN 'June'
WHEN MONTH(inte.date) = 7 THEN 'July'
WHEN MONTH(inte.date) = 8 THEN 'Agosto'
WHEN MONTH(inte.date) = 9 THEN 'September'
WHEN MONTH(inte.date) = 10 THEN 'October'
WHEN MONTH(inte.date) = 11 THEN 'November'
WHEN MONTH(inte.date) = 12 THEN 'December'
END AS month,
CASE
WHEN inte.item_id in (SELECT distinct item_id from Items where item_type = 'electronic') THEN 'electronic'
ELSE 'not electronic'
END AS item_type,
CASE WHEN scores.score <7 THEN -1
WHEN scores.score >8 THEN 1
ELSE 0
END AS score
FROM internal_items inte inner join scores_data scores on inte.item_id = scores.item_id
WHERE inte.internal_type = 'dba'
) quer
GROUP BY quer.month, quer.item_type
Result:
month item_type sum count avg
January electronic 0 3 0
January not electronic -2 7 0
February electronic -4 6 0
February not electronic -6 8 0
March electronic -4 5 0
March not electronic -3 6 0
You should use decimals or floats, not integers as data type, e. g. use
CASE WHEN scores.score <7 THEN -1.0
WHEN scores.score >8 THEN 1.0
ELSE 0.0
END AS score
(note the .0 added).
SQL Server uses the same data type as the base column for the aggregate result, and thus, the average would be calculated as int when the input values are ints.

Display the month and how many were hired in that month

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' )

converting number to string with specified order in sql

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.

sql nested sum query

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

SQL displays results for months - how to display only for current year?

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.