## Sales figures by financial years

 Author Message stevecurrey Grasshopper Group: General Forum Members Points: 11 Visits: 22 Hi all am having nightmares trying to figure this one out. In the UK our financial year runs from Apr 1st to Mar 31st. I need to create a sales figures report for a financial year for clients from a particular salesperson. The months need to be columns (Apr > Mar) Each row needs to be a client and each cell would be the sum of the invoice totals for that month (0 if none).I have the following tables:monthsmonthid intrealmonthid intmonth(eg 1,4,April)invoicesinvoiceid intinvoicedate datetimeinvoicetotal decimal(19,2)salesperson intclientid intclientsclientid intclientname varchar(150)I envisage getting all 12 months of my financial year from my months table and summing the invoices from particular clients by the month part of the invoice date. Any help would be grreatly appreciated!Thanks in advance,Stephen. Ronald Nijland SSC-Enthusiastic Group: General Forum Members Points: 135 Visits: 108 [EDIT] Lol i see you do not need this Sorry about that [/Edit]Hope this helps you on your way`create table #Month ( [month] varchar(32))insert into #month ([month])select '1' [Month]union allselect '2' [Month]union allselect '3' [Month]union allselect '4' [Month]union allselect '5' [Month]-- drop table #reportcreate table #report (id int identity(1,1) not null, salesperson int)select 'alter table #report add [' + [month] + '] decimal(19,2) default 0' from #month/*alter table #report add [1] decimal(19,2) default 0alter table #report add [2] decimal(19,2) default 0alter table #report add [3] decimal(19,2) default 0alter table #report add [4] decimal(19,2) default 0alter table #report add [5] decimal(19,2) default 0*/-- drop table #invoicescreate table #invoices (invoiceid int, invoicedate datetime, invoicetotal decimal(19,2), salesperson int, clientid int) insert into #invoices (invoiceid, invoicedate, invoicetotal, salesperson, clientid)select 1, cast('2008-1-15' as datetime), 200, 1, 1union allselect 1, cast('2008-2-15' as datetime), 100, 1, 1union allselect 1, cast('2008-3-15' as datetime), 150, 1, 1-- all salespersons you wantinsert into #report (salesperson)select distinct salespersonfrom #invoiceswhere clientid = 1select 'update r set [' + cast(datepart(month, invoicedate) as varchar(32)) + '] = ''' + cast(sum(invoicetotal) as varchar(32)) + ''' from #report r where salesperson = ' + cast(i.salesperson as varchar(32))from #invoices iwhere i.salesperson in (select salesperson from #invoices)group by i.salesperson, datepart(month, i.invoicedate)/*update r set [1] = '200.00' from #report r where salesperson = 1update r set [2] = '100.00' from #report r where salesperson = 1update r set [3] = '150.00' from #report r where salesperson = 1*/select * from #report`you have to make a function that executes the strings ( i have added the SQL strings so you can execute them by hand for this example) Its not the complete thing you want but it should get you started on how you can do it stevecurrey Grasshopper Group: General Forum Members Points: 11 Visits: 22 Hi many thanks for that reply - it's gone completely over my head though I'm afraid I've never used functions. When I was developing this app I pretty much knew I'd hit a brick wall when it came to this report I thought that having a months table may help with SSRS creating a matrix.Is there not just a way I can select every month from my months table and for each month a list of clients with invoices in that month being summed?Thanks,Stephen. Ronald Nijland SSC-Enthusiastic Group: General Forum Members Points: 135 Visits: 108 In my first example it will come up with the months as columsn`truncate table #invoicesinsert into #invoices (invoiceid, invoicedate, invoicetotal, salesperson, clientid)select 1, cast('2008-1-15' as datetime), 200, 1, 1union allselect 1, cast('2008-2-15' as datetime), 100, 1, 1union allselect 1, cast('2008-3-15' as datetime), 150, 1, 1union allselect 2, cast('2008-1-15' as datetime), 1200, 2, 1union allselect 2, cast('2008-2-15' as datetime), 1100, 2, 1union allselect 2, cast('2008-3-15' as datetime), 1150, 2, 1select salespersons.salesperson, #month.[month], sum(i.invoicetotal) totaalfrom #month, (select distinct salespersonfrom #invoices) salespersons, #invoices iwhere i.salesperson = salespersons.salespersonand #month.[month] = datepart(month, i.invoicedate)group by salespersons.salesperson, #month.[month]`if you use the same tables and execute this query it will come up with month / salesperson / sum(invoices) per salesperson and month... I am not familiar with SSRS so i cannot help you with that. stevecurrey Grasshopper Group: General Forum Members Points: 11 Visits: 22 No probs Have pretty much given up and gone back to manually creating the report in excel!Thanks anyway!Stephen stevecurrey Grasshopper Group: General Forum Members Points: 11 Visits: 22 Hi - just had a brainwave - I'm nearly there I *think*I have added a months table with monthid, realmonthid, monthname. April being the first month of the financial year, it would have monthid 1, realmonthid 4. I join this table to the invoices table on the month part of the invoice date. I set the datefrom and dateto to the beginning and end of the financial year in question. Am I completely off the mark here? I have the following query now:SELECT months.monthid, months.month, months.realmonthid, SUM(invoices.cinvoicesubtotal) AS Total, clients.clientname, invoices.invoicedateFROM months INNER JOINinvoices ON months.realmonthid = MONTH(invoices.invoicedate) INNER JOINclients ON invoices.clientid = clients.clientidWHERE invoices.invoicedate BETWEEN '01-apr-2007' AND '31-mar-2008'GROUP BY months.monthid, months.month, months.realmonthid, clients.clientname, invoices.invoicedateORDER BY months.monthidThis works but only brings up the months where invoices have been placed - how would I get all of the other months to display properly? Ronald Nijland SSC-Enthusiastic Group: General Forum Members Points: 135 Visits: 108 left outer join on the months table should work... This looks familiar stevecurrey Grasshopper Group: General Forum Members Points: 11 Visits: 22 lol sorry I'm not too hot on SQL and as soon as I saw temp tables and functions my eyes glazed over...I have:SELECT months.monthid, months.month, months.realmonthid, SUM(invoices.cinvoicesubtotal) AS Total, clients.clientname, invoices.invoicedateFROM months LEFT OUTER JOINinvoices ON months.realmonthid = MONTH(invoices.invoicedate) INNER JOINclients ON invoices.clientid = clients.clientidWHERE invoices.invoicedate BETWEEN '01-apr-2007' AND '31-mar-2008'GROUP BY months.monthid, months.month, months.realmonthid, clients.clientname, invoices.invoicedateORDER BY months.monthidBut still only showing the months with invoices - I definitely need some kind of refresher course! Ronald Nijland SSC-Enthusiastic Group: General Forum Members Points: 135 Visits: 108 In a left outer join all values not avalable will become null and you are trying an inner join on the client table with a null value. That is why the rows are missing... add a left outer join on the client table.. Also if you use sum(isnull(invoice.total, 0)) it will be 0 in stead of null in the outer join rows stevecurrey Grasshopper Group: General Forum Members Points: 11 Visits: 22 Hi Thanks again for the reply - I now have the following:SELECT months.monthid, months.month, months.realmonthid, isnull(SUM(invoices.cinvoicesubtotal),0) AS Total, isnull(clients.clientname,'')FROM months LEFT OUTER JOINinvoices ON months.realmonthid = MONTH(invoices.invoicedate) LEFT OUTER JOINclients ON invoices.clientid = clients.clientidWHERE invoices.invoicedate BETWEEN '01-apr-2007' AND '31-mar-2008'GROUP BY months.monthid, months.month, months.realmonthid, clients.clientname, invoices.invoicedateORDER BY months.monthidUnfortunately I'm still only being presented with the months with invoices in them

