3

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');
suffa
  • 3,606
  • 8
  • 46
  • 66
  • `INNER JOIN` and `WHERE` will limit records return. I suspect the issue is in the `AND CONVERT(VARCHAR(6), c.changedate, 112)` part. It's complicated and is implementing some kind of time logic in the formatting. I bet it doesn't work as you expect. Try hard coding some values in the `IN` part for starters. – Nick.Mc Jan 17 '19 at 02:23
  • 3
    This is where the power of CTEs come in. Not for technical processing power, rather developer readability power. Create a CTE of the flat data (i.e. no GROUP BY), applying your CASE statements and extracting the DATEPARTs for MONTH and YEAR, aliasing your field names, etc. Then, when all that ugly work is done, select the aggregates from that CTE. – HardCode Jan 18 '19 at 21:43

6 Answers6

2

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');
S3S
  • 24,809
  • 5
  • 26
  • 45
  • I made changes to those areas that you pointed out, but still, I get no results. This is really stumping me... – suffa Jan 17 '19 at 02:16
  • 3
    Well it’s one of the areas I pointed out. There is no other place for filtering. Remove them all and add them back one by one until you find the culprit – S3S Jan 17 '19 at 02:23
  • @suffa i made an edit to my post. Read from EDIT downward and let me know if you have any questions. – S3S Jan 23 '19 at 17:29
1

Try to change to this (look at the 1st convert):

...
AND CONVERT(VARCHAR(7), c.changedate, 120) 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)
...

You was converting 112 (yyyymm) instead of 120 (yyyy-mm) and your inner select returns yyyy-mm

Xabi
  • 465
  • 5
  • 8
  • I will try that again. When adding above, I may have copied and pasted erroneously when posting. But, I was careful to pay careful attention to that (formatting). I've changed it some umpteen times, trying everything to return data. – suffa Jan 18 '19 at 22:28
  • Next attempt in further answer – Xabi Jan 19 '19 at 14:34
1

JOIN or/and WHERE clauses can be a reason. Following basic deduction method is to figure out which part of the query gives such result:

Firstly, eliminate all WHERE clauses and check if current JOIN can return rows by setting WHERE this way:

WHERE 1 = 1
--AND 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

Then, uncomment WHERE statetements one by one to figure out which one filters rows:

WHERE 1 = 1
AND 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

Then, another statement:

WHERE 1 = 1
AND 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

And so on, until you get into the point when no rows returned

This technique will bring you eventually to a part (parts) of the query which filters rows out

If original dataset returns too big number of rows it can be expensive to retrieve all of them during the debugging, so I would recomend to comment them out and use COUNT(*) instead:

