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 7527169
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 30, 20262026-05-30T04:03:09+00:00 2026-05-30T04:03:09+00:00

I am trying to build a table with two cross tabs and multiple columns.

  • 0

I am trying to build a table with two cross tabs and multiple columns. I have this so far, and when I run it

    create table test2(city nvarchar(10), race nvarchar(30), sex nvarchar(10), age int)
    insert into test2 values ('Austin',  'African-American', 'male', 21)
    insert into test2 values ('Austin',  'Asian', 'female', 22)
    insert into test2 values ('Austin',  'Caucasian', 'male', 23)
    insert into test2 values ('Austin',  'Hispanic', 'female', 24)
    insert into test2 values ('Austin',  'African-American', 'Unknown', 25)
    insert into test2 values ('Austin',  'Asian', 'male', 26)
    insert into test2 values ('Austin',  'Caucasian', 'female', 27)
    insert into test2 values ('Austin',  'Hispanic', 'Unknown', 28)
    insert into test2 values ('Austin',  'Asian', 'male', 29)
    insert into test2 values ('Austin',  'Caucasian', 'female', 31)
    insert into test2 values ('Dallas',  'Hispanic', 'Unknown', 32)
    insert into test2 values ('Dallas',  'African-American', 'male', 33)
    insert into test2 values ('Dallas',  'Asian', 'female', 34)
    insert into test2 values ('Dallas',  'Caucasian', 'Unknown', 35)
    insert into test2 values ('Dallas',  'Hispanic', 'male', 500)
    insert into test2 values ('Dallas',  'African-American', 'female', 36)
    insert into test2 values ('Dallas',  'Asian', 'Unknown', 37)
    insert into test2 values ('Dallas',  'Caucasian', 'male', 38)
    insert into test2 values ('Dallas',  'Hispanic', 'female', 39)
    insert into test2 values ('Dallas',  'African-American', 'Unknown', 41)
    insert into test2 values ('Houston',  'Asian', 'male', 42)
    insert into test2 values ('Houston',  'Caucasian', 'female', 43)
    insert into test2 values ('Houston',  'Hispanic', 'Unknown', 44)
    insert into test2 values ('Houston',  'African-American', 'male', 45)
    insert into test2 values ('Houston',  'Asian', 'female', 46)
    insert into test2 values ('Houston',  'Caucasian', 'Unknown', 47)
    insert into test2 values ('Houston',  'Hispanic', 'male', 48)
    insert into test2 values ('Houston',  'African-American', 'female', 49)
    insert into test2 values ('Houston',  'Asian', 'Unknown', 51)
    insert into test2 values ('Houston',  'Caucasian', 'male', 52)

    SELECT  *
    FROM
    (
    SELECT  a.city, a.sex
    FROM [AdventureWorksDW].[dbo].[test2] a

    ) p
    PIVOT
    (
    COUNT (sex)
    FOR sex
    IN ([male], [female], [Unknown])
    ) AS pvt

I get this

s
(source: realestateagenthealthinsurance.com)

But what I need is this, with two cross tabs on top and a option for multiple columns.

s
(source: realestateagenthealthinsurance.com)

Is this possible with a pivot or any other solution?
Thanks 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-05-30T04:03:10+00:00Added an answer on May 30, 2026 at 4:03 am
    WITH T AS (
        SELECT A.city,
               A.sex, 
               CASE
                    WHEN A.age BETWEEN 20 AND 30 THEN '20-30_' + race
                    WHEN A.age BETWEEN 31 AND 40 THEN '31-40_' + race
                    WHEN A.age BETWEEN 41 AND 50 THEN '41-50_' + race
               END AS age_range_race
        FROM @test2 AS A
    )
    SELECT  *
    FROM T
    PIVOT( COUNT(age_range_race) FOR age_range_race
           IN(
              [20-30_African-American], 
              [20-30_Asian], 
              [20-30_Caucasian], 
              [20-30_Hispanic],
              [31-40_African-American], 
              [31-40_Asian], 
              [31-40_Caucasian], 
              [31-40_Hispanic],
              [41-50_African-American], 
              [41-50_Asian], 
              [41-50_Caucasian], 
              [41-50_Hispanic]
              )
    ) AS P
    ORDER BY city, sex
    
    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

I'm trying to build a table layout similar to this --------------------------------------------------------------------- | 01.01.2010 01:00
I have two tables in the database(videos and viewData) . Im trying to build
I'm trying to build a navigation system using categories table with hierarchies. Normally, the
I am trying to build an Oracle stored procedure which will accept a table
I'm trying to build a prototype application that consists of a table of data
I'm trying to build some jquery tabs based on the request (which is stored
I have been trying to get this to work for about three solid days
I'm trying to build a query with hibernate criteria for the following scenario: Two
I am trying to build a 2008 SSRS report so that it displays table
I have a table (id=docsTable) whose rows look similar to this: <tr bgcolor=#E7DDCC> <td

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.