How can I use case when .. then in #Parameters in MS Report Builder?
For example this code does not work correct, because Filter does not work
SELECT DISTINCT case
when [APPROVAL_STATUS] = 'approved' then '1'
when [APPROVAL_STATUS] = 'denied' then '2'
when [APPROVAL_STATUS] = 'pending' then '3'
when isnull([APPROVAL_STATUS], ' ') = '' then '4'
ELSE [APPROVAL_STATUS]
end as [APPROVAL_STATUS]
FROM [dbo].[SUMMARY]
Is it possible?
Related
I'm trying to use a variable in my where clause, but cant get it to work. When I fill in the actual value from the variable, it works.
I'm defining the variable by:
DECLARE #SalesorderTracking nvarchar(MAX)
SET #SalesorderTracking = ''; select #SalesorderTracking = CAST('''' + c_loadcarrier as Nvarchar(max)) + '''' + ',' + #SalesorderTracking from [ApportMM].[dbo].[#SalesorderTracking]
SET #SalesorderTracking = LEFT(#SalesorderTracking, LEN(#SalesorderTracking) -1)
PRINT #SalesorderTracking
The print command returns this value: 'PL10275703','PL10275700','PL10269031','PL10269030','PL10271642'
Running the following query returns no records.
SELECT [MyDB].[dbo].[t_Item_log].[c_id]
,[MyDB].[dbo].[t_Item_log].[c_Info]
,CASE [c_TypeRef]
WHEN '1' THEN 'Loadcarrier From'
WHEN '2' THEN 'Loadcarrier To'
END AS [Type]
,[c_loadcarrier]
,[c_WorkerName]
,[c_Material]
,[c_Qty]
,[c_Created]
,[c_ShelfLabelFrom]
,[c_WarehouseFrom]
,[c_WareHouseAreaFrom]
,[c_ZoneFrom]
,[c_TransactionId]
,[c_ItemRef]
FROM dbo.t_Item_log INNER JOIN dbo.t_Item_log_LoadCarrier ON dbo.t_Item_log.c_id = dbo.t_Item_log_LoadCarrier.c_Item_LogRef
where c_loadcarrier in (#SalesorderTracking) AND (c_TypeRef = '1' OR c_TypeRef = '2')
order by [c_loadcarrier] desc, [Type] desc
But running the following query, where I have changed the #SalesorderTracking variable with the actual value returns the expected records.
SELECT [MyDB].[dbo].[t_Item_log].[c_id]
,[MyDB].[dbo].[t_Item_log].[c_Info]
,CASE [c_TypeRef]
WHEN '1' THEN 'Loadcarrier From'
WHEN '2' THEN 'Loadcarrier To'
END AS [Type]
,[c_loadcarrier]
,[c_WorkerName]
,[c_Material]
,[c_Qty]
,[c_Created]
,[c_ShelfLabelFrom]
,[c_WarehouseFrom]
,[c_WareHouseAreaFrom]
,[c_ZoneFrom]
,[c_TransactionId]
,[c_ItemRef]
FROM dbo.t_Item_log INNER JOIN dbo.t_Item_log_LoadCarrier ON dbo.t_Item_log.c_id = dbo.t_Item_log_LoadCarrier.c_Item_LogRef
where c_loadcarrier in ('PL10275703','PL10275700','PL10269031','PL10269030','PL10271642') AND (c_TypeRef = '1' OR c_TypeRef = '2')
order by [c_loadcarrier] desc, [Type] desc
I really can't figure out what I'm doing wrong with that variable.
Any help and suggestions appreciated, thanks.
What's your SQL Server version? Version 14.0
I think this should work:
SELECT [MyDB].[dbo].[t_Item_log].[c_id]
,[MyDB].[dbo].[t_Item_log].[c_Info]
,CASE [c_TypeRef]
WHEN '1' THEN 'Loadcarrier From'
WHEN '2' THEN 'Loadcarrier To'
END AS [Type]
,[c_loadcarrier]
,[c_WorkerName]
,[c_Material]
,[c_Qty]
,[c_Created]
,[c_ShelfLabelFrom]
,[c_WarehouseFrom]
,[c_WareHouseAreaFrom]
,[c_ZoneFrom]
,[c_TransactionId]
,[c_ItemRef]
FROM dbo.t_Item_log
INNER JOIN dbo.t_Item_log_LoadCarrier
ON dbo.t_Item_log.c_id = dbo.t_Item_log_LoadCarrier.c_Item_LogRef
where c_loadcarrier in
(SELECT c_loadcarrier
FROM [ApportMM].[dbo].[#SalesorderTracking])
AND (c_TypeRef = '1' OR c_TypeRef = '2')
order by [c_loadcarrier] desc, [Type] desc
There is a query in which I have to concatenate 12 columns in a case statement. It would've been easier if it was 2 or 3. To be clearer, here is my query:
SELECT column1
,column2
,effet_secondaire_desc = CASE WHEN [symp01] = 'y' THEN 'Pedi apeti'
WHEN [symp02] = 'y' THEN 'Ke plen'
WHEN [symp03] = 'y' THEN 'Zye jon'
WHEN [symp04] = 'y' THEN 'Dyare'
WHEN [symp05] = 'y' THEN 'Ko grate'
WHEN [symp06] = 'y' THEN 'Lafyev ak frison'
WHEN [symp07] = 'y' THEN 'Ko fe mal ak jwenti fe mal'
WHEN [symp08] = 'y' THEN 'Pikotman'
WHEN [symp09] = 'y' THEN 'Fatig'
WHEN [symp10] = 'y' THEN 'Tet vire/vetij'
WHEN [symp11] = 'y' THEN 'Vant fe mal'
WHEN [symp12] = 'y' THEN [symp12_desc]
ELSE '~'
END
.....
,columnx
FROM MyTable
[symp01] ... [symp12] are the 12 columns I want to concatenate. So the value of effet_secondaire_desc should be something like this 'Pedi apeti, Ko grate, Lafyev ak frison, ...'.
Can anyone help me with this please?
Thanks in advance.
select
column1
, column2
, effet_secondaire_desc = isnull(nullif(stuff(
case when [symp01] = 'y' then ', Pedi apeti' else '' end
+case when [symp02] = 'y' then ', Ke plen' else '' end
+case when [symp03] = 'y' then ', Zye jon' else '' end
+case when [symp04] = 'y' then ', Dyare' else '' end
+case when [symp05] = 'y' then ', Ko grate' else '' end
+case when [symp06] = 'y' then ', Lafyev ak frison' else '' end
+case when [symp07] = 'y' then ', Ko fe mal ak jwenti fe mal' else '' end
+case when [symp08] = 'y' then ', Pikotman' else '' end
+case when [symp09] = 'y' then ', Fatig' else '' end
+case when [symp10] = 'y' then ', Tet vire/vetij' else '' end
+case when [symp11] = 'y' then ', Vant fe mal' else '' end
+case when [symp12] = 'y' then ', '+[symp12_desc] else '' end
,1,2,''),''),'~')
, columnx
from mytable
So something like this?:
SELECT
...
CASE WHEN [symp01] = 'y' THEN ', Pedi apeti' ELSE '' END
+ CASE WHEN [symp02] = 'y' THEN ', Ke plen' ELSE '' END
+ ...
END
And then wrap that in some logic to remove the first comma and space.
Using Pentaho Report Designer I want to take data from columns in a database and have this data displayed in a line.
See my example below. Is there a way I can accomplish this?
My database table looks like this:
GENUS SPECIFIC_EPITHET SPECIES_AUTHOR
Asplenium scolopendrium L.
Asplenium bradleyi D.C. Eaton
Asplenium platyneuron (L.) Britton, Sterns & Poggenb.
Asplenium viride Huds.
I would like the report to look like this:
Asplenium scolopendrium L., Asplenium bradleyi D.C. Eaton, Asplenium platyneuron (L.) Britton, Sterns & Poggenb., Asplenium viride Huds.
I am only able to get the report to show the data in a column like list as shown below.
Asplenium scolopendrium L.
Asplenium bradleyi D.C. Eaton
Asplenium platyneuron (L.) Britton, Sterns & Poggenb.
Asplenium viride Huds.
It would also be helpful if someone would tell me if this is not possible in PRD.
I figured this out thanks to being shown how by Deanicus on the Pentaho Reporting Forum (see here).
The main trick was to query my database before I got my data into the report via PRD. Since I am working with a POSTGRESQL database the code I used to query the database looked something like this.
SELECT
string_agg(CONCAT(CONCAT(CONCAT(
c."genus", ' '),
c."specific_epithet",' '),
c."species_author"),', ') as scinamewauthor
FROM
"public"."main_checklist" as c
My final query was actually a lot more complicated since I was adding all sorts of spaces and html formatting marks. The entire piece of code actually looks like this.
SELECT
string_agg (CONCAT(
(SELECT CASE WHEN s."genus_synonym" IS NULL THEN '' ELSE '<i>'||s."genus_synonym"||'</i>' END),
(SELECT CASE WHEN s."specific_epithet_synonym" IS NULL THEN '' ELSE ' <i>'||s."specific_epithet_synonym"||'</i>' END),
(SELECT CASE WHEN s."species_author_synonym" IS NULL THEN '' ELSE ' '||s."species_author_synonym" END),
(SELECT CASE WHEN s."subspecific_epithet_synonym" IS NULL THEN '' ELSE ' ssp. '||'<i>'||s."subspecific_epithet_synonym"||'</i>'END),
(SELECT CASE WHEN s."subspecies author_synonym" IS NULL THEN '' ELSE ' '|| s."subspecies author_synonym" END),
(SELECT CASE WHEN s."varietal_epithet_synonym" IS NULL THEN '' ELSE ' var. <i>'||s."varietal_epithet_synonym"||'</i>' END),
(SELECT CASE WHEN s."variety_author_synonym" IS NULL THEN '' ELSE ' '|| s."variety_author_synonym" END),
(SELECT CASE WHEN s."misapplied" = 'misapplied hybrid formula 1' THEN ' misapplied' ELSE '' END),
(SELECT CASE WHEN s."hybrid_formula_genus_synonym" is null THEN '' ELSE ' × <i>'||substring("hybrid_formula_genus_synonym" from 1 for 1)||'.</i>'END),
(SELECT CASE WHEN s."hybrid_formula_specific_epithet_synonym" is null THEN '' ELSE ' <i>'|| s."hybrid_formula_specific_epithet_synonym"||'</i>' END),
(SELECT CASE WHEN s."hybrid_formula_species_author_synonym" is null THEN '' ELSE ' '||s."hybrid_formula_species_author_synonym" END),
(SELECT CASE WHEN s."hybrid_formula_subspecific_epithet_synonym" IS NULL THEN '' ELSE ' ssp. <i>'||s."hybrid_formula_subspecific_epithet_synonym"||'</i>' END),
(SELECT CASE WHEN s."hybrid_formula_subspecies_author_synonym" IS NULL THEN '' ELSE ' '||s."hybrid_formula_subspecies_author_synonym" END),
(SELECT CASE WHEN s."hybrid_formula_varietal_epithet_synonym" IS NULL THEN '' ELSE ' var. <i>'||s."hybrid_formula_varietal_epithet_synonym"||'</i>'END),
(SELECT CASE WHEN s."hybrid_formula_variety_author_synonym" IS NULL THEN '' ELSE ' '||s."hybrid_formula_variety_author_synonym"END),
(SELECT CASE WHEN s."misapplied" = 'misapplied hybrid formula 2' THEN ' misapplied' ELSE '' END),
(SELECT CASE WHEN s."genus_hybrid_synonym" IS NULL THEN '' ELSE '×'End),
(SELECT CASE WHEN s."hybrid_name_genus_synonym" IS NULL THEN ''
WHEN s."genus_synonym" IS NULL THEN '<i>'||s."hybrid_name_genus_synonym"||'</i>'
Else ' = <i>'||"hybrid_name_genus_synonym" ||'</i>'END),
(SELECT CASE WHEN s."hybrid_name_specific_epithet_synonym" IS NULL THEN ''
when s."genus_hybrid_synonym" is null then ' ×<i>'||s."hybrid_name_specific_epithet_synonym"||'</i>'
ELSE ' <i>'||s."hybrid_name_specific_epithet_synonym"||'</i>' END),
(SELECT CASE WHEN s."hybrid_name_species_author_synonym" IS NULL THEN '' ELSE ' '||s."hybrid_name_species_author_synonym" END),
(SELECT CASE WHEN s."hybrid_name_subspecific_epithet_synonym" IS NULL THEN '' ELSE ' ssp. <i>'||s."hybrid_name_subspecific_epithet_synonym"||'</i>'END),
(SELECT CASE WHEN s."hybrid_name_subspecies_author_synonym" IS NULL THEN '' ELSE ' '||s."hybrid_name_subspecies_author_synonym"END),
(SELECT CASE WHEN s."hybrid_name_varietal_epithet_synonym" IS NULL THEN '' ELSE ' var. <i>'||s."hybrid_name_varietal_epithet_synonym"||'</i>'END),
(SELECT CASE WHEN s."hybrid_name_variety_author_synonym" IS NULL THEN '' ELSE ' 'END),
s."hybrid_name_variety_author_synonym",
(SELECT CASE WHEN (s."misapplied" IS NULL or s."misapplied" = 'misapplied hybrid formula 1' or s."misapplied" = 'misapplied hybrid formula 2') Then ''
WHEN s."misapplied" = 'misapplied hybrid name' Then ' misapplied'
Else ' '||"misapplied" END)),
', '
Order by s."genus_synonym" ASC,
s."specific_epithet_synonym" ASC,
s."varietal_epithet_synonym" ASC NULLS FIRST,
s."subspecific_epithet_synonym" ASC NULLS FIRST,
s."hybrid_formula_specific_epithet_synonym" ASC,
s."hybrid_formula_subspecific_epithet_synonym" ASC,
s."hybrid_formula_varietal_epithet_synonym" ASC,
s."hybrid_name_specific_epithet_synonym" ASC)as syno
FROM
"public"."synonyms" as s INNER JOIN "public"."main_checklist" ON s."record_id" = "public"."main_checklist"."record_id"
WHERE
s."record_id" = ${import_record_id}
I have a SQL Server stored proc that contains a CASE statement. However, I need to append the values if multiple conditions are true.
So if a particular record has an Invalid Date And Mileage exceeded, I would like both values to be displayed in the NotArchiveableReason column.
How would I accomplish that?
, CASE
WHEN DateOfLoss < PolicyStartDate THEN 'Invalid Date'
WHEN MilesDriven > TotalMilesAllowed THEN 'Mileage exceeded'
WHEN LossStatusCode != 'R' THEN 'Status code is Review'
Else 'Unknown issue'
END
As NotArchiveableReason
If you want to concatenate results like Invalid Date, Mileage Exceeded then you may be looking for something like this.
ISNULL(
NULLIF(
STUFF(
CASE WHEN DateOfLoss < PolicyStartDate THEN ', Invalid Date' ELSE '' END
+ CASE WHEN MilesDriven > TotalMilesAllowed THEN ', Mileage exceeded' ELSE '' END
+ CASE WHEN LossStatusCode != 'R' THEN ', Status code is Review' ELSE '' END
, 1, 2, '')
,'')
, 'Unknown issue')
As NotArchiveableReason
The STUFF() removes the leading comma. The NULLIF() converts the empty string to null. The ISNULL() will populate "Unknown Issue" when none of the CASE statement conditions are met.
, CASE WHEN DateOfLoss < PolicyStartDate THEN 'Invalid Date ' ELSE '' END
+ CASE WHEN MilesDriven > TotalMilesAllowed THEN 'Mileage exceeded ' ELSE '' END
+ CASE WHEN LossStatusCode != 'R' THEN 'Status code is Review ' ELSE '' END
+ CASE WHEN NOT
( DateOfLoss < PolicyStartDate
AND MilesDriven > TotalMilesAllowed
AND LossStatusCode != 'R') THEN 'Unknown issue ' ELSE '' END
As NotArchiveableReason
, CASE WHEN DateOfLoss < PolicyStartDate THEN 'Invalid Date ' ELSE '' END
+ CASE WHEN MilesDriven > TotalMilesAllowed THEN 'Mileage exceeded ' ELSE '' END
+ CASE WHEN LossStatusCode != 'R' THEN 'Status code is Review ' ELSE '' END
CASE WHEN DateOfLoss >= PolicyStartDate OR MilesDriven <= TotalMilesAllowed
OR LossStatusCode = 'R'
THEN 'Unknown issue' END As NotArchiveableReason
How about expand the result set and let the application handle it
, CASE WHEN DateOfLoss < PolicyStartDate THEN 1 ELSE 0 END as InvalidDate
, CASE WHEN MilesDriven > TotalMilesAllowed THEN 1 ELSE 0 END as MileageExceeded
, CASE WHEN LossStatusCode != 'R' THEN 1 ELSE 0 END as StatusCodeIsReview
Then if all is zero it is an unknown issue?
EDIT
You can try this then. This is using an inner select to first find the problems and then combine it afterwards. In this sollution you can add many different checks in the inner select, and then combine them as you like in the outer.
select case when tmp.InvalidDate is null and tmp.MileageExceeded is null and tmp.StatusCodeIsReview is null then 'Unknown issue' else
stuff
(
(
COALESCE(', ' + NULLIF(tmp.InvalidDate, ''), '') +
COALESCE(', ' + NULLIF(tmp.MileageExceeded, ''), '') +
COALESCE(', ' + NULLIF(tmp.StatusCodeIsReview, ''), '')), 1, 2, '')
end as NotArchiveableReason from
(
select *
, CASE WHEN DateOfLoss < PolicyStartDate THEN 'Invalid Date' ELSE NULL END as InvalidDate
, CASE WHEN MilesDriven > TotalMilesAllowed THEN 'Mileage Exceeded' ELSE NULL END as MileageExceeded
, CASE WHEN LossStatusCode != 'R' THEN 'Status Code Is Review' ELSE NULL END as StatusCodeIsReview
from MyTest
) as tmp
Why dont you go with 3 case statements and then in a new query CONCAT them?
, CASE
WHEN DateOfLoss < PolicyStartDate THEN 'Invalid Date'
END as 1
WHEN MilesDriven > TotalMilesAllowed THEN 'Mileage exceeded'
END as 2
WHEN LossStatusCode != 'R' THEN 'Status code is Review'
END as 3
select 1+2+3 As NotArchiveableReason
I am looking to select from three columns using three parameters, one for each column. If all the parameters are present for the columns then it will return a fourth column. If, however, any one of the three parameters is not found in the table, then it should return a message informing the user which of the parameters is incorrect.
I have tried the following:
SELECT RESULTS, "[status]" =
case
when CostCenterNo <> '800' then 'CentreNotFound'
when EmpNo <> '2' then 'EmpNotFound'
when Surname <> 'sonny' then 'SurnameNotFound'
else null
end
from CostCentres
where
CostCenterNo =
case CostCenterno when
'800' then '800'
else ''
end
and
EmpNo =
case EmpNo when
'2' then '2'
else ''
end
and
Surname =
case Surname when
'sonny' then 'sonny'
else ''
end
this retrieves the correct information when all parameters are correct but then I need for it to say CentreNotFound, EmpNotFound, SurnameNotFound for when the respective parameter is not found in the table.
I have tried looking up the links below but still no luck.
IF statement in SQL Server where clause
Using EXISTS as a column in TSQL
SQL: IF clause within WHERE clause
I have also tried the following code:
select results =
case when CostCenterNo = '800' then
case when EmpNo = '2' then
case when Surname = 'sonny' then
(select results from bcse
where CostCenterNo = 'BW800'
and EmpNo like '2'
and Surname = 'sonny')
else 'surname not found' end
else 'Emp not found' end
else 'center no not found' end
from CostCentres
where (CostCenterno = 'bw800' or CostCenterNo = '%')
and (EmpNo = '2' or EmpNo = '%' )
and (surname = 'sonny' or surname = '%')
The above works for the correct parameters but then again I need it to return the CentreNotFound, EmpNotFound, SurnameNotFound for when the respective parameter is not found in the table.
The WHERE condition should be
where (CostCenterno = 'bw800' or CostCenterNo = '%')
and (EmpNo = '2' or EmpNo like '%' )
and (surname = 'sonny' or surname like '%')
Also if you use parameters, it should be
where (CostCenterno = #CostCenterno or #CostCenterNo = '')
and (EmpNo = #EmpNo or #EmpNo ='' )
and (surname = #surname or #surname ='')
EDIT: You may need to use something like below
declare #error varchar(100)
set #error=
case
when not exists(select * from CostCentres where CostCenterno = #CostCenterno) then
'CenterNotFound'
when not exists(select * from CostCentres where EmpNo = #EmpNo) then
'EmpnoNotFound'
when not exists(select * from CostCentres where surname = #surname) then
'surnameNotFound'
end
select CostCenterno, EmpNo,surname , #error as status
from CostCentres
where
CostCenterno = #CostCenterno
and EmpNo = #EmpNo
and surname = #surname