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

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 26, 20262026-05-26T02:37:03+00:00 2026-05-26T02:37:03+00:00

I have a CTE expression of the following form: WITH nCTE AS ( SELECT

  • 0

I have a CTE expression of the following form:

WITH nCTE AS
(
   SELECT *
   FROM 
   TableA X INNER JOIN TableB Y
   ON X.ID = Y.ID
)
SELECT *
FROM
(
   SELECT ...
   FROM nCTE
)
WHERE ...
GROUP BY ...
ORDER BY ...

The results of this query look something like this:

ID   Name   Badge   Type   Value
1   John    Blue    Pre    10
1   John    Blue    Post   20
1   John    Blue    Cur    30
1   John    Green   Pre    50
1   John    Green   Post   30
1   John    Green   Cur    20

I am trying to get the results in the following form:

ID   Name   Badge   Pre   Cur   Post
1    John   Blue    10    20    30
2    John   Green   50    30    20

As I am using a CTE, I am not sure if I can do this in a direct way or I should self-join the result multiple times to get this (this will just make everything look long, ugly and complex). Is there a good way to achieve this?

UPDATE: Trying this returns a NULL value

WITH nCTE AS
(
   SELECT *
   FROM 
   TableA X INNER JOIN TableB Y
   ON X.ID = Y.ID
)
SELECT ID, Name, Badge, [0] AS 'Pre', [1] AS 'Cur', [2] AS 'Post'
FROM
(
    SELECT *
    FROM
    (
       SELECT ...
       FROM nCTE
    )
    WHERE ...
    GROUP BY ...
) Y
PIVOT
(
   MAX(Value)
   FOR Type IN ([0], [1], [2])
) AS PivotTable
ORDER BY ...

While the query itself works, it returns the following:

ID   Name   Badge   Pre     Cur     Post
1    John   Blue    NULL    NULL    NULL
2    John   Green   NULL    NULL    NULL

Any suggestions as to why this is happening?

  • 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-26T02:37:03+00:00Added an answer on May 26, 2026 at 2:37 am

    My bad! Pivoting required the actual values. I was blindly following the example from MSDN. In any case, the following works:

    WITH nCTE AS
    (
       SELECT *
       FROM 
       TableA X INNER JOIN TableB Y
       ON X.ID = Y.ID
    )
    SELECT ID, Name, Badge, [Pre] AS 'Pre', [Cur] AS 'Cur', [Post] AS 'Post'
    FROM
    (
        SELECT *
        FROM
        (
           SELECT ...
           FROM nCTE
        )
        WHERE ...
        GROUP BY ...
    ) Y
    PIVOT
    (
       MAX(Value)
       FOR Type IN ([Pre], [Cur], [Post])
    ) AS PivotTable
    ORDER BY ...
    
    • 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: FROM CTE_Order cte INNER JOIN tblOrders o ON cte.OrderId
I have the following CTE SQL WITH Tasks AS ( SELECT TaskID, ParentTaskID, CAST(SortKey
I have the following query: WITH cte AS ( SELECT windowId, frameIndx, elemIndx, comment,
I have the following CTE Query ;WITH cte AS ( SELECT 0 AS lvl,
I have the following cte: WITH cte AS ( SELECT c.ParentIDNo, c.Category, c.ChildCategory, c.WSWebProductNameIDNo,
I have these Queries: With CTE(comno) as (select distinct comno=ErpEnterpriseId from company) select id=Row_number()
I have written a very simple CTE expression that retrieves a list of all
Say that have the following CTE that returns the level of some tree data
I have a query that looks like this WITH CTE AS ( SELECT ROW_NUMBER()
I have a CTE as below (logic removed) ;with cte_a as ( select ID,

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.