sql nested sum query - sql

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

Related

How to the add balance in the next field amount, per Vendorname , per month/year

Good day guys . In my query I want to add the current balance (from previous payable )to the next field amount and subtract it to the payment made (paid field)
sample codes
select a.vendorname
,DATENAME(month ,(cdvdate)) as Month
,year(cdvdate) as Year
, sum(b.credit) as Amount
, sum(b.debit) as Paid
, sum(b.credit)- sum(b.debit) as balance
from (select cdvno,acct
, sum(case when credit = 0 then debit else 0 end) as debit
, sum(case when debit = 0 then credit else 0 end)as credit
, trantype
from cdvdtl
group by cdvno,acct, trantype
)b
left join cdvhdr a
on b.cdvno = a.cdvno and b.trantype = a.trantype
left join account c on b.acct = c.acct
where b.acct='2122102'
group by a.vendorname,year(cdvdate),DATENAME(month ,(cdvdate))
order by Vendorname,case year(cdvdate)
when '2016' then 1
when '2017' then 2
when '2018' then 3
when '2019' then 4
end
,case DATENAME(month ,(cdvdate))
when 'January' then 1
when 'February' then 2
when 'March' then 3
when 'April' then 4
when 'May' then 5
when 'June' then 6
when 'July' then 7
when 'August' then 8
when 'September' then 9
when 'October' then 10
when 'November' then 11
when 'December' then 12
end
I tried using row number but it doesn't work the way i want
i want the result to be something like this . Thank you so much enlighten me please
Try this:
with cte as
(
select t2.vendorname as vendorname,
convert(char(7), t1.cdvdate, 120) as YYYMM,
DATEPART(month, t1.cdvdate) as MM,
datepart(year, t1.cdvdate) as YYYY,
sum(case when t1.credit = 0 then t1.debit else 0 end) as debit,
sum(case when t1.debit = 0 then t1.credit else 0 end) as credit,
ROW_NUMBER() over (
partition by vendorname
order by convert(char(7), t1.cdvdate, 120)
) as rn
from cdvdtl t1
inner join cdvhdr t2 on t2.cdvno = t1.cdvno
and t2.trantype = t1.trantype
group by t2.vendorname,
convert(char(7), t1.cdvdate, 120),
DATEPART(month, t1.cdvdate),
datepart(year, t1.cdvdate)
)
select t1.vendorname as vendorname,
t1.MM as MM,
t1.YYYY as YYYY,
t1.credit as Amount,
t1.debit as Paid,
t1.debit-t1.credit as Balance,
t2.debit-t2.credit as BalanceBegin
from cte t1
left join cte t2 on t2.vendorname = t1.vendorname
and t2.rn = t1.rn-1
order by t1.vendorname,
t1.YYYMM

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

SQL: Add Column from table to another

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

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.

SQL MULTIPLE COUNTS, GROUP BY MONTHS

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