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

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 18, 20262026-06-18T04:12:32+00:00 2026-06-18T04:12:32+00:00

The following code takes a table containing vehicle names, and a table containing region

  • 0

The following code takes a table containing vehicle names, and a table containing region names and combines them into a results table.
I then perform a COUNT on the results table to show how many vehicles there are per region.

I need to amend the code to force ALL regions and ALL vehicles to be counted up. Currently the COUNT statement does not show any regions that contain no vehicles (I have manually deleted some entries from the results to show this). Can anyone illustrate a quick way to do this? I am using SQL 2012 and hoped there would be an easy solution…

enter image description here

For the purposes of answering the question please assume that the two original tables are no longer accessible, and we are now only working with @tbl_results. Thank you.

Note: I know I can do this directly from the vehicle and region tables but I need to use a table variable of results for other processes in my real work.

DECLARE @tbl_vehicles TABLE (vehicleID int, vehicleName nvarchar(100))
DECLARE @tbl_regions TABLE (regionID int, regionName nvarchar(100))
DECLARE @tbl_results TABLE (regionID int, regionName nvarchar(100), vehicleID int, vehicleName nvarchar(100))

INSERT INTO @tbl_regions (regionID, regionName) VALUES (1, 'England')
INSERT INTO @tbl_regions (regionID, regionName) VALUES (2, 'United States')
INSERT INTO @tbl_regions (regionID, regionName) VALUES (3, 'Arctic')

INSERT INTO @tbl_vehicles (vehicleID, vehicleName) VALUES (1, 'Planes')
INSERT INTO @tbl_vehicles (vehicleID, vehicleName) VALUES (2, 'Trains')
INSERT INTO @tbl_vehicles (vehicleID, vehicleName) VALUES (3, 'Automobiles')

DECLARE @i INT
SET @i = 0 

WHILE @i < 100
    BEGIN
        INSERT INTO @tbl_results
            (regionID, regionName, vehicleID, vehicleName) 
        SELECT
            r.regionID, r.regionName, v.vehicleID, v.vehicleName 
        FROM
            @tbl_regions r CROSS JOIN
            @tbl_vehicles v 
        WHERE
            (r.regionID = CAST(RAND() * 4 AS INT)) AND
            (v.vehicleID = CAST(RAND() * 4 AS INT)) 
        SET @i = @i + 1
    END

-- remove trains in the arctic for count example
DELETE FROM @tbl_results WHERE (regionID=3) AND (vehicleID=2)

-- this statement needs to include ALL vehicles and ALL regions (even if no vehicles are found at a region)
SELECT 
    regionName,
    vehicleName,
    COUNT(*) as VehicleCount
FROM
    @tbl_results 
GROUP BY
    regionID,
    regionName,
    vehicleID,
    vehicleName
ORDER BY regionName, vehicleName  
  • 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-18T04:12:34+00:00Added an answer on June 18, 2026 at 4:12 am
    SELECT  r.regionName
    ,       v.vehicleName
    ,       COUNT(res.vehicleName) as VehicleCount
    FROM    (
            select  distinct regionName
            from    @tbl_results
            ) r
    CROSS JOIN 
            (
            select  distinct vehicleName
            from    @tbl_results
            ) v
    LEFT OUTER JOIN
            @tbl_results res
    ON      r.regionName = res.regionName
            AND v.vehicleName = res.vehicleName
    GROUP BY
            r.regionName
    ,       v.vehicleName
    ORDER BY
            r.regionName
    ,       v.vehicleName
    
    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

I have the following code which takes List of boolean as a parameter then
I have the following piece of code that takes in some words, stores them
I have an input that takes tag names, with a space between them. Then
Following code takes like 2500 milliseconds on an i7-*3.4 GHz windows-7 64-bit computer to
I have the following code which takes a touch on one button and draws
I have the following code that takes a double value and converts it to
I've seen the following code that takes a screenshot and saves it as jpg,
I would like to optimize the following code in R. This loop takes a
I have the following code: <div><strong>Name: </strong><?php echo anchor('http://'.$link, $row->Name); ?></div> Which takes a
I have the following piece of code from a function that takes the host

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.