SQL Query

Posted on 2006-03-28
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 68

    Accepted Solution


    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]

    Write Comment

    Please enter a first name

    Please enter a last name

    We will never share this with anyone.

    Featured Post

    How your wiki can always stay up-to-date

    Quip doubles as a “living” wiki and a project management tool that evolves with your organization. As you finish projects in Quip, the work remains, easily accessible to all team members, new and old.
    - Increase transparency
    - Onboard new hires faster
    - Access from mobile/offline

    Suggested Solutions

    Title # Comments Views Activity
    Tracking a Database Lock - SQL 2014 10 28
    query Syntax 4 21
    Import csv files to MS SQL 5 34
    ms sql + top 1 for each customer 3 22
    Introduction In my previous article ( I showed you how the XML Source component can be used to load XML files into a SQL Server database, us…
    Everyone has problem when going to load data into Data warehouse (EDW). They all need to confirm that data quality is good but they don't no how to proceed. Microsoft has provided new task within SSIS 2008 called "Data Profiler Task". It solve th…
    Via a live example, show how to setup several different housekeeping processes for a SQL Server.
    Viewers will learn how to use the INSERT statement to insert data into their tables. It will also introduce the NULL statement, to show them what happens when no value is giving for any given column.

    779 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

    Need Help in Real-Time?

    Connect with top rated Experts

    15 Experts available now in Live!

    Get 1:1 Help Now