Multiple Parameter condition using Crystal Reports - crystal-reports

I have the following in my selection expert:
(
if {?Job} <> "All" then
{SPSYSPRO_PO_CapexJobs;1.MJob} = {?Job}
else
true
) OR
(
if {?Supplier} <> "All" then
{SPSYSPRO_PO_CapexJobs;1.MJob}= {?Job}
else
true
)
However its not doing what I want ^^
What I require is if Job has not value selected then use the supplier's list to obtain data.
So if Job has no value and Supplier = multiple values it should bring me all those suppliers jobs, or vice versa...
If I select Job = All and choose multiple supplier's it should bring me back all jobs for just those suppliers selected and vice versa...
Hey just thought ill add my SP here, then you might understand what im looking for as your formula don't work in this regard.
SELECT SUBSTRING(PD.PurchaseOrder,9,7) PurchaseOrder
,PD.[Line]
,PD.[MStockCode]
,PD.[MStockDes]
,PD.[MOrderQty]
,PD.[MReceivedQty]
,SUBSTRING(RD.Supplier,9,7) Supplier
,AP.SupplierName
,PH.OrderEntryDate CreateDate
,PH.OrderStatus
,PD.[MLatestDueDate]
,PD.[MLastReceiptDat]
,PD.[MDiscValue]
,PD.[MPrice]
,PD.[MForeignPrice]
,PD.MJob
,PD.[MRequisition]
,PD.[MRequisitionLine]
FROM [SysproCompanyR].[dbo].[PorMasterDetail] PD
INNER JOIN [SysproCompanyR].[dbo].[PorMasterHdr] PH
on PD.PurchaseOrder = PH.PurchaseOrder
INNER JOIN [SysproCompanyR].[dbo].ReqDetail RD
on PD.MRequisition = RD.Requisition and PD.MRequisitionLine = RD.Line
INNER JOIN [SysproCompanyR].[dbo].ApSupplier AP
on RD.Supplier = AP.Supplier
Where SUBSTRING(PD.MJob,1,1)= 'C'
and PD.MCompleteFlag <> 'Y'
and PH.OrderStatus <> '*'

if you need only jobs for the suppliers that are selected then it would be better you use the sub report
In main report create a parameter for suppliers and jobs and pass suppliers parameter to sub report and link job parameter in main report to sub report
using sub report links and write the below formula in sub report record selection formula
For parameters create 2 default values All, None and your normal list
if {?Job} = "All" and ({?suppliers} <> "ALL" and {?suppliers} <> "none") then
{SPSYSPRO_PO_CapexJobs;1.MJob} = {?Job} and
{SPSYSPRO_PO_CapexJobs;1.Msuppliers} in ("list of comma saperated suppliers")
//For above case you need to have relation between jobs and suppliers
else if
(({?Job} = "All" or {?suppliers} = "none") and {?suppliers} <> "ALL")
then {SPSYSPRO_PO_CapexJobs;1.MJob} = {?Job}
else if
({?Job} <> "All" and ({?suppliers} = "ALL" or {?Job} = "none"))
then {SPSYSPRO_PO_CapexJobs;1.MJob}= {?Job}
if you don't want to use the sub report then retrieve all records and do the selection in crystal reports
if
(({?Job} = "All" or {?suppliers} = "none") and {?suppliers} <> "ALL")
then {SPSYSPRO_PO_CapexJobs;1.MJob} = {SPSYSPRO_PO_CapexJobs;1.MJob} else if
({?Job} <> "All" and ({?suppliers} = "ALL" or {?Job} = "none"))
then {SPSYSPRO_PO_CapexJobs;1.MJob}= {SPSYSPRO_PO_CapexJobs;1.Supplier}
Hope it helps

Related

Pass order by clause from Java in Jasper Report

