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

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 13, 20262026-06-13T05:39:47+00:00 2026-06-13T05:39:47+00:00

I have a query that produces the following results: table1: degree_code occupation_code degree_completions degree_level

  • 0

I have a query that produces the following results:

table1:

degree_code  occupation_code  degree_completions  degree_level
1.0000       20-2021          10                  1
1.0000       20-2022          10                  1
1.1051       52-2095          2                   3
1.1051       52-2095          41                  2
1.5010       15-1100          2                   3

I have another small, lookup table that relates degree_level to custom categories that I need to use:

table2

degree_level  degree_level_recode
1             AADEGREE
2             AADEGREE
3             BACHDEGREE

I would like to build the output of of the first query to report the following format:

degree_code  occupation_code  degree_completions  degree_level AADEGREE BACHDEGREE
1.0000       20-2021          10                  1            10       0
1.0000       20-2022          10                  1            10       0
1.1051       52-2095          2                   3            3        0
1.1051       52-2095          41                  2            0        41
1.5010       15-1100          2                   3            2        1

Basically, create new temporary recode fields in the original query that report the degree_completions underneath them when they match the degree_level_recode, input 0 if no match. This is highly simplified; I will be performing operations on the recode fields against other fields in each element in the query.

I’ve shown the degree_completions field for reference, but I’d leave it out in the final query for obvious redundancy reasons.

I’m thinking I need to use a CASE statement or something similar for comparison checking, but I’m new to SQL.

EDIT:

In reference to Cha’s answer below, take a revised table1 output (after mapping the recode):

degree_code  degree_level  degree_completions  degree_level_recode
01.0601      2             11                  LESSCOLL
01.0601      3             22                  AADEGR
01.0605      2             3                   LESSCOLL

Consider this code (table2 is referenced above the edit):

SELECT degree_code
    ,degree_level
    ,[LESSCOL] AS LESSCOL
    ,[AADEGR] AS AADEGR
    ,[BACHDEGR] AS BACHDEGR
    ,[MADEGR] AS MADEGR
    ,[DOCDEGR] AS DOCDEGR
FROM 
    (
    SELECT degree_code
        ,table1.degree_level
        ,degree_level_recode
        ,degree_code
     FROM table1
          ,table2 
     WHERE table1.degree_level = table2.degree_code
     ) AS p
     PIVOT
         (
         SUM (degree_completions)
         FOR degree_level_recode IN ([LESSCOL], [AADEGR], . . .)
         ) AS pvt

Which produces these results:

degree_code  degree_level  LESSCOL  AADEGR  BACHDEGR  MADEGR  DOCDEG  
01.0601      2             NULL     NULL    NULL      NULL    NULL
01.0601      3             NULL     22      NULL      NULL    NULL
01.0505      2             NULL     NULL    NULL      NULL    NULL

I’m trying to get to:

degree_code  degree_level  LESSCOL  AADEGR  BACHDEGR  MADEGR  DOCDEG  
01.0601      2             11       NULL    NULL      NULL    NULL
01.0601      3             NULL     22      NULL      NULL    NULL
01.0505      2             3        NULL    NULL      NULL    NULL

Additionally, replace NULL with 0.

  • 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-13T05:39:49+00:00Added an answer on June 13, 2026 at 5:39 am

    This is what you are after (assumptions: your first table is called #temp1, your second table is called #temp2):

    SELECT *
    FROM
    #temp1,
    (SELECT degree_level, [AADEGREE] as col1, [BACHDEGREE] as col2
    FROM 
    (SELECT degree_completions, #temp1.degree_level, degree_level_recode
    FROM #temp1, #temp2 WHERE #temp1.degree_level = #temp2.degree_level) AS p
    PIVOT
    (
    SUM (degree_completions)
    FOR degree_level_recode IN
    ([AADEGREE], [BACHDEGREE])
    ) AS pvt
    ) as V
    WHERE #temp1.degree_level = V.degree_level
    ORDER BY 1
    
    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

Dear all, I have a select query that currently produces the following results: DoctorName
I have the following LINQ conditional where clause query that produces a result of
I have the following Sql Query that returns the type of results that I
I have the following query that performs a AND on-demand: var products = //
Here is a silly question. Lets say I have a query that produces for
I have a LinqToEntities query that double produces a subquery when creating the SQL.
I have a linq to sql query that produces duplicates. i can seem to
I have a SELECT query that yields multiple results and do not have any
I have the following query. SELECT MIN(col1) FROM table1 WHERE id1 IN (SELECT id2
I have a query in the following format select * from Table1 t1 inner

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.