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. 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.
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. 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. 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.
2) Table A and B has inner joins on ID will bring the results.
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. In this case it is "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??
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.
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.
The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.