Using Pentaho Report Designer how to display data in a line - database

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}

Related

How to remove CASE statements from my results?

I am writing a query to return some info, but the stored procedure wont return the results in the page because the values dont match the table definition. i understand this error. but i'm using the case statement to filtering based off a field (whether the t.TimeCallWasReceived is received or not). when i do this, it allows for the sp to run while either the user checking the db for changes or using signalR to detect db changes. I dont want the CASE statement to be a part of my results. i know i can alter the page or the temp table, but i want to know if there is anything i can do in t-sql to return the results without the case statement in said results. the query is below.
SELECT DISTINCT
c.Num,
'Notified for pickup ' + (CONVERT(varchar(30), c.ServiceCalled, 100)) + '. Number ' + c.Num AS Issue,
RTRIM(c.NameFirst) + ' ' + RTRIM(c.NameLast) AS CusName,
(CASE
WHEN t.TimeCallWasReceived IS NULL THEN CONVERT(varchar(20), t.TimeCallWasReceived, 113)
ELSE 'NO'
END) AS CallReceivedAt
FROM tblCustomer c
LEFT JOIN tblTransportation t ON t.Num = c.Num
WHERE t.TimeCallWasReceived IS NULL
AND c.Type = 'Type1'
AND c.ServiceCalled IS NOT NULL
AND c.TransportNum IS NULL
AND LEFT(c.Num, 2) <> 'CR'
AND LEFT(c.Num, 2) <> 'TR'
AND (c.Status <> 'Void'
OR c.Status <> 'Closed')
AND ((DATEDIFF(MINUTE, c.ServiceCalled, GETDATE())) > 180)
AND c.Type <> 'VOID'
AND c.Type <> 'NON'
AND c.Type <> 'Walk-In'
AND c.Type <> 'CFR'
AND c.CaseType <> 'PAP'
AND c.ServiceCalled >= DATEADD(DAY, -2, GETDATE())
ORDER BY c.Num DESC;
--1. Since the WHERE clause has `t.TimeCallWasReceived IS NULL`, you will see only ---'NO' in the CallReceivedAt result column.
--2. Since the WHERE clause has `c.Type = 'Type1'`, it makes unnecessary to look for
c.Type <> 'VOID'
c.Type <> 'NON'
c.Type <> 'Walk-In'
c.Type <> 'CFR'
--because they are exclusive for the rest of the values different than 'Type1'.
--3. I don't see a way to get rid of the CASE
-- Perhaps you like this way better:
CASE ISNULL(t.TimeCallWasReceived, '')
WHEN '' THEN CONVERT(varchar(20), t.TimeCallWasReceived, 113)
ELSE 'NO'
END

Add multiple in clauses depending if variable is blank or not (stored procedure) SQL Server