I am using latest version of Jasper soft studio to design Jasper reports. I want to pass order by clause to jasper report based on certain criteria from Java. Can you please guide as how to append parameter(containing order by clause from Java) in the report query?
Query
with tmp as (
select d.gl_code, nvl(sum(d.debit),0)- nvl(sum(d.credit),0) as simple
from gl_forms g,gl_voucher_detail d,financial_year f
where g.voucher_no = d.voucher_no
and f.fid = $P{P_FID}
and g.voucher_date >= to_date('01-07-'+f.start_year,'dd-MM-yyyy') and g.voucher_date < $P{P_FROM}
and g.post=1
group by d.gl_code)
select g.voucher_no,
g.voucher_date,
g.voucher_type,
g.remarks remarks,
d.sr_no,
c.account_name,
d.debit,
d.credit,
d.narration narration,
c.account_code,
c.account_name || ' - ' || c.account_code Account,
(select name from company) name,
nvl((o.debit-o.credit),0) + nvl(tmp.simple,0) opening
from gl_voucher_detail d
join gl_forms g
on g.voucher_no = d.voucher_no
and g.post=1
and g.voucher_date between $P{P_FROM} and $P{P_TO}
left outer join opening_balances o
on d.gl_code = o.account_code
and o.fid = $P{P_FID}
right outer join chart_of_account c
on c.account_code = d.gl_code
left outer join tmp
on c.account_code = tmp.gl_code
where c.account_level=4
and c.active=1
and c.account_code = CASEWHEN($P{P_ACCOUNT}='%',c.account_code,$P{P_ACCOUNT})
Order by clause shall be one among these below two based on front fields selection:
order by c.account_name, g.voucher_date,g.voucher_no,d.sr_no
order by c.debit+c.credit desc

PostgreSQL subquery split rows (error is more than one row returned by a subquery used as an expression)

