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

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 11, 20262026-06-11T01:05:45+00:00 2026-06-11T01:05:45+00:00

I have read-only access to these tables, and need to build a query that

  • 0

I have read-only access to these tables, and need to build a query that will list the count of part number records based on the count of image records per part number. Essentially, I need the results to be formatted like this:

vendor_desc|Count of parts with 1 image|Count of parts with 2 images|…

VendorXYZ|250|307

I made this union query that lists the results in one column, but need the columns to be separated by count of images. Here is my union query:

SELECT vnd.vendor_desc,
       Count(img1.[part#]) part
FROM   [mytable].[dbo].[parts_images] AS img1
       INNER JOIN mytable.dbo.vendors AS vnd
               ON vnd.vendor_id = LEFT(img1.part#, 3)
       INNER JOIN (SELECT img2.part#,
                          Count(img2.image_file)images
                   FROM   [mytable].[dbo].[parts_images] AS img2
                   GROUP  BY img2.part#
                   HAVING ( Count(img2.image_file) = 1 )) AS img2
               ON img1.part# = img2.part#
GROUP  BY vendor_desc

UNION

SELECT vnd.vendor_desc,
       Count(img1.[part#]) part
FROM   [mytable].[dbo].[parts_images] AS img1
       INNER JOIN mytable.dbo.vendors AS vnd
               ON vnd.vendor_id = LEFT(img1.part#, 3)
       INNER JOIN (SELECT img2.part#,
                          Count(img2.image_file)images
                   FROM   [mytable].[dbo].[parts_images] AS img2
                   GROUP  BY img2.part#
                   HAVING ( Count(img2.image_file) = 2 )) AS img2
               ON img1.part# = img2.part#
GROUP  BY vendor_desc
ORDER  BY vendor_desc

Thanks in advance!

Here is the query for the result that I was looking for:

SELECT vnd.vendor_desc,
       Count(img2.[part#]) part
       ,COUNT(img3.part#)
FROM   mytable.[dbo].[parts_images] AS img1
       INNER JOIN mytable.dbo.vendors AS vnd
               ON vnd.vendor_id = LEFT(img1.part#, 3)
       left JOIN (SELECT img2.part#,
                          Count(img2.image_file)images
                   FROM   mytable.[dbo].[parts_images] AS img2
                   GROUP  BY img2.part#
                   HAVING ( Count(img2.image_file) = 1 )) AS img2
               ON img1.part# = img2.part#
               left JOIN (SELECT img3.part#,
                          Count(img3.image_file)images
                   FROM   mytable.[dbo].[parts_images] AS img3
                   GROUP  BY img3.part#
                   HAVING ( Count(img3.image_file) = 2 )) AS img3
               ON img1.part# = img3.part#
GROUP  BY vendor_desc
order by vendor_desc

Thanks for your help, podiluska!

  • 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-11T01:05:46+00:00Added an answer on June 11, 2026 at 1:05 am

    So

    SELECT *
    FROM (yourunionquery) src
    PIVOT
    (COUNT(vendor_desc) FOR part IN ([1],[2],[3], ... )) p
    

    If you need a variable number of columns, you’ll have to construct this query using dynamic SQL to populate the result columns

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

Sidebar

Related Questions

I have read-only access to a database that was set up for a third-party,
I want a group of users to have read-only access to all tables and
I'm creating a service that has read-only access to the database. I have a
Now that I have a read-only application working, I am working on the insert
I have read that TempData is backed by session in Asp.Net MVC, but only
On mysql and using only myisam tables, I need to access the contents of
Many library classes in AS3 have read only properties. Is it possible to create
I have a read only combo-box with 5 values in, when the user selects
I have a web page with a read-only text box which shows some HTML
I want to create a read-only user in PostgreSQL. The intention is to have

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.