Sign Up

Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.

Have an account? Sign In

Have an account? Sign In Now

Sign In

Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.

Sign Up Here

Forgot Password?

Don't have account, Sign Up Here

Forgot Password

Lost your password? Please enter your email address. You will receive a link and will create a new password via email.

Have an account? Sign In Now

You must login to ask a question.

Forgot Password?

Need An Account, Sign Up Here

Please briefly explain why you feel this question should be reported.

Please briefly explain why you feel this answer should be reported.

Please briefly explain why you feel this user should be reported.

Sign InSign Up

The Archive Base

The Archive Base Logo The Archive Base Logo

The Archive Base Navigation

  • SEARCH
  • Home
  • About Us
  • Blog
  • Contact Us
Search
Ask A Question

Mobile menu

Close
Ask a Question
  • Home
  • Add group
  • Groups page
  • Feed
  • User Profile
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Buy Points
  • Users
  • Help
  • Buy Theme
  • SEARCH
Home/ Questions/Q 8900107
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 15, 20262026-06-15T01:01:31+00:00 2026-06-15T01:01:31+00:00

This is my table to track down the employees off days. This example is

  • 0

This is my table to track down the employees off days. This example is only for one person.

ID   PID     Year       OffDays       DayTypeNumber
------------------------------------------
1     1      2011       10            1
2     1      2011       5             2
3     1      2012       20            1
4     1      2012       3             2

I would like to write such a query that should only show one result for each year with additional column

Year       OffDays(1)    OffDays(2)
------------------------------------------
2011       10            5
2012       20            3
  • 1 1 Answer
  • 0 Views
  • 0 Followers
  • 0
Share
  • Facebook
  • Report

Leave an answer
Cancel reply

You must login to add an answer.

Forgot Password?

Need An Account, Sign Up Here

1 Answer

  • Voted
  • Oldest
  • Recent
  • Random
  1. Editorial Team
    Editorial Team
    2026-06-15T01:01:32+00:00Added an answer on June 15, 2026 at 1:01 am

    You can use the PIVOT function for this:

    select year,
      [1] [OffDays(1)],
      [2] [OffDays(2)]
    from
    (
      select year, offdays, daytypenumber
      from yourtable
    ) src
    pivot
    (
      sum(offdays)
      for daytypenumber in([1], [2])
    ) piv
    

    See SQL Fiddle with Demo

    Result:

    | YEAR | OFFDAYS(1) | OFFDAYS(2) |
    ----------------------------------
    | 2011 |         10 |          5 |
    | 2012 |         20 |          3 |
    

    Or you can use an aggregate function with a CASE statement:

    select year,
      sum(case when daytypenumber = 1 then offdays end) [OffDays(1)],
      sum(case when daytypenumber = 2 then offdays end) [OffDays(2)]
    from yourtable
    group by year
    

    See SQL Fiddle with Demo

    If you only have two types that you are comparing, then you can use subqueries:

    select t1.year, 
      [OffDays(1)],
      [OffDays(2)]
    from
    (
      select sum(offdays) [OffDays(1)], year
      from yourtable
      where daytypenumber = 1
      group by year
    ) t1
    left join
    (
      select sum(offdays) [OffDays(2)], year
      from yourtable
      where daytypenumber = 2
      group by year
    ) t2
      on t1.year = t2.year
    

    See SQL Fiddle with Demo

    The above answers will work great, if you have a known number of values for DayTypeNumber, but if those are unknown then you can use dynamic SQL to generate the PIVOT:

    DECLARE @cols AS NVARCHAR(MAX),
        @colNames AS NVARCHAR(MAX),
        @query  AS NVARCHAR(MAX)
    
    select @cols = STUFF((SELECT distinct ',' + QUOTENAME(DayTypeNumber) 
                        from yourtable
                FOR XML PATH(''), TYPE
                ).value('.', 'NVARCHAR(MAX)') 
            ,1,1,'')
    
    select @colNames = STUFF((SELECT distinct ', ' + QUOTENAME(DayTypeNumber) 
                          +' as [OffDays('+cast(DayTypeNumber as varchar(10))+')]'
                        from yourtable
                FOR XML PATH(''), TYPE
                ).value('.', 'NVARCHAR(MAX)') 
            ,1,1,'')
    
    
    set @query = 'SELECT year,' + @colNames + ' from 
                 (
                    select year, offdays, daytypenumber
                    from yourtable
                ) x
                pivot 
                (
                    sum(offdays)
                    for daytypenumber in (' + @cols + ')
                ) p '
    
    execute(@query)
    

    See SQL Fiddle with Demo

    All of these will produce the same results:

    | YEAR | OFFDAYS(1) | OFFDAYS(2) |
    ----------------------------------
    | 2011 |         10 |          5 |
    | 2012 |         20 |          3 |
    
    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

I've got a table that is supposed to track days and costs for shipping
this table i want to create job_id dynamic Jobs Title text Job Description text
This table is used to store sessions (events): CREATE TABLE session ( id int(11)
In this table how would I use YQL/Xpath to get all data members of
Got this: Table a ID RelatedBs 1 NULL 2 NULL Table b AID ID
Consider this table on SQL Server wordID aliasID value =========================== 0 0 'cat' 1
With this table: CREATE TABLE test_insert ( col1 INT, col2 VARCHAR(10), col3 DATE )
Take this table WORDS WORD Hello Aardvark Potato Dog Cat And this list: ('Hello',
I have table and this table contain result column with some entries. I just
I have just created this table: create table dbo.OrderTypes ( ID smallint primary key

Explore

  • Home
  • Add group
  • Groups page
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Users
  • Help
  • SEARCH

Footer

© 2021 The Archive Base. All Rights Reserved
With Love by The Archive Base

Insert/edit link

Enter the destination URL

Or link to existing content

    No search term specified. Showing recent items. Search or use up and down arrow keys to select an item.