this is my first post on stackoverflow so please be gentle. I have researched this problem and came up with many varied solutions...all of which seem to be just off from what I need. I have a postgresql subquery in a SELECT statement that returns multiple rows, which I know is obviously not ideal/allowed/sensible/etc.... However, this is the case for my current problem and I need to be able to take those multiple returned rows and apply them to every previous corresponding record row that they originally came out of.
Current Query:
SELECT cohead_number "Sales Order#",
cohead_custponumber "Purchase Order#",
item_number "Part Number",
item_descrip1 "Part Description",
CAST(shipitem_qty AS integer) "Item Quantity",
getPacklistItemLotSerial(shiphead_id, coitem_id) AS "LotNumbers" --this is the duplicating row subquery that I need listed in separate rows without changing other respective columns--
FROM cohead
LEFT JOIN coitem
ON coitem_cohead_id = cohead_id
LEFT JOIN shipitem
ON coitem_id = shipitem_orderitem_id
LEFT JOIN itemsite
ON coitem_itemsite_id = itemsite_id
LEFT JOIN item
ON itemsite_item_id = item_id
LEFT JOIN shiphead
ON shiphead_order_id = cohead_id
WHERE cohead_number = '79464' --this is just to test with one sales order instead of all (sales order being the input for the query)--
Results:
LINK: Results of above query here
What I Have Tried
Now, this line lets me split the column results via the delimiter ',' but I can't figure out how to get the results from this back into my original query's results:
(SELECT lot from regexp_split_to_table(getPacklistItemLotSerial(shiphead_id, coitem_id),', ') AS lot)
Results:
Here I input the shiphead_id and coitem_id for the example sales order so it can show you the resulting split column into rows.
SELECT lot from regexp_split_to_table(getPacklistItemLotSerial(22082, 50351),', ') AS lot
LINK: Results of Example
Please help walk me through what I need to do to achieve this. I imaging we need to declare some things and maybe join 2 tables in a more complex query...I don't really know. Thank you for any help you can offer.
EDIT
Added in the requested source code for the Function "getpacklistitemlotserial"
DECLARE
pShipheadId ALIAS FOR $1;
pOrderItemId ALIAS FOR $2;
_lotserial text;
_r RECORD;
_first BOOLEAN;
BEGIN
--Test to see if Lot/Serial Enabled
SELECT metric_value INTO _lotserial
FROM metric
WHERE ((metric_name='LotSerialControl')
AND (metric_value ='t'));
IF (FOUND) THEN
_lotserial := '';
_first := true;
FOR _r IN SELECT DISTINCT ls_number
FROM invdetail, invhist, shipitem, ls
WHERE ((shipitem_shiphead_id=pShipheadId)
AND (shipitem_orderitem_id=pOrderItemId)
AND (shipitem_invhist_id=invhist_id)
AND (invhist_id=invdetail_invhist_id)
AND (invdetail_ls_id=ls_id)) LOOP
IF (_first = false) THEN
_lotserial := _lotserial || ', ';
END IF;
_lotserial := _lotserial || _r.ls_number;
_first := false;
END LOOP;
RETURN _lotserial;
ELSE
RETURN '';
END IF;
END
Try:
SELECT DISTINCT cohead_number "Sales Order#",
cohead_custponumber "Purchase Order#",
item_number "Part Number",
item_descrip1 "Part Description",
CAST(shipitem_qty AS integer) "Item Quantity",
-- getPacklistItemLotSerial(shiphead.shiphead_id, coitem.coitem_id) AS "LotNumbers" --this is the duplicating row subquery that I need listed in separate rows without changing other respective columns--
CASE WHEN EXISTS(
SELECT metric_value FROM metric
WHERE metric_name='LotSerialControl' AND metric_value ='t'
)
THEN x.ls_number
ELSE ''
END AS "LotNumbers"
FROM cohead
LEFT JOIN coitem
ON coitem_cohead_id = cohead_id
LEFT JOIN shipitem
ON coitem_id = shipitem_orderitem_id
LEFT JOIN itemsite
ON coitem_itemsite_id = itemsite_id
LEFT JOIN item
ON itemsite_item_id = item_id
LEFT JOIN shiphead
ON shiphead_order_id = cohead_id
LEFT JOIN (
SELECT ls_number,
shipitem_shiphead_id, -- parameter: pShipheadId
shipitem_orderitem_id -- parameter pOrderItemId
FROM invdetail, invhist, shipitem, ls
WHERE
-- (shipitem_shiphead_id=pShipheadId)
-- AND (shipitem_orderitem_id=pOrderItemId)
(shipitem_invhist_id=invhist_id)
AND (invhist_id=invdetail_invhist_id)
AND (invdetail_ls_id=ls_id)
) x
ON ( x.shipitem_shiphead_id = shiphead.shiphead_id
AND
x.shipitem_orderitem_id = coitem.coitem_id
)
WHERE cohead_number = '79464'

Compare results with percentage with PRINT

I've queried the results I'm looking for, now I simply want to compare "OnTime" Y/N for a percentage "OnTime", but I'm having trouble comparing the alias and/or using the column name in the PRINT function. Any suggestions?
SELECT
shmast.fcnumber AS Customer,
shmast.fcbcompany AS Company,
somast.fsono AS SalesOrder,
somast.fduedate AS DueDate,
shmast.fshipno AS Shipper,
shmast.fshipdate AS ShipDate,
--DATEDIFF(DAY,somast.fduedate,shmast.fshipdate) AS 'Days',
CASE
WHEN shmast.fshipdate <= somast.fduedate THEN 'Y'
WHEN shmast.fshipdate > somast.fduedate THEN 'N'
END AS OnTime
FROM somast
JOIN shmast ON somast.fsono = shmast.fcsono
WHERE shmast.fcnumber = '000111'
PRINT (OnTime = 'Y') / COUNT (*)
/*GROUP BY
shmast.fcnumber,
shmast.fcbcompany,
somast.fsono,
somast.fduedate,
shmast.fshipno,
shmast.fshipdate*/
Giving it another shot after your description
SELECT
sum(CASE WHEN shmast.fshipdate<=somast.fduedate THEN 100.0 END)/count(*) AS OnTime
FROM somast
JOIN shmast ON somast.fsono = shmast.fcsono
WHERE shmast.fcnumber = '000111'

