[Webinar] Learn how to a build a cloud-first strategyRegister Now


SQL Query

Posted on 2006-03-28
Medium Priority
Last Modified: 2011-10-03
I can't figure out a SQL query that I'm sure will be very simple

I have a Company Table and a Financial Year Table

Company Table contains Company Name and ID

The Financial Year table contains  a company ID column and a Financial year column and a finance column for each company for that year

I want to display columns for

Company Name | Finance Figure (This Year) | Finance Figure (Last Year)

Question by:Milkybar-kid
1 Comment
LVL 70

Accepted Solution

Scott Pletcher earned 2000 total points
ID: 16315535

SELECT ct.[Company Name], fy.[Financial Year],
    SUM(CASE WHEN [Financial Year] = YEAR(GETDATE) THEN fy.col1 + fy.col2 + ... ELSE 0 END) AS [Finance Total, Curr Year],
    SUM(CASE WHEN [Financial Year] = YEAR(GETDATE) - 1 THEN fy.col1 + fy.col2 + ... ELSE 0 END) AS [Finance Total, Prior Year]
FROM [Company Table] ct
INNER JOIN [Financial Year] fy ON fy.[Company ID] = ct.[Company ID]
WHERE fy.[Financial Year] IN (YEAR(GETDATE()) - 1, YEAR(GETDATE()))
GROUP BY ct.[Company Name], fy.[Financial Year]

Featured Post

 [eBook] Windows Nano Server

Download this FREE eBook and learn all you need to get started with Windows Nano Server, including deployment options, remote management
and troubleshooting tips and tricks

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

Ever wondered why sometimes your SQL Server is slow or unresponsive with connections spiking up but by the time you go in, all is well? The following article will show you how to install and configure a SQL job that will send you email alerts includ…
Ready to get certified? Check out some courses that help you prepare for third-party exams.
This video shows how to set up a shell script to accept a positional parameter when called, pass that to a SQL script, accept the output from the statement back and then manipulate it in the Shell.
Via a live example, show how to extract insert data into a SQL Server database table using the Import/Export option and Bulk Insert.
Suggested Courses

865 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question