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.
Related
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?
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 am building a SSRS Report to show Student Class Information.
I have a table named class with information regarding classes students were attended.
My requirement is to show the days in which the classes occur. For eg: If Column Name Monday = 'Y' then I have to show the day as [Mon]. I have tried below code .
SELECT
case when Monday ='Y' then'[Mon]' else '' end +case when Monday ='Y' then ',' else '' end +
case when Tuesday ='Y' then'[Tue]' else '' end +case when Tuesday ='Y' then ','else '' end +
case when Wednesday='Y' then'[Wed]' else '' end +case when Wednesday ='Y' then ',' else '' end +
case when Thursday ='Y' then'[Thu]' else '' end +case when Thursday ='Y' then ',' else '' end +
case when Friday ='Y' then'[Fri]' else '' end +case when Friday ='Y' then ',' else '' end +
case when Saturday ='Y' then'[Sat]' else '' end +case when Saturday ='Y' then ',' else '' end +
case when Sunday ='Y' then'[Sun]' else '' end as classday
FROM vw_Class_Without_Instructor
I got the output as below.
In output there is an extra comma at the end of each line. Any way to exclude it in the select statement itself?. Is there any other way to get the output?. Use of case statements seems to be slow down the execution.
Please try this below query
SELECT
SUBSTRING(case when Monday ='Y' then',[Mon]' else '' end +
case when Tuesday ='Y' then',[Tue]' else '' end +
case when Wednesday='Y' then',[Wed]' else '' end +
case when Thursday ='Y' then',[Thu]' else '' end +
case when Friday ='Y' then',[Fri]' else '' end +
case when Saturday ='Y' then',[Sat]' else '' end +
case when Sunday ='Y' then',[Sun]' else '' end, 2,200) as classday
FROM vw_Class_Without_Instructor
trick: I've added commas with text so no half number of case evaluations. With ,[Mon] value I've ensured that there is always a comma in beginning which is removed using SUBSTRING.
be wise to choose a very high value in last part parameter of SUBSTRING so that it return everything starting from the charindex in second part.
See msdn reference: https://msdn.microsoft.com/en-us/library/ms187748.aspx
If the sum of start and length is greater than the number of
characters in expression, the whole value expression beginning at
start is returned.
You can try this;
select
CASE WHEN LEN(classday_with_comma) > 0 THEN LEFT(classday_with_comma,LEN(classday_with_comma) - 1) ELSE '' END as classday
From (
SELECT
case when Monday = 'Y' then '[Mon],' else '' end +
case when Tuesday = 'Y' then '[Tue],' else '' end +
case when Wednesday = 'Y' then '[Wed],' else '' end +
case when Thursday = 'Y' then '[Thu],' else '' end +
case when Friday = 'Y' then '[Fri],' else '' end +
case when Saturday = 'Y' then '[Sat],' else '' end +
case when Sunday = 'Y' then '[Sun],' else '' end as classday_with_comma
FROM vw_Class_Without_Instructor
) as x
Two case statement can be truncated to one from
case when Monday ='Y' then'[Mon]' else '' end +
case when Monday ='Y' then ',' else '' end +
To
case when Monday = 'Y' then '[Mon],' else '' end +
And
LEFT(classday_with_comma, LEN(classday_with_comma) - 1)
will remove the last ,
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
Why is it that I can't do the below script and how would I go about fixing it? I get the error Inccorect syntax near '='. I was doing it using ...= 1 THEN 'D' ELSE '' END as Aset but I need to check Aset and Bset in the third Case so I declared Aset and Bset instead.
declare #Aset varchar(10)
declare #Bset varchar(10)
UPDATE m
SET m.new_name = m.new_name + ' ' + #ASet + AndSet + #BSet + ' Type'
FROM contactMaster m
inner join contact c on
m.contactid = c.contactid
CROSS APPLY (
SELECT CASE #Aset WHEN (c.category1|
c.category2|
c.category3|
c.category4) = 1 THEN 'C' ELSE '' End
,CASE #Bset WHEN (c.category5|
c.category6|
c.category7|
c.category8) = 1 THEN 'D' ELSE '' END
,CASE WHEN #BSet = 'D' and #ASet = 'C' THEN ' & ' ELSE '' END AS AndSet
) AS CA1
I don't have your table schema or data, so I had to wing it and couldn't test it, but see if this might work:
UPDATE m
SET m.new_name = m.new_name
+ ' '
+ CA1.ASet
+ CASE WHEN
(
CA1.Aset = 'C' AND
CA1.Bset = 'D'
)
THEN ' & ' ELSE '' END
+ CA1.BSet + ' Type'
FROM contactMaster m
JOIN contact c on m.contactid = c.contactid
CROSS APPLY (
SELECT CASE WHEN
(
c.category1 |
c.category2 |
c.category3 |
c.category4
)
= 1 THEN 'C' ELSE '' END AS Aset,
CASE WHEN
(
c.category5 |
c.category6 |
c.category7 |
c.category8
)
= 1 THEN 'D' ELSE '' END AS Bset
) AS CA1