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

  • Home
  • SEARCH
  • 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 6850761
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 27, 20262026-05-27T01:09:53+00:00 2026-05-27T01:09:53+00:00

Is there a way using Pivot to include rows for which no records exist

  • 0

Is there a way using Pivot to include rows for which no records exist and show 0s or nulls for the results?

I want the reults of the query to look something like this :-

     A    B   C    D
5   12   81   107  0
4   0     0    0   0 
3   1    12   12   5 
2   3    0     0   0 
1   0    0     0   0

However, currently Pivot is not returning empty rows and my results look like this :-

     A    B   C    D
5   12   81   107  0
3   1    12   12   5 
2   3    0     0   0 

Any way that this can be achieved with by doing a sort of ‘left outer join’ on the Pivot to show all rows?

  • 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-27T01:09:54+00:00Added an answer on May 27, 2026 at 1:09 am

    You really need to manufacture the missing records using a cross product and a left join in order to have them appear

    declare @table table
    (
          n     int not null,
          ch    char(1) not null,
          cnt   int not null,     
    
          primary key (n, ch)
    )
    
    insert into @table values (5, 'A', 12)
    insert into @table values (5, 'B', 81)
    insert into @table values (5, 'C', 107)
    insert into @table values (3, 'A', 1)
    insert into @table values (3, 'B', 12)
    insert into @table values (3, 'C', 12)
    insert into @table values (3, 'D', 5)
    insert into @table values (2, 'A', 3)
    
    declare @numbers table (n int not null primary key)
    insert into @numbers values (1)
    insert into @numbers values (2)
    insert into @numbers values (3)
    insert into @numbers values (4)
    insert into @numbers values (5)
    
    declare @chars table (ch char(1) not null primary key)
    insert into @chars values ('A')
    insert into @chars values ('B')
    insert into @chars values ('C')
    insert into @chars values ('D')
    
    
    select n, [A], [B], [C], [D]
      from 
      ( -- manufacture missing records 
        select n.n, ch.ch, coalesce(t.cnt, 0) as cnt
          from @numbers n
            cross join @chars ch 
            left join @table t on (n.n = t.n and ch.ch = t.ch)
      ) as t
    pivot
    (
      sum(cnt)
      for ch in ([A], [B], [C], [D])
    ) as pivotTable
    order by n desc
    
    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

Is there any way to create a pivot and/or cross-tab query using for SQLite
Is there any way using JQuery to Show the contents of the Title tag
Is there a way I can create Excel Pivot tables using ASP .NET 3.5
Is there a way using which I can request Android OS to reboot after
Is there a way using NSRegularExpression to specify that you want to do a
Is there a way using the iPhone SDK to get WiFi information? Things like
Is there a way using Java to over-ride the browser authentication dialog box when
Is there a way using Flash (CS3+AS3) to determine if the published swf is
Is there a way using SQL to list all foreign keys for a given
Is there a way using SWT to get a list of all processes currently

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.