Help with T-SQL (2008R2)

  • Hi,
    I'm creating a report and am trying to work out how to output the following info.
    First a bit about the data.
    We have a column called TotalProfiledHours which represents how many hours a week an employee is contracted to work.
    We have another column called ALTotalUnits which contains the annual leave balance for an employee.
    I need to report on the balances and whether they fall above or below 3 weeks.
    So for an employee who has a TotalProfiledHours of 40 then 3 weeks or over would be 120 or more in the ALTotalUnits column.
    An employee who has a TotalProfiledHours of 20 would have 60 or more in the ALTotalUntils column to be considered 3 weeks or over.
    This is how I want to output the data....

    Name | TotalProfiledHours | Higher than 3 weeks | Lower than 3 weeks
    John | 40 | 121 | - 
    Jane | 40 |  -  | 80
    Fred | 20 |  -  | 25
    Sue  | 20| 70 | -

    Can't seem to get my head around the required CASE (?) statement required.

  • CASE WHEN ALTotalUnits / TotalProfiledHours >= 3 THEN <higher than 3 weeks>
    CASE WHEN ALTotalUnits / TotalProfiledHours < 3 THEN <lower than 3 weeks>

    SQL DBA,SQL Server MVP(07, 08, 09) A socialist is someone who will give you the shirt off *someone else's* back.

  • How can I output the actual values under the column headings as shown below?

  • If you want blanks rather than NULL, then:

    CASE WHEN ALTotalUnits / TotalProfiledHours >= 3 THEN CAST(ALTotalUnits AS varchar(30)) ELSE '' END AS [Higher than 3 Weeks],
    CASE WHEN ALTotalUnits / TotalProfiledHours < 3 THEN '' ELSE CAST(ALTotalUnits AS varchar(30)) END AS [Lower than 3 Weeks]

    SQL DBA,SQL Server MVP(07, 08, 09) A socialist is someone who will give you the shirt off *someone else's* back.

Viewing 4 posts - 1 through 3 (of 3 total)

You must be logged in to reply to this topic. Login to reply