Search code examples
sqldb2-luw

What is "Select -1", and how is it different from "Select 1"?


I have the following query that is part of a common table expression. I don't understand the function of the "Select -1" statement. It is obviously different than the "Select 1" that is used in "EXISTS" statements. Any ideas?

  select days_old, 
         count(express_cd),
         count(*),  
         case 
           when round(count(express_cd)*100.0/count(*),2) < 1 then '0'      
           else '' 
         end ||
           cast(decimal(round(count(express_cd)*100.0/count(*),2),5,2) as varchar(7)) || 
           '%'   
  from foo.bar   
  group by days_old   
  union all  
  select -1, -- Selecting the -1 here
         count(express_cd),
         count(*),   
         case 
           when round(count(express_cd)*100.0/count(*),2) < 1 then '0' 
           else ''
         end ||
           cast(decimal(round(count(express_cd)*100.0/count(*),2),5,2) as varchar(7)) || 
           '%'  
  from foo.bar   
  where days_old between 1 and 7

Solution

  • Based on your query, all the records with days_old between 1 and 7 will be output as '-1', that is what select -1 does, nothing special here and there is no difference between select -1 and select 1 in exists, both will output the records as either 1 or -1, they are doing the same thing to check whether if there has any data.

    Back to your query, I noticed that you have a union all and compare each four columns you select connected by union all, I am guessing your task is to get a final result with days_old not between 1 and 7 and combine the result with day_old, which is one because you take all between 1 and 7.