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

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 26, 20262026-05-26T14:45:57+00:00 2026-05-26T14:45:57+00:00

I usually can figure out any sql queries I need for my applications, but

  • 0

I usually can figure out any sql queries I need for my applications, but I have recently been stumped by a Cross Tab query I need to create and was wondering if you could help?

I have 3 tables

Category(catID, catTitle) 
Equipment(equipID, make, model, quantity, catID, siteID)
Site(siteID, title)

And I would like to create a Cross Tab query to display a result set like below

Category   Site1   Site2   Site3   Site4   Site5
PC           2       0       10      3      6
Camera       12      4       2       0      8
Printer      3       2       1       1      2

The numbers displayed represent a total of each category item within each site using the quantity field withint the Equipment table. I have never had to do a Cross Tab query before and I am struggling to get this working.

  • 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-26T14:45:57+00:00Added an answer on May 26, 2026 at 2:45 pm

    You should be able to do this with the ‘pivot’ operator. Something like this (though I am sure I muffed some spelling or syntax details…):

    select catTitle, [1] as site1, [2] as site2, [3] as site3, [4] as site4, [5] as site5
      from (select category.catTitle, equipment.quantity, site.title
              from equipment
                inner join site
                  on (equipment.siteid = site.siteid)
                inner join category
                  on (category.catid = equipment.catid)
            ) 
      pivot
      (
      sum (quantity)
        for equipment.siteid in ( [1], [2], [3], [4], [5] )
      ) as pvt
    order by pvt.category;
    

    The problem with this is that you need to know the exact set of site ids you want to include in the query. If you need a more dynamic crosstab (like you can get in Excel), then you need to generate the query text as a string and use sp_executesql to run it. In the generated text, you include as many of the “[1], [2], [3], [4], [5]…” and “[1] as site1, [2] as site2…” things as you need.

    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

I can usually figure out most C code but this one is over my
I've been looking over this post, but still can't figure out how to solve
I'm having a slight problem that I can't figure out, but should be really
I can figure out a couple of hack ways of doing this,...but no really
I have been coding for over 10 years but I have never used any
Can somebody help me to figure out why DataAnnotations will not work with my
When trying to figure out if a string is null or empty, I usually
I'm more of a designer by trade, but I can usually hold my own
I usually do not have difficulty to read JavaScript code but for this one
I've got autocomplete working fine, but I'm trying to figure out way to make

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.