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

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 13, 20262026-06-13T04:17:53+00:00 2026-06-13T04:17:53+00:00

this is my first attempt at dynamic pivoting in SQL and I have become

  • 0

this is my first attempt at dynamic pivoting in SQL and I have become a bit stuck!

I have two tables that I am joining on, one holds stock items and the other holds different prices for stock items for different websites(sources). As there is the possibility of having different sites, the query needs to be dynamic.

The problem is that I cannot remove nulls from the results and I was hoping that someone could help.

results look like this currently:

ID    site-site1   site-site2    site-site3
1      null          1.99          2.99
2      12.99         null          10.00
3      1.50          null          2.00

The query is as follows:

DECLARE @sources nvarchar(max)

SELECT @sources = 
STUFF(( SELECT DISTINCT ',[' + 
CASE 
WHEN ip.[Source] is null or ip.[Source] = '' THEN 'Default'
ELSE ip.[Source] 
END 
+ ' - ' +  
CASE 
WHEN ip.secondSource is null or ip.secondSource = '' THEN  'Default'
ELSE ip.secondSource 
END  
+ ']'
    FROM itemprice ip
    for XML PATH('')
  ), 1, 1, '')

DECLARE @SQL nvarchar(MAX)
SELECT @SQL = N'
SELECT *
FROM 
(
SELECT 
s.id, 
[Source] = CASE 
               WHEN ip.[Source] is null or ip.[Source] = '''' THEN  ''Default'' 
       ELSE ip.[Source] 
       END + '' - '' +  
CASE WHEN ip.secondSource is null or ip.secondSource = '''' THEN ''Default''
ELSE ip.secondSource 
END, 
    SalePrice = isnull(ip.SalePrice, 0)  
    FROM stock s 
    LEFT OUTER JOIN itemprice ip on ip.id = s.id
) data 
PIVOT 
( 
  MAX(SalePrice) for [Source] IN (' + @sources + ')
) as PivotTable
ORDER BY PivotTable.id'
exec sp_executesql @sql

Any help would be greatly appreciated, it appears as though it is not as simple as wrapping each @sources item in an ISNULL.

Thanks

  • 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-13T04:17:54+00:00Added an answer on June 13, 2026 at 4:17 am

    Unfortunately, you have to ISNULL each of the individual pivoted columns. Either that or CROSS JOIN your data subquery to generate the full matrix and perform ISNULL there.

    e.g. to replace NULLs with 0’s using ISNULL on pivoted columns:

    DECLARE @sources nvarchar(max), @Selectlist nvarchar(max)
    
    SELECT @sources = 
    STUFF(( SELECT DISTINCT ',[' + 
    CASE 
    WHEN ip.[Source] is null or ip.[Source] = '' THEN 'Default'
    ELSE ip.[Source] 
    END 
    + ' - ' +  
    CASE 
    WHEN ip.secondSource is null or ip.secondSource = '' THEN  'Default'
    ELSE ip.secondSource 
    END  
    + ']'
        FROM itemprice ip
        for XML PATH('')
      ), 1, 1, '')
    
    SELECT @Selectlist = 
    STUFF(( SELECT DISTINCT ',ISNULL([' + 
    CASE 
    WHEN ip.[Source] is null or ip.[Source] = '' THEN 'Default'
    ELSE ip.[Source] 
    END 
    + ' - ' +  
    CASE 
    WHEN ip.secondSource is null or ip.secondSource = '' THEN  'Default'
    ELSE ip.secondSource 
    END  
    + '],0) [' + 
    CASE 
    WHEN ip.[Source] is null or ip.[Source] = '' THEN 'Default'
    ELSE ip.[Source] 
    END 
    + ' - ' +  
    CASE 
    WHEN ip.secondSource is null or ip.secondSource = '' THEN  'Default'
    ELSE ip.secondSource 
    END  
    + ']'
        FROM itemprice ip
        for XML PATH('')
      ), 1, 1, '')
    
    DECLARE @SQL nvarchar(MAX)
    SELECT @SQL = N'
    SELECT ID, ' + @Selectlist + '
    FROM 
    (
    SELECT 
    s.id, 
    [Source] = CASE 
                   WHEN ip.[Source] is null or ip.[Source] = '''' THEN  ''Default'' 
           ELSE ip.[Source] 
           END + '' - '' +  
    CASE WHEN ip.secondSource is null or ip.secondSource = '''' THEN ''Default''
    ELSE ip.secondSource 
    END, 
        SalePrice = isnull(ip.SalePrice, 0)  
        FROM stock s 
        LEFT OUTER JOIN itemprice ip on ip.id = s.id
    ) data 
    PIVOT 
    ( 
      MAX(SalePrice) for [Source] IN (' + @sources + ')
    ) as PivotTable
    ORDER BY PivotTable.id'
    exec sp_executesql @sql
    
    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

Because this is my first attempt at an extension method that seems quite useful
Up front: This is my first attempt at an Android app. I'm in that
This is my first attempt to use boost::threads and I have a silly question.
This is my first attempt at responsive design, so I'm keeping it simple. I
This is my first attempt to write shorthand if statements however am befuddled by
this if my first attempt at using streaming for WCF, and I am struggling
This is my first attempt to create a GUI in MATLAB. I haven't been
this is my first attempt at a responsive design so excuse me if this
This is pretty strange (admitedly, this is my first attempt with python / sqlite),
I'm trying to create a simple threading procedure (granted this is my first attempt

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.