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

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 13, 20262026-06-13T23:42:48+00:00 2026-06-13T23:42:48+00:00

Suppose I have a query that returns result like this: Project Year Type Amt

  • 0

Suppose I have a query that returns result like this:

Project Year Type Amt
PJ00001 2012 1    1000
PJ00001 2012 2    1000
PJ00001 2011 1    1000
PJ00002 2012 1    1000

What I want: Every Project will have 2 rows of Types for each Year. If the row is not there, add it to the result with Amt = 0.
For example:
– PJ00001 have 2 rows of type 1,2 in 2012 –> OK. But in 2011, it only have 1 row of Type 1 –> We add one row:PJ00001 2011 2 0
– PJ00002 have only 1 row of type 1 –> add:PJ00002 2012 2 0

Is there a way to easily do it. The only way I know now is to create a view like: PJ_VIEW. And then:

SELECT *
FROM PJ_VIEW
UNION ALL
SELECT t.PROJECT, t.YEAR_NO, 1 AS TYPE_NO, 0 AS AMT
FROM PJ_VIEW t
WHERE NOT EXISTS (SELECT 1 FROM PJ_VIEW t2 WHERE t2.PROJECT = t.PROJECT AND t2.YEAR_NO = t.YEAR_NO AND t2.TYPE_NO = 1)
UNION ALL
SELECT t.PROJECT, t.YEAR_NO, 2 AS TYPE_NO, 0 AS AMT
FROM PJ_VIEW t
WHERE NOT EXISTS (SELECT 1 FROM PJ_VIEW t2 WHERE t2.PROJECT = t.PROJECT AND t2.YEAR_NO = t.YEAR_NO AND t2.TYPE_NO = 2)
  • 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-13T23:42:50+00:00Added an answer on June 13, 2026 at 11:42 pm

    Try something like this using DB2 temp tables. Just replace the content of the table named query with your query.

    WITH TypeList AS ( 
        SELECT 1 TypeCode
        UNION ALL
        SELECT 2
    ),
    query AS (
        SELECT Project, [Year], [Type], Amt FROM yourQuerySource
    ),
    DistinctProjectYears AS (
        SELECT Project, [YEAR]
        FROM query
        GROUP BY Project, [YEAR]
    ),
    Projects AS (
        SELECT Project, [YEAR], [Type] as TypeCode, MAX(Amt) as Amt
        FROM query
        GROUP BY Project, [Year], [Type]
    )
    SELECT l.TypeCode, py.Project, py.[Year], COALESCE(t.Amt, 0) as Amt
    FROM TypeList l CROSS JOIN DistinctProjectYears py
        LEFT JOIN Projects t ON l.TypeCode = t.TypeCode
            AND py.Project = t.Project
            AND py.[Year] = t.[Year]
    
    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

Suppose I have a SQL query that looks like this: SELECT fName from employees
I have a Linq2Sql query that looks like this: var data = from d
Suppose I have a query like this: SELECT s.staffID as staffID, CONCAT_WS(, , lname,
Suppose I have a SQL Query that isn't really nice. The SQL Query has
Lets suppose that I have the following simple query var q = from p
Suppose I have a pure virtual method in the base interface that returns to
Suppose I have a static method of my class that returns an object of
I want to write a query like this: For a table that has these
Suppose I have an IQueryable<T> expression that I'd like to encapsulate the definition of,
I have a query that returns list of results. I want a single random

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.