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

  • Home
  • SEARCH
  • 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 9083823
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 16, 20262026-06-16T20:51:47+00:00 2026-06-16T20:51:47+00:00

I have a table Pet.Info like the following in SQL Server : pet age

  • 0

I have a table Pet.Info like the following in SQL Server :

pet      age ...
--------------------
dog       4
cat       3
pig       2
dog       3
...

Now, I want to convert it into a table to calculate the average ages of dog and cat, and the average of all the others. Like the following:

dog    cat   others
-------------------------
3.5      3       2

Is it possible to do it with PIVOT?

Here is what I have get, but there seems no way to make it for the column of others

SELECT * FROM 
(SELECT * FROM Pet.Info) AS B
PIVOT (AVG(age) FOR pet IN ([dog],[cat])) AS C

Thanks for advises in advance.

  • 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-16T20:51:48+00:00Added an answer on June 16, 2026 at 8:51 pm

    Try this:

    SELECT * 
    FROM (  SELECT  CASE WHEN pet NOT IN ('dog','cat') THEN 'others' ELSE pet END pet, 
                    age
            FROM Pet.Info) AS B
    PIVOT (AVG(age) FOR pet IN ([dog],[cat],[others])) AS C
    
    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

I have a table like the following: owner Pet __________ Bob Joe Sam Rob
I have a table as follows OWNERID PET_QUANTITY PET_TYPE PET_TYPE is either DOG, CAT,
For example, I have a pet table and a lost pets table. I want
I have a database that contains a table of deposits (security deposits, pet deposits,
I have a database table with thousands of rows. I would like to run
I have a table Owners, and Pets. Each Owner has_many pets, and each pet
I have table, and I want check/uncheckcheck box not only by clicking on checkbox,
i have table name is ads like that: id ad_country ad_gender ad_birthday 1 05
I want to write documentation on my pet project. I have 30 tables and
I have the following table of people and their birthdays: name birthday ---------------------- yannis

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.