## Permissions

 You can't post new topics. You can't post topic replies. You can't post new polls. You can't post replies to polls. You can't edit your own topics. You can't delete your own topics. You can't edit other topics. You can't delete other topics. You can't edit your own posts. You can't edit other posts. You can't delete your own posts. You can't delete other posts. You can't post events. You can't edit your own events. You can't edit other events. You can't delete your own events. You can't delete other events. You can't send private messages. You can't send emails. You can read topics. You can't vote in polls. You can't upload attachments. You can download attachments. You can't post HTML code. You can't edit HTML code. You can't post IFCode. You can't post JavaScript. You can post emoticons. You can't post or upload images.

##### Select a forum
 SQL Server 2017      SQL Server 2017 - Administration      SQL Server 2017 - Development SQL Server 2016      SQL Server 2016 - Administration      SQL Server 2016 - Development and T-SQL SQL Server 2014      Administration - SQL Server 2014      Development - SQL Server 2014 SQL Server 2012      SQL 2012 - General      SQL Server 2012 - T-SQL SQL Server vNext      SQL Server 15 - Administration      SQL Server 15 - Development SQL Server 2008      SQL Server 2008 - General      T-SQL (SS2K8)      June 2007 CTP      Working with Oracle      July CTP      SQL Server Newbies      Security (SS2K8)      SQL Server 2008 High Availability      SQL Server 2008 Administration      Data Corruption (SS2K8 / SS2K8 R2)      SQL Server 2008 Performance Tuning Cloud Computing      SQL Azure - Development      SQL Azure - Administration      Amazon AWS and other cloud vendors      General Cloud Computing Questions      CosmosDB      Azure Data Lake      Azure Machine Learning Reporting Services      Reporting Services      Reporting Services 2005 Administration      Reporting Services 2005 Development      Reporting Services 2008/R2 Administration      Reporting Services 2008 Development      SSRS 2012      SSRS 2014      SSRS 2016 Programming      Connecting      General      SMO/RMO/DMO      XML      Service Broker      Powershell      Testing      TFS/Data Dude/DBPro      SSDT      Continuous Integration, Deployment, and Delivery      R Services and R Language Data Warehousing      Integration Services      Strategies and Ideas      Analysis Services      Data Transformation Services (DTS)      Performance Point      Data Mining      PowerPivot      R language      Machine Learning Database Design      Disaster Recovery      Design Ideas and Questions      Relational Theory      Hardware      Virtualization      Security and Auditing SQLServerCentral.com      Anything that is NOT about SQL!      Contests!      Editorials      SQLServerCentral.com Announcements      SQLServerCentral.com Website Issues      Suggestions      Tag Issues with Content      Podcast Feedback      SQLServerCentral.com Test Forum      Articles Requested SQL Server 2005      Administering      Backups      Business Intelligence      CLR Integration and Programming.      Data Corruption      Development      Working with Oracle      SQL Server 2005 Compact Edition      SQL Server 2005 General Discussion      SQL Server 2005 Security      SQL Server 2005 Strategies      SS2K5 Replication      SQL Server Express      SQL Server 2005 Performance Tuning      SQL Server 2005 Integration Services      T-SQL (SS2K5)      SQL Server Newbies SQL Server 7,2000      Administration      Backups      Data Corruption      General      Globalization      In The Enterprise      Working with Oracle      Security      Strategies      SQL Server Newbies      Service Packs      SQL Server CE      Performance Tuning      Replication      Sarbanes-Oxley      T-SQL      SQL Server Agent SQL Server and other platforms      MySQL      Oracle      PostgreSQL      DB2      SQL Server and Sharepoint Older Versions of SQL (v6.5, v6.0, v4.2)      Older Versions of SQL (v6.5, v6.0, v4.2) Career      Certification      Employers and Employees      Events      Job Postings      Resumes and Job Hunters      Presentations and Speaking      Retired Members Testing Center      SQL Server Security Skills      Question of the Day (QOD) Microsoft Access      Microsoft Access Products and Books      Third Party Products         SQLCentric         Extreme Technologies.         Innovartis         Embarcadero         SQL Sentry         Sonasoft         Golden Gate Software         Idera         Lumigent         Red Gate Software         ApexSQL         Quest Software         CA      Discussions about Books         Discuss Programming Books          Discuss XML Books          Discuss T-SQL Books          Discuss Data Warehousing Books          Discuss DTS Books          Discuss SQL Server 2000 Books         Discuss SQL Server 7.0 Books Notification Services      Administration Article Discussions Future Versions      SQL 12