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

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 14, 20262026-06-14T10:14:22+00:00 2026-06-14T10:14:22+00:00

I have a table that looks like this, lets call this table B .

  • 0

I have a table that looks like this, lets call this table B.

id    boardid    schoolid     subject     cnt1   cnt2  cnt3 ....
=================================================================
1       20         21           f     
2       20         21           r
3       20         21           w
4       20         21           m
5       20         30           r
6       20         30           w
7       20         30           m

Suppose the counts are just integers. Notice that there is no subject = f for schoolid = 30. Similarly, for most schools, some subject dosnt exist. You might have a schoolid that has just r, w or some that are just r, m, f..

So what I want to do is have 4 consistent rows for each school, and the row that dosnt exist I want dummy values. I thought about creating a secondary table

drop table #A
Select * into #A FROM 
(
select [subject_s] = 'r', orderNo = 1
union all
select [subject_s] = 'w', orderNo = 2
union all
select [subject_s] = 'm', orderNo = 3
union all
select [subject_s] = 'f', orderNo = 4
) z

and doing some joins on them, but I’ve gotten NO where. I’ve tried inner join, left outer, cross join, everything. I’ve even tried to make cartesian product. I think my cartesian product messes up because I have orderno in there so it makes 16 rows per row in the main table. Actually typing this out, I realize if I remove the orderno, apply the cartesian product and then add orderno in later, it might work but I am interested to see what you guys can come up with. I am stumped.

End result

id    boardid    schoolid     subject     cnt1   cnt2  cnt3 ....
=================================================================
1       20         21           r     
2       20         21           w
3       20         21           m
4       20         21           f
5       20         30           r
6       20         30           w
7       20         30           m
7       20         30           f
  • 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-14T10:14:23+00:00Added an answer on June 14, 2026 at 10:14 am

    Try the following:

    SELECT S.boardid, S.schoolid, A.[subject], B.cnt1, B.cnt2, B.cnt3
    FROM (SELECT DISTINCT boardid, schoolid FROM YourTable) S
    CROSS JOIN #A A
    LEFT JOIN YourTable B
        ON B.boardid = S.boardid AND  B.schoolid = S.schoolid 
        AND A.[subject] = B.[subject]
    
    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

So lets say I have a table that looks something like this: ItemName ProductType
Suppose I have a simple MySQL table that looks like this: CREATE TABLE `my_table`
I have a table, let's call it 'entries' that looks like this (simplified): id
I have a table (let's call it tableA) that looks something like this: id
I have a table that looks like this: id value AGA 0.211 AGA 0.433
I have a table that looks like this: CREATE TABLE foobar ( id SERIAL
I have a table that looks like this: ID (pk,int) Col1 (nvarchar) Col2 (nvarchar)
I have a table that looks like this Id FirstName 5 Adam 6 Bob
I have a table that looks like this: table name: uno str_id | title
I have a table that looks like this Group Recipe Priority 0 A 400

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.