SELECT COUNT(*)
/*
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 
Alexander Volok
  • 5,630
  • 3
  • 17
  • 33
0

My bet is on different column definitions

DECLARE @qrt VARCHAR(3);

vs

whatever is FROM FyQm f   WHERE f.Quarter = @qrt

'Q2 ' with a blank or null probably does not equal  f.Quarter which may be defined as VARCHAR(2)

Perhaps we could try this sql to see that each criterion has some rows

Select
  Sum(1) as cntAll
 ,Sum (CASE When c.Group = 'HR' Then 1 Else 0 End) as cntGroup  
 ,Sum (CASE When c.bFlag IS NULL Then 1 Else 0 End) as cntbFlag
 ,Sum (CASE When s.Report IN ('P', 'N') Then 1 Else 0 End) as cntsReport
 ,Sum (CASE When CONVERT(VARCHAR(6), c.changedate, 112) 
    IN ('200204', '200205', '200206') Then 1 Else 0 End) as cntchangedate
 ,Sum (CASE When c.GroupLabel = 'Hr43234' Then 1 Else 0 End) as cntGroupLabel

FROM cforms c 
INNER JOIN spitems s
ON c.Id = s.FormId

Maybe time to follow @scsimon advice, and add back criteria one at a time and see which one blocks all of the rows

--  WHERE c.Group = 'HR'
--  AND c.bFlag IS NULL
--  AND s.Report IN ('P', 'N')
--  AND CONVERT(VARCHAR(6), c.changedate, 112) IN ('200204', '200205', '200206')
--  AND c.GroupLabel = 'Hr43234'
S3S
  • 24,809
  • 5
  • 26
  • 45
donPablo
  • 1,937
  • 1
  • 13
  • 18
  • In the Schema, f.Quarter is defined as varchar(3), not null – suffa Jan 20 '19 at 02:22
  • Could you try in the script to add the trailing blank SET @qrt = 'Q2 '; – donPablo Jan 20 '19 at 02:28
  • Thanks. I've also noticed that sometimes HR is all caps, and sometimes Hr is cap and lower. Is that correct? Also, what is the count of rows (and some sample data) for the inner Select... From FY ? When just that part is run separately. – donPablo Jan 20 '19 at 05:53
  • Aha. AND CONVERT(VARCHAR(6), c.changedate, 112) IN gives YYYYMM. But the inner select delivers YYYY-MM with a hyphen. Ergo, not found in it. – donPablo Jan 20 '19 at 06:03
  • It returns a particular quarter in a year Q1,Q2... (represented by 3 months: eg. Q1= 01/2002, 02/ 2002, 03/2002) – suffa Jan 20 '19 at 06:08
  • I think the hypen just got left in the above example when updating. I've validated several times. – suffa Jan 20 '19 at 06:09
  • I didn't say it was solved. I merely reiterated that outside the context of the entire script that piece of code returns data, as mentioned when I first posted. – suffa Jan 22 '19 at 02:34
0

I've reformated your code with remarks:

declare @u_cnt int, @f_yr date, @qrt varchar(3), @dnum varchar(5);
select @u_cnt = 10000, @f_yr = '20020520', @qrt = 'Q2', @dnum = '43234';

select c.GroupLabel as ORG_CODE
      -- Assuming c.changedate is datetime, otherwise cast(c.changedate as datetime)
    , format(c.changedate, 'MM-yyyy') as [MONTH] 
    , count(distinct case s.TestType when 'IR' then c.changedate else null end) as IR_TEST_DAYS
    , count(distinct c.changedate) as TEST_DAYS
    , count(s.Id) as TOTAL
    , (case when (@u_cnt is null) then - 1 else @u_cnt end) as BOARD_CNT
    , format((count(s.Id) /
        -- avoiding also division by 0
        case when isnull(@u_cnt, 0) = 0
            then - 1
            else @u_cnt end), 'P0')
        as PCT
    , case
        when 100 * (count(s.Id) / 
            -- avoiding also division by 0
            case when isnull(@u_cnt, 0) = 0
                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 as PCT_TEST_COMP
from cforms c
join spitems s 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 -- yyyymm (ISO format)
        (
        select 
            cast(year(@f_yr) + 
                case
                    when f.Quarter = 'Q1'
                        then (-1)
                    when f.Quarter = 'ALL'
                        and f.FyMonthNumber in ('10', '11', '12')
                        then (-1)
                    else (0)
                end as varchar(4))
                + f.FyMonthNumber -- JAN = '01' or '1' ?
        from FyQm f
        where f.Quarter = @qrt
        )
    and c.GroupLabel = 'Hr' + @dnum
group by c.GroupLabel
      -- Assuming c.changedate is datetime, otherwise cast(c.changedate as datetime)
    , format(c.changedate, 'MM-yyyy')
order by ORG_CODE, [MONTH];

Could you check if FyQm.FyMonthNumber is varchar(2) or char(2) and represents January as '01' instead of '1'?

suffa
  • 3,606
  • 8
  • 46
  • 66
Xabi
  • 465
  • 5
  • 8
  • it yields the same result that I'm getting: no data! This is strange; that convert block works as expected when run individually. – suffa Jan 20 '19 at 02:42
0

Your main question is why you are not getting data for given query ? So you want to debug and check where the problem is.

So for given parameter,

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';

so start from basic

 select * 
    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')

note the comments part,does it return data, if yes then uncomment AND c.bFlag IS NULL and this way uncomments other part.

Are you sure it will be INNER JOIN or LEFt JOIN ?

Put the period subquery in temp table,though this is not main reason,if it return less records then you can use CTE also,

Create table #tempperiod(period varchar(6))
insert into #tempperiod(period)
select 
            cast(year(@f_yr) + 
                case
                    when f.Quarter = 'Q1'
                        then (-1)
                    when f.Quarter = 'ALL'
                        and f.FyMonthNumber in ('10', '11', '12')
                        then (-1)
                    else (0)
                end as varchar(4))
                + f.FyMonthNumber 
        from FyQm f
        where f.Quarter = @qrt

-- in order to test,does it return any records,does it return desire output
select * from #tempperiod
  1. Check for space (LTRIM and RTRIM) in columns which is use in predicate.
  2. Avoid division by 0 in your case statement
  3. If it is really,INNER JOIN then use EXISTS clause coz you don't require spitems sp columns .
  4. What is ORDER BY 1 ? GroupLabel ? then you don't need them in Order clause coz all rows will be 'HR'+'43234'
  5. ABOVE all,you don't need Order by at all,coz Group By will sort it for you and that is the only requirement.
  6. Thoroughly check #tempperiod data,is the format same as

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

KumarHarsh
  • 5,046
  • 1
  • 18
  • 22