Search code examples
sql-serversql-server-2016ssms-16

What can I use in a SQL query to help me determine why my query is not returning any data results


Can someone assist me in troubleshooting my SQL query to discover why it's not returning any results, only the column aliases?

I've broken it apart, and all sections that obviously group together returns the expected data individually. Thanks for any guidance/assistance in advance. Below is my script:

...

DECLARE @u_cnt INT;
DECLARE @f_yr DATE;
DECLARE @qrt VARCHAR(3);
DECLARE @dnum VARCHAR(5);


SET @u_cnt = 10000;
SET @f_yr = '2002-05-20';
SET @qrt = 'Q2';
SET @dnum = '43234';




SELECT c.GroupLabel AS ORG_Code,
   CONVERT (VARCHAR(7), FORMAT((CONVERT(DATE, c.changedate)), 'MM-yyyy')) AS [MONTH],
   COUNT(DISTINCT CASE s.TestType 
         WHEN 'IR' THEN c.changedate
              ELSE NULL END) AS TEST_DAYS,
   COUNT(DISTINCT c.changedate) AS ALLDAYS,
   COUNT(s.Id) AS total,
   (CASE WHEN (@u_cnt IS NULL) THEN -1 
         ELSE @u_cnt
         END) AS board_cnt,
   FORMAT((COUNT(s.Id) / CASE
                                WHEN (@u_cnt IS NULL) THEN -1 
                                ELSE @u_cnt
                                END), 'P0') AS pct_tested_text,

   CASE WHEN 100 * (COUNT(s.Id) / CASE 
                                   WHEN (@u_cnt IS NULL) THEN -1 
                                   ELSE @u_cnt 
                                   END) >= 15 
               AND (COUNT(DISTINCT CASE s.TestType
                          WHEN 'IR' THEN c.changedate
                          ELSE NULL END)) >= 4  
   THEN 'Yes'
   ELSE 'NO' END 
FROM cforms c 
INNER JOIN spitems sp
ON c.Id = s.FormId
WHERE c.Group = 'HR'
AND c.bFlag IS NULL
AND s.Report IN ('P', 'N')
AND CONVERT(VARCHAR(6), c.changedate, 112) IN
       (SELECT
            CASE 
                WHEN f.Quarter = 'Q1' THEN CONVERT(VARCHAR(4), YEAR(@f_yr) - 1) + f.FyMonthNumber
                WHEN f.Quarter = 'ALL' AND f.FyMonth IN ('OCT', 'NOV', 'DEC') THEN CONVERT(VARCHAR(4), YEAR(@f_yr) - 1, 112) + f.FyM
                ELSE CONVERT(VARCHAR(4), YEAR(@f_yr), 112) + f.FyM 
            END AS FY_MONTH
        FROM fis f
        WHERE f.Quarter = @qrt)
AND c.GroupLabel = 'Hr' + @dnum
GROUP BY c.GroupLabel, FORMAT((CONVERT(DATE, c.changedate)), 'MM-yyyy')
ORDER BY 1, FORMAT((CONVERT(DATE, c.changedate)), 'MM-yyyy');

