0

I have to sum invoice total from a table in sql server 2008 based of two given dates let suppose 01/01/2012 to 07/31/2013 in terms of each months in given dates.

Like this..

   Jan 12  30000
   Feb 12  40000

   July 13 45000

Your help is highly appreciated.

1 Answer 1

2
SELECT DATEPART(MONTH, invoiceDate), DATEPART(YEAR, invoiceDate), COUNT(1) 
FROM table1
where invoiceDate between '1/1/2012' and '7/31/2013'
GROUP BY DATEPART(MONTH, invoiceDate), DATEPART(YEAR, invoiceDate)
4
  • Thanks for your reply.I am using this qurey .this is correct but when i filter for date YTD year to date means 01/01/2013 to current date the filter is not correct,it is showing previous year records as well.Kindly suggest here..
    – Vikram SE
    Jul 9, 2013 at 16:22
  • SELECT year(INV.InvoiceDate) as 'YEAR',DATENAME(Month,INV.InvoiceDate) as 'MONTH',sum(invd.Amount) as TotalSalePrice FROM Invoice inv WHERE ( inv.CompanyId=5 and inv.IsDeleted='false' and invd.IsPurchased='TRUE')AND RSA.RSAID=12 AND (CONVERT(VARCHAR(10),INV.InvoiceDate,101) BETWEEN CONVERT(VARCHAR(10),DATEADD(yy, DATEDIFF(yy,0,getdate()), 0),101) AND CONVERT(VARCHAR(10),getdate(),101)) GROUP BY YEAR(inv.InvoiceDate),DATENAME(Month,inv.InvoiceDate)--,INV.InvoiceDate order by YEAR(inv.InvoiceDate)asc, DATENAME(Month,inv.InvoiceDate)desc
    – Vikram SE
    Jul 9, 2013 at 16:26
  • It's because you're comparing strings when you do convert(varchar(10). What you have to do is wrap that convert in another convert to change it to be an integer so that you're comparing numbers
    – Avitus
    Jul 9, 2013 at 17:03
  • I have one more query ..How to get all week of the current month.Let suppose for the month of june 13 .I have to show all the sum of invoice on weekly basis for that month.week 1 200 week 2 500 week 3 600 week 4 500 and week 5 if any then 400.
    – Vikram SE
    Jul 9, 2013 at 18:25

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Not the answer you're looking for? Browse other questions tagged or ask your own question.