Question

Here's the edited code: This code is more close to the actual code I am running. It gives me all records except for Cancelled records. I want to include cancelled records with NOT NULL fields in Prod columns.

Select  AppID,
    Prod1, 
Prod2, 
Prod3, 
Prod4, 
Prod5, 
Prod6, 
Prod7, 
Prod8,
CASE WHEN len(Prod1) > 0 OR
   len(Prod2) > 0 OR
       len(Prod3) > 0 OR
       len(Prod4) > 0 OR
       len(Prod5) > 0 OR
       len(Prod6) > 0 OR
       len(Prod7) > 0 OR
       len(Prod8) > 0 THEN 1 ELSE 0 END AS NbrIncomplete
WHERE      (CASE WHEN LEFT(ProductShortName, 2) IN ('CT', 'GR') THEN 1 ELSE 0 END = 0) AND  
       (CTRACKS.Ctrack IS NULL) AND (AppInitiatedDate >= @StartDate) AND (AppInitiatedDate < @EndDate) AND 
       (Status <> N'Cancelled' OR
    Status IS NULL) 
OR
       (CASE WHEN LEFT(ProductShortName, 2) IN ('CT', 'GR') THEN 1 ELSE 0 END = 0) AND 
       (CTRACKS.Ctrack IS NULL) AND (Status <> N'Cancelled' OR
        Status IS NULL) AND 
   (CASE WHEN 
         len(Prod1) > 0 OR
             len(Prod1) > 0 OR
                     len(Prod1) > 0 OR
                     len(Prod1) > 0 OR
                     len(Prod1) > 0 OR
                     len(Prod1) > 0 OR
                     len(Prod1) > 0 OR
                     len(Prod1) > 0 
                     THEN 1 ELSE 0 END = 1)
ORDER BY AppID
Was it helpful?

Solution 5

Finally, I was able to run the code successfully and got the desired output. Yeyy!!!

Select  AppID, 
Prod1, 
Prod2, 
Prod3, 
Prod4, 
Prod5, 
Prod6, 
Prod7, 
Prod8,
CASE WHEN len(Prod1) > 0 OR
   len(Prod2) > 0 OR
   len(Prod3) > 0 OR
   len(Prod4) > 0 OR
   len(Prod5) > 0 OR
   len(Prod6) > 0 OR
   len(Prod7) > 0 OR
   len(Prod8) > 0 THEN 1 ELSE 0 END AS NbrIncomplete
WHERE      
   (CASE WHEN LEFT(ProductShortName, 2) IN ('CT', 'GR') THEN 1 ELSE 0 END = 0) AND  
   (CTRACKS.Ctrack IS NULL) AND ((Status = 'Cancelled' AND 
   Prod1 IS NOT NULL OR
   Prod1 IS NOT NULL OR
   Prod1 IS NOT NULL OR
   Prod1 IS NOT NULL OR
   Prod1 IS NOT NULL OR
   Prod1 IS NOT NULL OR
   Prod1 IS NOT NULL OR
   Prod1 IS NOT NULL)) OR Status <> 'Cancelled')
   AND 
   (AppInitiatedDate >= @StartDate) AND (AppInitiatedDate < @EndDate)  
   OR
   (CASE WHEN LEFT(ProductShortName, 2) IN ('CT', 'GR') THEN 1 ELSE 0 END = 0) AND 
   (CTRACKS.Ctrack IS NULL) AND 
   (CASE WHEN 
     len(Prod1) > 0 OR
         len(Prod1) > 0 OR
                 len(Prod1) > 0 OR
                 len(Prod1) > 0 OR
                 len(Prod1) > 0 OR
                 len(Prod1) > 0 OR
                 len(Prod1) > 0 OR
                 len(Prod1) > 0 
                 THEN 1 ELSE 0 END = 1)
  ORDER BY AppID

Thank you for your efforts. Appreciate it.

OTHER TIPS

Based on the sample of your desired output, you want this:

SELECT AppID, Status, Prod1,Prod2,Prod3,Prod4,Prod5,Prod6,Prod7,Prod8
FROM   Status
WHERE  Coalesce(Prod1, Prod2, Prod3, Prod4, Prod5, Prod6, Prod7, Prod8) IS NOT NULL
   OR  Status <> 'Cancelled' 

That assumes a reasonable DB schema, such that the values of the Prod1,etc columns are NULL, and not empty strings. If they are empty strings, you should fix the schema.


Having just seen your comment that the Prod[n] types are, indeed, nvarchar, I will re-iterate: you should fix the db schema. What you have is broken. In addition to making this query more complicated than it needs to be, it will also be near-impossible to accurately compare dates to find things like, for example, which prod[n] for a specific row came first (I also recommend pulling those product fields out into a separate table, with the key for this table plus product number and date as columns).

But in the near term, and not knowing yet whether the empty values in the report are NULL or just empty strings, you can do this:

SELECT    AppID, Status, Prod1,Prod2,Prod3,Prod4,Prod5,Prod6,Prod7,Prod8
FROM      Status
WHERE     Status <> 'Cancelled' OR 
           LEN(coalesce(Prod1,''))>0 or
           LEN(coalesce(Prod2,''))>0 or
           LEN(coalesce(Prod3,''))>0 or
           LEN(coalesce(Prod4,''))>0 or
           LEN(coalesce(Prod5,''))>0 or
           LEN(coalesce(Prod6,''))>0 or
           LEN(coalesce(Prod7,''))>0 or
           LEN(coalesce(Prod8,''))>0  
ORDER BY   AppID

I provide that because you likely have someone who needs this data now, but again: your very next task should be to fix the schema to use date types.

That where clause isn't going to work like you want it to. You need something like this:

where
(status = 'CANCELLED' and (PROD1 is not null or prod2 is not null<etc>))
or
status <> 'CANCELLED'

Here's a simplified example: SQL Fiddle

IS NOT NULL instead of >0
So: WHERE Status <> 'Cancelled' AND (Prod1 IS NOT NULL or Prod2 IS NOT NULL or Prod3 IS NOT NULL or Prod4 IS NOT NULL or Prod5 IS NOT NULL or Prod6 IS NOT NULL or Prod7 IS NOT NULL or Prod8 IS NOT NULL)

Select * from status 
    where (Status<>'Cancelled' and Prod1 is not null and...and Prod8 is not null) or Status='Cancelled'
Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow
scroll top