Solution

  • Everything that could be limiting your data is in this part of your code below. I broke it apart and added comments to why and where they are limited. I think your CONVERT is the culprit.

    --this inner join will limit the rows to only those with matching Id and FormId
    INNER JOIN spitems sp
    ON c.Id = s.FormId
    
    --of the rows already filtered via the JOIN, they are further limited to thous with the Group = 'HR', a NULL bFlag, and Report = to P or N
    WHERE c.Group = 'HR'
    AND c.bFlag IS NULL
    AND s.Report IN ('P', 'N')
    
    
    --The first convert here changed changedate to yyyymmd (notice the day). 
    --In the sub-query, you seem to only be returning yyyymm formatted with a -, 
    --thus this would return ZERO rows. varchar(6) could resolve this, 
    --by dropping the day, but you'd need to add the hyphen or remove it from the `IN` clause
    AND CONVERT(VARCHAR(7), c.changedate, 112) IN
           (SELECT
                CASE 
                    WHEN f.Quarter = 'Q1' THEN CONVERT(VARCHAR(4), YEAR(@f_yr) - 1) + '-' + f.FyMonthNumber
                    WHEN f.Quarter = 'ALL' AND f.FyMonth IN ('OCT', 'NOV', 'DEC') THEN CONVERT(VARCHAR(4), YEAR(@f_yr) - 1, 112) + '-' + f.FyMonthNumber
                    ELSE CONVERT(VARCHAR(4), YEAR(@f_yr), 112) + '-' + f.FyMonthNumber 
                END AS FY_MONTH
            FROM FyQm f
            WHERE f.Quarter = @qrt)
    
    --Lastly, there may be a case sensitivity here Hr vs HR or there just simply aren't any rows that match this predicate
    AND c.GroupLabel = 'Hr' + @dnum
    

    EDIT

    Elaborating on my answer above... you have changed a portion of your where clause. Specifically the portion where you are evaluating c.changedate to a list of values. You have made the change to :

    AND CONVERT(VARCHAR(6), c.changedate, 112) IN ...

    This is a partial fix. It would remove the trailing DAY value you had before, leaving you with YYYYMM. However, in your subquery, you are formatting the list of values as YYYYMM-? where the ? is whatever f.FyMonthNumber is. As you can see, this will never match your original convert statement since it doesn't have a hyphen. The first thing to change would be remove the hyphen from the string concatenation. In your edited post, you have already done that so good job. Next, the issue could be that your + is not being treated as addition instead of concatenation when you are trying to combine it with f.FyMonthNumber. If f.FyMonthNumber is an int then it will add it.

    DECLARE @f_yr DATE;
    SET @f_yr = '2002-05-20';
    
    SELECT CONVERT(VARCHAR(4), YEAR(@f_yr) - 1) + 02
    

    Here you are wanting it to return 200102 but it returns 2003 since it's performing addition. You can cast it as a varchar or char to fix this.

    SELECT CONVERT(VARCHAR(4), YEAR(@f_yr) - 1) + cast('02' as varchar)
    

    Lastly, an issue you may run into is if f.FyMonthNumber is stored as an int, it won't have the leading zero. Thus, for January it would be represented as 1 instead of 01 and this would also return zero rows for any month before October. You can handle this with the right function.

    DECLARE @f_yr DATE;
    SET @f_yr = '2002-05-20';
    
    SELECT CONVERT(VARCHAR(4), YEAR(@f_yr) - 1) + right('0' + cast('1' as varchar(2)),2) --a month as a single digit
    SELECT CONVERT(VARCHAR(4), YEAR(@f_yr) - 1) + right('0' + cast('12' as varchar(2)),2) --a month with double digits
    

    Putting that all together, I would suspect this edit would fix your issue. I would note though, you aren't evaluating any case expressions for Q2, Q3, or Q4 if that would be applicable...

    DECLARE @u_cnt INT;
    DECLARE @f_yr DATE;
    DECLARE @qrt VARCHAR(3);
    DECLARE @dnum VARCHAR(5);
    
    
    SET @u_cnt = 10000;
    SET @f_yr = '2002-05-20';
    SET @qrt = 'Q2';
    SET @dnum = '43234';
    
    
    
    
    SELECT c.GroupLabel AS ORG_Code,
       CONVERT (VARCHAR(7), FORMAT((CONVERT(DATE, c.changedate)), 'MM-yyyy')) AS [MONTH],
       COUNT(DISTINCT CASE s.TestType 
             WHEN 'IR' THEN c.changedate
                  ELSE NULL END) AS TEST_DAYS,
       COUNT(DISTINCT c.changedate) AS ALLDAYS,
       COUNT(s.Id) AS total,
       (CASE WHEN (@u_cnt IS NULL) THEN -1 
             ELSE @u_cnt
             END) AS board_cnt,
       FORMAT((COUNT(s.Id) / CASE
                                    WHEN (@u_cnt IS NULL) THEN -1 
                                    ELSE @u_cnt
                                    END), 'P0') AS pct_tested_text,
    
       CASE WHEN 100 * (COUNT(s.Id) / CASE 
                                       WHEN (@u_cnt IS NULL) THEN -1 
                                       ELSE @u_cnt 
                                       END) >= 15 
                   AND (COUNT(DISTINCT CASE s.TestType
                              WHEN 'IR' THEN c.changedate
                              ELSE NULL END)) >= 4  
       THEN 'Yes'
       ELSE 'NO' END 
    FROM cforms c 
    INNER JOIN spitems sp
    ON c.Id = s.FormId
    WHERE c.Group = 'HR'
    AND c.bFlag IS NULL
    AND s.Report IN ('P', 'N')
    AND CONVERT(VARCHAR(6), c.changedate, 112) IN
           (SELECT
                CASE 
                    WHEN f.Quarter = 'Q1' THEN CONVERT(VARCHAR(4), YEAR(@f_yr) - 1) + right('0' + cast(f.FyMonthNumber as varchar(2)))
                    WHEN f.Quarter = 'ALL' AND f.FyMonth IN ('OCT', 'NOV', 'DEC') THEN CONVERT(VARCHAR(4), YEAR(@f_yr) - 1, 112) + right('0' + cast(f.FyMonthNumber as varchar(2)))
                    ELSE CONVERT(VARCHAR(4), YEAR(@f_yr), 112) + right('0' + cast(f.FyMonthNumber as varchar(2)))
                END AS FY_MONTH
            FROM fis f
            WHERE f.Quarter = @qrt)
    AND c.GroupLabel = 'Hr' + @dnum
    GROUP BY c.GroupLabel, FORMAT((CONVERT(DATE, c.changedate)), 'MM-yyyy')
    ORDER BY 1, FORMAT((CONVERT(DATE, c.changedate)), 'MM-yyyy');