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

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 20, 20262026-05-20T17:27:19+00:00 2026-05-20T17:27:19+00:00

Assuming I have an SQL table with this schema: CREATE TABLE( foo INTEGER, bar

  • 0

Assuming I have an SQL table with this schema:

CREATE TABLE(
foo INTEGER,
bar INTEGER,
baz INTEGER DEFAULT 0
);

which contains data

foo|bar|baz
-----------
1  |1  |2
2  |3  |4

and I am interested in values 1,2 for foo and 1,2,3 for bar. Is there an SQL request which would return “missing” values along with the existing ones:

foo|bar|baz
-----------
1  |1  |2
1  |2  |0
1  |3  |0
2  |1  |0
2  |2  |0
2  |3  |4

? I suspect there isn’t, but perhaps I just don’t know it?

UPDATE:
1) using SQLite 3;
2) the missing values are given by the DEFAULT clause;
3) the second table shows the expected result.

  • 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-20T17:27:20+00:00Added an answer on May 20, 2026 at 5:27 pm

    Assuming you named your table Data, following would get you the results you’ve posted.

    • the WITH statement creates a temporary in memory table containing all Bar’s from 1 to the Maximum present in your actual table.
    • the CROSS APPLY returns a row for every bar, existing in your table or not.
    • the CASE statement selects an existing baz if present, 0 if not.

    SQL Statement

    WITH q AS (
      SELECT  [bar] = 1
              , [MaxBar] = MAX(bar)
      FROM    Data
      UNION ALL 
      SELECT  q.bar + 1
              , q.MaxBar
      FROM    q
      WHERE   q.bar + 1 <= q.MaxBar
    )  
    SELECT  Data.foo
            , q.bar
            , CASE WHEN q.bar = Data.bar THEN Data.baz ELSE 0 END
    FROM    q
            CROSS APPLY Data
    ORDER BY
            Data.foo
            , q.bar
    

    Test script

    WITH Data AS (
      SELECT [foo] = 1, [bar] = 1, [baz] = 2
      UNION ALL SELECT 2, 3, 4
    ), q AS (
      SELECT  [bar] = MIN(bar)
              , [MaxBar] = MAX(bar)
      FROM    Data
      UNION ALL 
      SELECT  q.bar + 1
              , q.MaxBar
      FROM    q
      WHERE   q.bar + 1 <= q.MaxBar
    )  
    SELECT  Data.foo
            , q.bar
            , CASE WHEN q.bar = Data.bar THEN Data.baz ELSE 0 END
    FROM    q
            CROSS APPLY Data
    ORDER BY
            Data.foo
            , q.bar
    
    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

Assuming I have a table like this one: CREATE TABLE user_delegates ( [id] INT
In SQL Server assuming one has columns that need to have the same data
Assuming I have fonts installed which have the appropriate glyphs in them, is there
Hej, assuming I have a code that looks like this: List<User> userList = GetUserByName
Assuming Visual C/C++ 6, I have a complex data structure of 22399 elements that
In SQL Server 2008 I have a table called Zone with a column ZoneReference
This is on SQL Server 2008. I have several columns I want to convert
Assuming that a table contains sufficient information to warrant an index seek, at what
Assuming I have only the class name of a generic as a string in
Assuming I have an open source web server or proxy I can enhance, let's

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.