Need some SQL Stored proc help - joining

So I have this section of my proc:
SELECT
com_contact.rc_name_full as CreatedBy,
capComponent.cm_strike as CapStrike,
floorComponent.cm_strike as FloorStrike,
tq_nominal_notional as Notional,
maxComponent.cm_effective_dt as EffectiveDate,
maxComponent.cm_maturity_dt as MaturityDate,
CAST(CAST(DATEDIFF(mm,maxComponent.cm_effective_dt,maxComponent.cm_maturity_dt) as decimal(9,2))/12 as decimal(9,2)) as term,
(
CASE WHEN se_amort_term_mnth IS NOT NULL THEN se_amort_term_mnth / 12
ELSE CAST(CAST(DATEDIFF(mm,
ISNULL(cmam_amortization_start_dt, maxComponent.cm_effective_dt),
cmam_amortization_end_dt) as decimal(9,2))/12 as decimal(9,2))
END
) AS AmortTermYears,
tq_dd_product as Product,
dh_key_rate as KeyRate,
dh_pv01 as PV01,
dh_val_time_stamp as RateTimeStamp,
re_bnk_le.re_company_name as Company,
rc_contact_id as UserId,
stp_name as NickName,
'' as project,
'' as Borrower,
'' as Lender,
'' as AdditionalInfo,
CASE WHEN tpm_pd_permission_id = 85 THEN 'LLH' WHEN tpm_pd_permission_id = 86 THEN 'ALM' ELSE '' END as Permission,
tr_transaction_id as TransactionId,
NULL as IndicationId
FROM cfo_transaction
The line that says '' as project, we have to actually change to return data now.
The table that next to the FROM, called cfo_transaction has an id on it called tr_transaction_id. We have another table called com_project_transaction_link, that links those id's with project id's, using two two columns called:
pt_tr_transaction_id and pt_pj_project_id, and then we have a table containing all the projects called com_project that has a pj_project_id and a pj_project_name.
GOAL: return the pj_project_name from that projects table where it links with the transactions being pulled.
I really don't know how to do this.
Thanks!
Try this:
SELECT
com_contact.rc_name_full as CreatedBy,
capComponent.cm_strike as CapStrike,
floorComponent.cm_strike as FloorStrike,
tq_nominal_notional as Notional,
maxComponent.cm_effective_dt as EffectiveDate,
maxComponent.cm_maturity_dt as MaturityDate,
CAST(CAST(DATEDIFF(mm,maxComponent.cm_effective_dt,maxComponent.cm_maturity_dt) as decimal(9,2))/12 as decimal(9,2)) as term,
(
CASE WHEN se_amort_term_mnth IS NOT NULL THEN se_amort_term_mnth / 12
ELSE CAST(CAST(DATEDIFF(mm,
ISNULL(cmam_amortization_start_dt, maxComponent.cm_effective_dt),
cmam_amortization_end_dt) as decimal(9,2))/12 as decimal(9,2))
END
) AS AmortTermYears,
tq_dd_product as Product,
dh_key_rate as KeyRate,
dh_pv01 as PV01,
dh_val_time_stamp as RateTimeStamp,
re_bnk_le.re_company_name as Company,
rc_contact_id as UserId,
stp_name as NickName,
PR.pj_project_name as project,
'' as Borrower,
'' as Lender,
'' as AdditionalInfo,
CASE WHEN tpm_pd_permission_id = 85 THEN 'LLH' WHEN tpm_pd_permission_id = 86 THEN 'ALM' ELSE '' END as Permission,
tr_transaction_id as TransactionId,
NULL as IndicationId
FROM cfo_transaction TR
INNER JOIN com_project_transaction_link TL
ON TR.tr_transaction_id = TL.pt_tr_transaction_id
INNER JOIN com_project PR
ON TL.pt_pj_project_id = PR.pj_project_id
The query above assumes that every transaction and project is on the table that joins your tables of projects and transactions (thus the INNER JOIN), but yo can change those to LEFT JOIN if you want
You just add a second join to the other table to the query.
select
yourfields,
p.pj_project_name as project
FROM cfo_transaction t
join com_project_transaction_link tl
on t.tr_transaction_id = tl.pt_tr_transaction_id
join com_project p
on tl.pt_pj_project_id = p.pj_project_id
SELECT ..., cp.pj_project_name
FROM cfo_transaction ct
INNER JOIN com_project_transaction_link cptl
ON ct.tr_transaction_id = cptl.pt_tr_transaction_id
INNER JOIN com_project cp
ON cptl.pt_pj_project_id = cp.pj_project_id