How am I able to get this to work I want it to add in clause if the value is not blank and not write the in clause if the value is blank. I have already tried with using OR and It runs but does not give me the results I want. I am using SQL Server 2008.
My variables are:
#orderNumber varchar(max),
#itemCode varchar(max),
#shipTo varchar(max),
#orderQty varchar(max),
#orderType varchar(max)
Now I took out a bunch of the stored procedure as it is a total of 600 lines but this should explain exactly what I want to do. I have searched high and low and have not found any solutions for this.
The query is:
SELECT
u.INVENTORY_ITEM_ID, u.ORDER_TYPE,
u.ORDER_QTY, u.ORDER_NUMBER,
u.SHIP_TO_NAME, u.ITEM_CODE AS SET_BOX,
ii.ITEM_CODE,
U.LANGUAGE_DESC
FROM
UnReleased_DeliveriesSet u
JOIN
SetDetails s ON u.item_code = s.SetCode
FULL OUTER JOIN
InventorySetBox i ON u.ITEM_CODE = i.ITEM_CODE
FULL OUTER JOIN
InventorySetBox ii ON ii.ITEM_CODE = s.ItemCode
FULL OUTER JOIN
SET_BOX_TYPE SB ON SB.ITEM_CODE = u.ITEM_CODE
WHERE
u.item_code LIKE '%.set%'
AND u.item_code NOT LIKE '%NN-%SET%'
AND ii.ITEM_CODE IS NOT NULL
AND ii.SUBINVENTORY_CODE <> 'BMS'
AND u.ITEM_CODE NOT IN ('inner query')
This is what I want to do:
AND
CASE WHEN #itemCode != ''
THEN u.ITEM_CODE IN (SELECT items FROM dbo.Split(#itemCode,','))
WHEN #orderType != ''
THEN u.ORDER_TYPE IN (SELECT items FROM dbo.Split(#orderType,','))
WHEN #orderQty != ''
THEN u.ORDER_QTY IN (SELECT items FROM dbo.Split(#orderQty, ','))
WHEN #orderNumber != ''
THEN u.ORDER_NUMBER IN (SELECT items FROM dbo.Split(#orderNumber, ','))
WHEN #shipTo != ''
THEN u.SHIP_TO_NAME in (SELECT items FROM dbo.Split(#shipTo, ','))
END
GROUP BY
u.INVENTORY_ITEM_ID, u.ITEM_CODE,
U.LANGUAGE_DESC,
i.item_box_quantity,
sb.BOX_TYPE, sb.set_box_qty,
ii.ITEM_CODE,
S.SetCode, RELEASED_STATUS,
u.ORDER_TYPE, u.ORDER_QTY, u.ORDER_NUMBER, u.SHIP_TO_NAME
and 1 = case
when #itemCode != '' then case
when u.ITEM_CODE in (SELECT items FROM dbo.Split(#itemCode,',')) then 1
else 0
end
when #orderType != '' then case
when u.ORDER_TYPE in (SELECT items FROM dbo.Split(#orderType,',')) then 1
else 0
end
when #orderQty != '' then case
when u.ORDER_QTY in (SELECT items FROM dbo.Split(#orderQty,',')) then 1
else 0
end
when #orderNumber != '' then case
when u.ORDER_NUMBER in (SELECT items FROM dbo.Split(#orderNumber,',')) then 1
else 0
end
when #shipTo != '' then case
when u.SHIP_TO_NAME in (SELECT items FROM dbo.Split(#shipTo,',')) then 1
else 0
end
else 0
end
A really hacky way using case expressions:
and 1 = case when #itemCode = '' or u.ITEM_CODE in (SELECT items FROM dbo.Split(#itemCode,',') s) then 1 else 0 end
and 1 = case when #orderType = '' or u.ORDER_TYPE in (SELECT items FROM dbo.Split(#orderType,',') s) then 1 else 0 end
and 1 = case when #orderQty = '' or u.ORDER_QTY in (SELECT items FROM dbo.Split(#orderQty,',') s) then 1 else 0 end
and 1 = case when #orderNumber = '' or u.ORDER_NUMBER in (SELECT items FROM dbo.Split(#orderNumber,',') s) then 1 else 0 end
and 1 = case when #shipTo = '' or u.SHIP_TO_NAME in (SELECT items FROM dbo.Split(#shipTo,',') s) then 1 else 0 end
A still rather hacky way using or
and (#itemCode = '' or u.ITEM_CODE in (SELECT items FROM dbo.Split(#itemCode,',') s))
and (#orderType = '' or u.ORDER_TYPE in (SELECT items FROM dbo.Split(#orderType,',') s))
and (#orderQty = '' or u.ORDER_QTY in (SELECT items FROM dbo.Split(#orderQty,',') s))
and (#orderNumber = '' or u.ORDER_NUMBER in (SELECT items FROM dbo.Split(#orderNumber,',') s))
and (#shipTo = '' or u.SHIP_TO_NAME in (SELECT items FROM dbo.Split(#shipTo,',') s))
A much better way would use dynamic sql to conditionally build your where statement and pass your parameters to sp_executesql
Reference:
An Updated "Kitchen Sink" Example - Aaron Bertand
Dynamic Search Conditions - Erland Sommarskog
Catch-all queries - Gail Shaw
Parameter Sniffing, Embedding, and the RECOMPILE Options - Paul White

SQL - Alias in CASE statements

I have this piece of code (please look below). I keep getting error: "Invalid column name 'SuppFinish2'
SELECT
CASE
WHEN [RegFinish] IS NULL THEN ''
ELSE [RegFinish]
END AS [RegFinish],
CASE
WHEN [SuppFinish] IS NULL THEN ''
ELSE [SuppFinish]
END AS [SuppFinish2],
CASE
WHEN [RegFinish]<[SuppFinish2] THEN '1'
ELSE '0'
END AS [TEST]
FROM TABLE
Is it because of [SuppFinish2] being an alias? Thanks!
As you said its due to alias and aliased columns can be referenced only on order by to logical query flow order
with cte
as
(
SELECT
CASE
WHEN [RegFinish] IS NULL THEN ''
ELSE [RegFinish]
END AS [RegFinish],
CASE
WHEN [SuppFinish] IS NULL THEN ''
ELSE [SuppFinish]
END AS [SuppFinish2]
FROM TABLE
)
select
CASE
WHEN [RegFinish]<[SuppFinish2] THEN '1'
ELSE '0'
END AS [TEST]
from cte
You cant use those aliases in the same level as you created them, becuase they are not existing yet.. wrap your query with another select like this:
SELECT * ,
CASE
WHEN [RegFinish]<[SuppFinish2] THEN '1'
ELSE '0'
END AS [TEST]
FROM (
SELECT
[ID],
CASE
WHEN [RegFinish] IS NULL THEN ''
ELSE [RegFinish]
END AS [RegFinish],
CASE
WHEN [SuppFinish] IS NULL THEN ''
ELSE [SuppFinish]
END AS [SuppFinish2],
FROM TABLE)
In order to reference aliased columns, you can use a derived table (or CTE, but that is not shown here)
Select *, CASE
WHEN [RegFinish]<[SuppFinish2] THEN '1'
ELSE '0'
END AS [TEST] From
(
SELECT
CASE
WHEN [RegFinish] IS NULL THEN ''
ELSE [RegFinish]
END AS [RegFinish],
CASE
WHEN [SuppFinish] IS NULL THEN ''
ELSE [SuppFinish]
END AS [SuppFinish2]
) T1
FROM TABLE
You cannot, at the same time, set and access an alias in the SELECT clause. I would suggest rewriting your query using CROSS APPLY:
SELECT t1.[RegFinish],
t2.[SuppFinish],
CASE
WHEN t1.[RegFinish] < t2.[SuppFinish] THEN '1'
ELSE '0'
END AS [TEST]
FROM TABLE
CROSS APPLY (SELECT COALESCE([RegFinish], '') AS [RegFinish]) AS t1
CROSS APPLY (SELECT COALESCE([SuppFinish], '') AS [SuppFinish]) AS t2
SELECT ISNULL([RegFinish],'') as [RegFinish]
, ISNULL([SuppFinish],'') as [SuppFinish2], CASE
WHEN
ISNULL([RegFinish],'') < ISNULL([SuppFinish],'') THEN 1
ELSE 0
END AS [TEST]
FROM TABLE
Why not use ISNULL instead of CASE? The problem with your query is that [SuppFinish2] is an alias not an column and can only be used in ORDER BY clause

in SQL SELECT statement :conditionally removing comma operator from field

I have a select statement like
SELECT 'Name' = customer_fname+ ', ' + customer_lname
FROM customers
Its output is like:
peter, willson
jenny, Mark
Now, if customer_fname is null, then output will be:
, willson
, Mark
If customer_lname is null then:
peter,
jenny,
And if both customer_fname and customer_lname are null then only the comma will be displayed.
I want to remove the comma. How do I do this?
Ordinarily I would suggest using the ISNULL operator. However, as you need to check on both fields, the logic becomes a bit nasty. Therefore, I would suggest using a CASE statement.
SELECT CASE
WHEN first_name IS NULL AND last_name IS NULL THEN ''
WHEN first_name IS NULL AND last_name IS NOT NULL THEN last_name
WHEN first_name IS NOT NULL AND last_name IS NULL THEN first_name
ELSE last_name + ', ' + first_name
END
FROM customers
EDIT For dknaack - an ISNULL solution :)
SELECT ISNULL(last_name + ', ' + first_name,
ISNULL(last_name,
ISNULL(first_name, '')))
FROM Customers
You should use operator CASE and function ISNULL.
Please use below query: thanks
SELECT 'Name' =
(CASE WHEN customer_fname IS NULL OR customer_lname IS NULL THEN (customer_fname + ' ' + customer_lname )
ELSE customer_fname+ ', ' + customer_lname END)
FROM customers
You can use case function in SQL Server:
SELECT 'Name' = customer_fname+ case when customer_fname is not null then ',' + customer_lname FROM customers
careful with NULL values with [+] operator :
unless you set the database parameter CONCAT_NULL_YIELDS_NULL to OFF,
NULL+'any non null string' returns NULL, not empty string
SELECT ISNULL(customer_fname,'')+ ISNULL(', '+ customer_lname,'') [Name]
FROM customers

T-SQL: Displaying data from multiple columns into one output column using CASE and COALESCE

I have the following SQL code that runs against a Change Request database. Each record has several columns that represent affected US regions. If the change affects a region the value will be 1 otherwise NULL.
So I am adding the values in each column to determine if more than one region is affected, the answer will be greater than 1.
I need help in how to make a change to my COALESCE and/or CASE statements to list all affected areas that have a value of 1, in the "Affected_Area" output.
Affected Regions Last Seven Days
SELECT ID,
(ISNULL(southeast,0) + ISNULL(allregions,0) + ISNULL(midamerica,0) + ISNULL(northcentral,0) + ISNULL(northeast,0) + ISNULL(pacificnorthwest,0) + ISNULL(pacificsouthwest,0)),
Affected_Area = COALESCE(
CASE WHEN [allregions]=1 THEN 'All Regions' ELSE NULL END,
CASE WHEN [midamerica]=1 THEN 'Mid-America' ELSE NULL END,
CASE WHEN [northcentral]=1 THEN 'North Central' ELSE NULL END,
CASE WHEN [northeast]=1 THEN 'Northeast' ELSE NULL END,
CASE WHEN [pacificnorthwest]=1 THEN 'Pacific Northwest' ELSE NULL END,
CASE WHEN [pacificsouthwest]=1 THEN 'Pacific Southwest' ELSE NULL END,
CASE WHEN [southeast]=1 THEN 'Southeast' ELSE NULL END
),
FROM [DB_Reporting].[dbo].change c with (nolock)
WHERE convert(varchar(10),([needed_by_date]),110) BETWEEN (DATEADD(DD,-7,CONVERT(VARCHAR(10),GetDATE(),110))) AND (DATEADD(DD,-1,CONVERT(VARCHAR(10),GetDATE(),110)))
Any help is appreciated!
Hank Stallings
Try something like this:
SELECT ID,
(ISNULL(southeast,0) + ISNULL(allregions,0) + ISNULL(midamerica,0) + ISNULL(northcentral,0) + ISNULL(northeast,0) + ISNULL(pacificnorthwest,0) + ISNULL(pacificsouthwest,0)),
Affected_Area =
CASE WHEN [allregions]=1 THEN 'All Regions, ' ELSE '' END +
CASE WHEN [midamerica]=1 THEN 'Mid-America, ' ELSE '' END +
CASE WHEN [northcentral]=1 THEN 'North Central, ' ELSE '' END +
CASE WHEN [northeast]=1 THEN 'Northeast, ' ELSE '' END +
CASE WHEN [pacificnorthwest]=1 THEN 'Pacific Northwest, ' ELSE '' END +
CASE WHEN [pacificsouthwest]=1 THEN 'Pacific Southwest, ' ELSE '' END +
CASE WHEN [southeast]=1 THEN 'Southeast' ELSE '' END
FROM [DB_Reporting].[dbo].change c with (nolock)
WHERE convert(varchar(10),([needed_by_date]),110) BETWEEN (DATEADD(DD,-7,CONVERT(VARCHAR(10),GetDATE(),110))) AND (DATEADD(DD,-1,CONVERT(VARCHAR(10),GetDATE(),110)))
You'll likely need to trim a trailing comma.
Do you have any ability to change the design of this table? It seems less than correct - you'd be better with a region column with an FK off to a regions table. What will you do with the above if you have to add another region...
If you don't, you might want to use the PIVOT command and create a more normalised view that you can then use to query.

Resources