I am using MySQL for my Shiny App. I am using this query for App and it is running perfectly okay.
Select
concat(monthname(date_of_test), '-', year(date_of_test)) as 'Time',
product_group AS 'ProductGroup',
Pass,
Case
when pass='N' then @no:=count(distinct serial_number)
when pass='Y' then count(distinct serial_number)-@no
end as Count
from test_data
where
year(date_of_test)=2018
and product_group='BHO'
and month(date_of_test) between 3 and 4
group by
product_group,
month(date_of_test),
pass
But I need to change it in MS SQL Server. I have tried with declaring as variable and use it as in SQL Server.
My try in SQL Server:
declare @no int;
set @no = 0;
Select
CONCAT(datename(MM, date_of_test), '-', DATENAME(YY,date_of_test)) as 'Time',
product_group AS 'ProductGroup',
Pass,
case
when pass ='N' then @no = count(distinct serial_number)
when pass ='Y' then count(distinct serial_number)- @no
end as 'Count'
from test_data
where
year(date_of_test)=2018
and product_group='BHO'
and month(date_of_test) between 3 and 5
group by
product_group,
CONCAT(datename(MM, date_of_test),
'-',
DATENAME(YY,date_of_test)),
pass
The query without the variable is like:
Select
CONCAT(datename(MM, date_of_test), '-', DATENAME(YY,date_of_test)) as 'Time',
product_group AS 'ProductGroup',
Pass,
case
when pass ='N' then count(distinct serial_number)
when pass ='Y' then count(distinct serial_number)
end as 'Count'
from test_data
where
year(date_of_test)=2018 and product_group='BHO'
and month(date_of_test) between 3 and 4
group by
product_group,
CONCAT(datename(MM, date_of_test),
'-',
DATENAME(YY,date_of_test)),
pass
and it is producing the following output:
The desired output was like which is from MySQL. Please take a look where Pass=Y then the value of Pass=N subtracted from it.
It is showing an error.
My initial assumption: in MySQL I can initialize variable in query and can use it within it,but in MS SQL Server may be there is other rules. My syntax or process can be wrong.
Select Count(distinct serial_number) from Test_Data where year(date_of_test)=2018 and product_group='BHO'and month(date_of_test)=4
503
Select Count(distinct serial_number) from Test_Data where year(date_of_test)=2018 and product_group='BHO' and PASS='Y' and month(date_of_test)=4
503
Select Count(distinct serial_number) from Test_Data where year(date_of_test)=2018 and product_group='BHO' and PASS='N'and month(date_of_test)=4
71
SO all 503 product(serial number) gone for multiple test and get Pass=Y value but 71 product have gone through the same test where they have failed in some case where it is noted as Pass=N. So if I can calculate the (distinct serial_number with PASS=y)-(distinct serial_number with PASS=N) then it will give number of products who pass all the tests.
I can do this and the result is:
Select CONCAT(datename(MM, date_of_test),'-',DATENAME(YY,date_of_test)) as 'Time',product_group AS 'ProductGroup',
(Count(Distinct case when PASS='Y' then serial_number end)-Count(Distinct case when PASS='N' then serial_number end))
as ' All Test Passed',
Count(Distinct case when PASS='N' then serial_number end) as 'Min 1 Test Failed'
from test_data
where
year(date_of_test)=2018
and
month(date_of_test) between 3 and 4
and product_group='BHO'
group by product_group,CONCAT(datename(MM, date_of_test),'-',DATENAME(YY,date_of_test))
And the result is