crytal report count total records

Im using query in crystal report like:
if({?User Name}) <>"ALL"
then
{COMN_USER_RESP_LINK_T.APPL_USER_NAME}={?User Name}
else
{COMN_USER_RESP_LINK_T.APPL_USER_NAME} ={COMN_USER_RESP_LINK_T.APPL_USER_NAME}
and
{COMN_USER_RESP_LINK_T.ENABLED}="Y"
this is actual query in sql:
SELECT
C.APPL_USER_NAME,
A.RESP_NAME,
B.MENU_NAME,
B.DESCRIPTION,
B.MODULE_NAME,
C.APPL_RESP
FROM COMN_RESPONSIBILITY_T A,
COMN_RESP_MENU_LINK_T B,
COMN_USER_RESP_LINK_T C
WHERE A.COMP_CODE = B.COMP_CODE
AND B.COMP_CODE = C.COMP_CODE
AND C.COMP_CODE = A.COMP_CODE
AND A.RESP_NAME = B.RESP
AND C.APPL_RESP = A.RESP_NAME
AND B.ENABLED = 'Y'
AND C.APPL_USER_NAME = c.APPL_USER_NAME
CASE #ACCT_CODE_FROM
WHEN 'ALL' THEN C.APPL_USER_NAME
ELSE #ACCT_CODE_FROM
END
ORDER BY APPL_USER_NAME,
RESP_NAME
I should select user name in parameter field(?user name), if I select ALL then I have to show all records ({COMN_USER_RESP_LINK_T.APPL_USER_NAME})
is the above query correct?
I would probably rephrase that as something like:
SELECT C.APPL_USER_NAME, A.RESP_NAME, B.MENU_NAME, B.DESCRIPTION,
B.MODULE_NAME, C.APPL_RESP
FROM COMN_RESPONSIBILITY_T A
INNER JOIN COMN_RESP_MENU_LINK_T B ON A.COMP_CODE = B.COMP_CODE
AND A.RESP_NAME = B.RESP
INNER JOIN COMN_USER_RESP_LINK_T C ON B.COMP_CODE = C.COMP_CODE
AND C.APPL_RESP = A.RESP_NAME
WHERE ((#ACCT_CODE_FROM = 'ALL') AND (B.ENABLED = 'Y'))
OR (C.APPL_USER_NAME = #ACCT_CODE_FROM)
ORDER BY APPL_USER_NAME, RESP_NAME
This gets you the details for either:
when ALL is selected, all accounts that are enabled; or
just the user code entered
i don't read your code because it is very dirty! but i think you can use SelectionFormula in crystal report. Certainly you can set it in code :
crystalReportViewer1.SelectionFormula ="(({?User Name}= 'ALL') AND ({B.ENABLED} = 'Y'))
OR ({C.APPL_USER_NAME} = #ACCT_CODE_FROM)"