Home > OS >  how to supply default counts when grouping on strings that have not occurred yet?
how to supply default counts when grouping on strings that have not occurred yet?

Time:08-26

Suppose I have a Result table that did a grouping on Section and Status in a Source table and produced this:

    Section        Status         Count


    C              Not Started        5
    D              Success            5
    D              Success            5
    M              Success            4
    M              Not Started       19

But in reality the Status can take on one of 4 values in the future, 'Not Started', 'Success', 'Failed' and 'In Progress'. What is the simplest way to represent all the possible values for all the sections, and place a 0 as the Count when they were not yet in the Source table? The sections will not grow in the future, they are fixed as 'C', 'D', or 'M'.

CodePudding user response:

  1. If you have tables that already describe the possible statuses and the possible sections, then you add those two tables to your query and Left Outer Join both of them to the rest of your existing query.
  2. If you do not have those tables in your data base then you'll have to create them inside your query. Now the exact syntax for that varies from DB to DB, so you need to tag this question with the DBMS you are using before we go any further if this is the case.
  •  Tags:  
  • sql
  • Related