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');
Try to change to this (look at the 1st convert):
You was converting 112 (
yyyymm
) instead of 120 (yyyy-mm
) and your inner select returnsyyyy-mm
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:
Then, uncomment WHERE statetements one by one to figure out which one filters rows:
Then, another statement:
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:
My bet is on different column definitions
I've reformated your code with remarks:
Could you check if FyQm.FyMonthNumber is
varchar(2)
orchar(2)
and represents January as '01' instead of '1'?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,
so start from basic
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
orLEFt 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,
LTRIM and RTRIM
) in columns which is use in predicate.INNER JOIN
then useEXISTS
clause coz you don't requirespitems sp
columns .ORDER BY 1
?GroupLabel
? then you don't need them in Order clause coz all rows will be 'HR'+'43234'Order by
at all,cozGroup By
will sort it for you and that is the only requirement.Thoroughly check #tempperiod data,is the format same as
CONVERT(VARCHAR(6), c.changedate, 112)
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.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 asYYYYMM-?
where the ? is whateverf.FyMonthNumber
is. As you can see, this will never match your originalconvert
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 withf.FyMonthNumber
. Iff.FyMonthNumber
is anint
then it will add it.Here you are wanting it to return 200102 but it returns 2003 since it's performing addition. You can cast it as a
varchar
orchar
to fix this.Lastly, an issue you may run into is if
f.FyMonthNumber
is stored as anint
, it won't have the leading zero. Thus, for January it would be represented as1
instead of01
and this would also return zero rows for any month before October. You can handle this with theright
function.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...