[英]How to get and populate Zero '0' if no record found in SSRS Report 2005
I am using SSRS report to get the result based on the category and the category value changed to Yes,No,Not Met by replacing with 1 = Yes, 2 = No, 3 = Not Met. 我正在使用SSRS报告来获取基于类别的结果,并且类别值更改为Yes,No,Not Met,方法是替换为1 =是,2 =否,3 =不符合。 The query is,
查询是
SELECT (
CASE
WHEN A = '99' or B = '99' THEN 3
WHEN C + D >= 10 THEN 1
ELSE 2
END) as Category,
and the result is, 结果是
N % N % N %
Not Met 10 11% 5 7% 45 20%
Yes 4 5% 30 4% 8 6%
No 10 11% 5 7% 45 20%
and if for example no result found for "Not Met", i want result like this with Zeros instead. 例如,如果没有找到“ Not Met”的结果,我希望将结果改为零。
N % N % N %
Not Met 0 0% 0 0% 0 0%
Yes 4 5% 30 4% 8 6%
No 10 11% 5 7% 45 20%
I have tried Left Join in the query but it will bring extra record. 我已经在查询中尝试了Left Join,但是它将带来额外的记录。 i am kind of stuck and don't know how to get Zero '0' result if no record found for Yes, No or Not Met.
如果没有找到“是”,“否”或“未达到”的记录,我有点卡住,不知道如何获得零“ 0”结果。 My query is,
我的查询是
SELECT (
CASE
WHEN A = '99' or B = '99' THEN 3
WHEN C + D >= 10 THEN 1
ELSE 2
END
) as Category, [Table A].*, [Table B].*
FROM Table A inner join Table B on Table A.id = Table B.id
WHERE
(
Condition and Field is not null
)
Please help as this is my final project and i am stuck. 请帮忙,因为这是我的最后一个项目,我被困住了。
Thanks. 谢谢。
MY SAMPLE DATA
TABLE A
=======
ID -------REFNO-------BGDATE-------SBRST-------xx xx ...
-- --------- -- --
1209-------23-------09/09/1900-------13-------XX XX
3453-------12-------14/02/1978-------10-------XX XX
3476-------56-------02/03/1980-------10 -------XX XX
TABLE B
=======
ID-------- CITY -------xx xx ... xx
-- -- -- --
1209-------Glasgow-------xx-------X
3453-------Edinburgh-----xx-------X
3476-------Manchester----xx-------X
SELECT
(
CASE
WHEN BGDATE = '09/09/1900' THEN 3
--I tried this to get Value 3, if no condition met but no success
WHEN NOT EXISTS(BGDATE = '09/09/1900') THEN 3
WHEN SBRST IN ('11','12') THEN 1
ELSE 2
END
) as Category, [Table - A].*, [Table - B].*
FROM [Table - A] inner join [Table - A] on [Table - A].id = [Table - A].ID
----> For Ian <-----This is sample data of my WHERE clause
WHERE
(
(
SBRST IN ('4','5','6','7','11','12') AND SBRST<>'99
AND NOT
(
EXTNT IN ('2','3','4','99') OR
HOR IN ('2','99') OR
(DATEDIFF(day,CHDATE1,CHENDATE1)>='42')
)
)
AND
(
SBRST IS NOT NULL AND
EXTNT IS NOT NULL AND
HOR IS NOT NULL
)
)
order by Category desc
RESULT
======
category----------ID----------REFNO----------BGDATE----------SBRST----------xx
3-----------------1209----------23----------09/09/1900----------13----------XX
2-----------------3453----------12----------14/02/1978----------10----------XX
2-----------------3476----------56----------02/03/1980----------10----------Xx
Points to be considered: 要考虑的要点:
1) The above is my sample data. 1)以上是我的示例数据。
2) Table A and B has inner joins on ID will bring the results. 2)表A和B在ID上具有内部联接将带来结果。
3) Populating the Category based on the above SELECT condition, but the problem is, *if there is no condition matching, no category gets populated but i want the missing category as well. 3)根据上述SELECT条件填充类别,但是问题是,*如果没有条件匹配,则不会填充任何类别,但我也希望缺少类别。 In this case it is "1".
在这种情况下,它是“ 1”。
I want some thing like this. 我想要这样的东西。
EXPECTED RESULT
======
category----------ID----------REFNO----------BGDATE----------SBRST----------xx
3-----------------1209----------23----------09/09/1900----------13----------XX
2-----------------3453----------12----------14/02/1978----------10----------XX
2-----------------3476----------56----------02/03/1980----------10----------XX
1-----------------0-------------0-----------NULL----------------0-----------NULL
Another solution could be, if i create another table with all the 3 categories in it and then use RIGHT OUTER JOIN to get the results but i do not know HOW?? 另一个解决方案可能是,如果我创建一个包含所有3个类别的表,然后使用RIGHT OUTER JOIN获取结果,但我不知道怎么办?
I had this issue once. 我曾经有这个问题。 I did a quick fix by creating a temp table and inserting the results into the temp table and then checked to see if any of the three categories were missing from the temp table then inserted a row into the temp table with the desired default values in there.
我通过创建临时表并将结果插入到临时表中进行了快速修复,然后检查以查看临时表中是否缺少这三个类别,然后在其中插入了具有所需默认值的一行到临时表中。 Below is an example code (just to give you an idea)
下面是一个示例代码(只是为了给您一个想法)
SELECT (
CASE
WHEN A = '99' or B = '99' THEN 3
WHEN C + D >= 10 THEN 1
ELSE 2
END
) as Category, [Table A].*
INTO #Temp
FROM [Table A]
IF NOT EXISTS (SELECT 1 FROM #Temp WHERE Category=1)
BEGIN
INSERT INTO #Temp (Category,column1,column2,column3,etc...) VALUES ( 1,0,0,0,etc...
)
END
IF NOT EXISTS (SELECT 1 FROM #Temp WHERE Category=2)
BEGIN
INSERT INTO #Temp (Category,column1,column2,column3,etc...) VALUES ( 2,0,0,0,etc...
)
END
IF NOT EXISTS (SELECT 1 FROM #Temp WHERE Category=3)
BEGIN
INSERT INTO #Temp (Category,column1,column2,column3,etc...) VALUES ( 3,0,0,0,etc...
)
END
SELECT * FROM #Temp
DROP TABLE #temp
You can use a query something like this: 您可以使用类似以下的查询:
select cat.Category
, a.ID
, b.CITY
, a.BGDATE
, a.REFNO
, a.SBRST
from
(
select Category = 1
union all
select Category = 2
union all
select Category = 3
) cat
left join
(
[Table - A] a
inner join [Table - B] b on a.ID = b.ID
cross apply
(
SELECT Category = CASE WHEN a.BGDATE = '09/09/1900' THEN 3
WHEN a.SBRST IN ('11','12') THEN 1
ELSE 2
END
) c
) on c.Category = cat.Category
order by Category desc
I've created a SQL Fiddle which shows this gives the required results. 我创建了一个SQL Fiddle ,它显示了所需的结果。
The key point to note is that I am using a subquery to create the required categories (the cat
subquery), then using a left join
to join this to the actual results - this makes sure all required categories are always included. 需要注意的关键点是,我正在使用子查询创建所需的类别(
cat
子查询),然后使用left join
将其连接到实际结果中-这确保始终包含所有必需的类别。
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.