Return SQL value based on condition - sql

Trying to get my head around this but I am stumped. Can I use an IIf with a HAVING statement or a CASE?
My current SQL is this:
SELECT dbo_forecast.mtl_part_part_nbr,
SUM(Round(dbo_forecast.forecast_qty)) AS FCST,
dbo_m8stacd_standards.whse_nbr,
dbo_m8stacd_standards.statn_cd,
dbo_m8stacd_standards.prty
INTO tblforecast77
FROM dbo_m8stacd_standards
INNER JOIN dbo_forecast
ON dbo_m8stacd_standards.part_nbr =
dbo_forecast.mtl_part_part_nbr
WHERE ( ( ( dbo_forecast.type ) = 11
OR ( dbo_forecast.type ) = 12 )
AND ( ( dbo_forecast.forecast_month ) = '01'
OR ( dbo_forecast.forecast_month ) = '02'
OR ( dbo_forecast.forecast_month ) = '03'
OR ( dbo_forecast.forecast_month ) = '04'
OR ( dbo_forecast.forecast_month ) = '05'
OR ( dbo_forecast.forecast_month ) = '06'
OR ( dbo_forecast.forecast_month ) = '07'
OR ( dbo_forecast.forecast_month ) = '08'
OR ( dbo_forecast.forecast_month ) = '09'
OR ( dbo_forecast.forecast_month ) = '10'
OR ( dbo_forecast.forecast_month ) = '11'
OR ( dbo_forecast.forecast_month ) = '12' ) )
GROUP BY dbo_forecast.mtl_part_part_nbr,
dbo_m8stacd_standards.whse_nbr,
dbo_m8stacd_standards.statn_cd,
dbo_m8stacd_standards.prty
HAVING ( ( ( dbo_forecast.mtl_part_part_nbr ) = "00104918" )
AND ( ( dbo_m8stacd_standards.whse_nbr ) = 77 )
AND ( ( dbo_m8stacd_standards.prty ) = "01"
OR ( dbo_m8stacd_standards.prty ) = "d" )
AND ( ( SUM(Round([dbo_forecast].[forecast_qty])) ) > 0 ) );
And returns this:
What I want to return is if there is a "D" in prty (for that part number) return the FCST for that, if there is not a prty (for that part number) D return the FCST for prty = "01"
edit: Sorry I should have worded this better. What I want is return one record. The prty=d record if it exists, otherwise the 01 record.

Related

Union Query Getting error on sales summary

Here is my code. Two CTES With a subquery and im trying to get the 2ns cte to give me totals like a pivot table.
WITH
SalesSummary AS
(
SELECT
Employee AS RepID,
Type,
SUM( ForeignTotal ) AS ForeignTotal,
SUM( projectedTotal*probability ) AS ProjectedTotal
FROM
Transaction
WHERE
( ( Type = 'SalesOrd' ) AND ( BUILTIN.CF( Status ) IN ('SalesOrd:B','SalesOrd:A') AND TO_CHAR(shipdate, 'mm') = TO_CHAR(sysdate, 'mm')
AND TO_CHAR(shipdate, 'yyyy') = TO_CHAR(sysdate, 'yyyy') ) )
OR ( ( Type = 'Opprtnty' ) AND ( BUILTIN.CF( Status ) IN ('Opprtnty:A','Opprtnty:C') AND TO_CHAR(expectedclosedate, 'mm') = TO_CHAR(sysdate, 'mm')
AND TO_CHAR(expectedclosedate, 'yyyy') = TO_CHAR(sysdate, 'yyyy') ) )
OR ( ( Type = 'CustInvc' ) AND ( BUILTIN.CF( Status ) IN ('CustInvc:A','CustInvc:B') AND TO_CHAR(trandate, 'mm') = TO_CHAR(sysdate, 'mm')
AND TO_CHAR(trandate, 'yyyy') = TO_CHAR(sysdate, 'yyyy') ) )
OR ( ( Type = 'CashSale' ) AND ( BUILTIN.CF( Status ) IN ('CashSale:C','CashSale:B') AND TO_CHAR(trandate, 'mm') = TO_CHAR(sysdate, 'mm')
AND TO_CHAR(trandate, 'yyyy') = TO_CHAR(sysdate, 'yyyy') ) )
OR ( ( Type = 'CustCred' ) AND ( BUILTIN.CF( Status ) IN ('CustCred:B','CustCred:A') AND TO_CHAR(trandate, 'mm') = TO_CHAR(sysdate, 'mm')
AND TO_CHAR(trandate, 'yyyy') = TO_CHAR(sysdate, 'yyyy') ) )
GROUP BY
Employee,
Type
)
,
Totals AS
(
SELECT
NULL AS RepName,
SUM ( CASE WHEN Type = 'Opprtnty' THEN ProjectedTotal ELSE 0 END ) AS Opportunity_Total,
SUM ( CASE WHEN Type = 'SalesOrd' THEN ForeignTotal ELSE 0 END ) AS Sales_Pending_Total,
SUM ( CASE WHEN Type = 'CustInvc' THEN ForeignTotal ELSE 0 END ) AS Invoiced,
SUM ( CASE WHEN Type = 'CashSale' THEN ForeignTotal ELSE 0 END ) AS Cash_Sale,
SUM ( CASE WHEN Type = 'CustCred' THEN ForeignTotal ELSE 0 END ) AS Credit_Memo,
NULL AS forecast
FROM
Transaction
WHERE
( ( Type = 'SalesOrd' ) AND ( BUILTIN.CF( Status ) IN ('SalesOrd:B','SalesOrd:A') AND TO_CHAR(shipdate, 'mm') = TO_CHAR(sysdate, 'mm')
AND TO_CHAR(shipdate, 'yyyy') = TO_CHAR(sysdate, 'yyyy') ) )
OR ( ( Type = 'Opprtnty' ) AND ( BUILTIN.CF( Status ) IN ('Opprtnty:A','Opprtnty:C') AND TO_CHAR(expectedclosedate, 'mm') = TO_CHAR(sysdate, 'mm')
AND TO_CHAR(expectedclosedate, 'yyyy') = TO_CHAR(sysdate, 'yyyy') ) )
OR ( ( Type = 'CustInvc' ) AND ( BUILTIN.CF( Status ) IN ('CustInvc:A','CustInvc:B') AND TO_CHAR(trandate, 'mm') = TO_CHAR(sysdate, 'mm')
AND TO_CHAR(trandate, 'yyyy') = TO_CHAR(sysdate, 'yyyy') ) )
OR ( ( Type = 'CashSale' ) AND ( BUILTIN.CF( Status ) IN ('CashSale:C','CashSale:B') AND TO_CHAR(trandate, 'mm') = TO_CHAR(sysdate, 'mm')
AND TO_CHAR(trandate, 'yyyy') = TO_CHAR(sysdate, 'yyyy') ) )
OR ( ( Type = 'CustCred' ) AND ( BUILTIN.CF( Status ) IN ('CustCred:B','CustCred:A') AND TO_CHAR(trandate, 'mm') = TO_CHAR(sysdate, 'mm')
AND TO_CHAR(trandate, 'yyyy') = TO_CHAR(sysdate, 'yyyy') ) )
)
SELECT
a.RepName,
a.Opportunity_Total,
a.Sales_Pending_Total,
a.Invoiced,
a.Cash_Sale,
a.Credit_Memo,
case when (a.Sales_Pending_Total + a.Invoiced + a.Cash_Sale + a.Credit_Memo) < Opportunity_Total
then Opportunity_Total*-1
else (a.Sales_Pending_Total + a.Invoiced + a.Cash_Sale + a.Credit_Memo) END as forecast
from(
SELECT
RepID,
( Employee.FirstName || ', ' || Employee.LastName ) AS RepName,
SUM ( CASE WHEN Type = 'Opprtnty' THEN ProjectedTotal ELSE 0 END ) AS Opportunity_Total,
SUM ( CASE WHEN Type = 'SalesOrd' THEN ForeignTotal ELSE 0 END ) AS Sales_Pending_Total,
SUM ( CASE WHEN Type = 'CustInvc' THEN ForeignTotal ELSE 0 END ) AS Invoiced,
SUM ( CASE WHEN Type = 'CashSale' THEN ForeignTotal ELSE 0 END ) AS Cash_Sale,
SUM ( CASE WHEN Type = 'CustCred' THEN ForeignTotal ELSE 0 END ) AS Credit_Memo,
FROM
SalesSummary
INNER JOIN Employee ON
( Employee.ID = SalesSummary.RepID )
WHERE
RepID IN ('17','24','-5','26')
GROUP BY
RepID,
( Employee.FirstName || ', ' || Employee.LastName )
ORDER BY
RepName
) a
UNION
SELECT
RepName,
Opportunity_Total,
Sales_Pending_Total,
Invoiced,
Cash_Sale,
Credit_Memo,
forecast
FROM
Totals

Stuff with xml is taking so much time in my query

I have a view from the view need to fetch some result set for that purpose I have wrote following query.
So here the problem is stuff with xml is taking so much time any suggestions please.
;WITH CTE_transaction_type1 AS
(
SELECT case_summary_id,Primary_Payer, Physician,Generate_Bill,
CASE
WHEN Current_Payer IS NULL
THEN CASE
WHEN Resposible_Patient_Name IS NULL
AND PG_Patient_Name IS NULL
THEN 'PG:' + Patient_Last_Name + ',' + Patient_First_Name
WHEN Resposible_Patient_Name IS NULL
AND NULLIF(PG_Patient_Name,' ') IS NOT NULL
THEN 'SG:' + Patient_Last_Name + ',' + Patient_First_Name
ELSE Resposible_Patient_Name
END
ELSE
CASE
WHEN Current_Payer_Name IS NULL
THEN ''
ELSE Insurance_Type + Current_Payer_Name + IIF(Plan_Name IS NULL, '', ',' + Plan_Name)
END
END AS Responsible_Party_List
FROM [charts].[vw_billing_analysis]
WHERE sort_order = 0 AND transaction_type_id = 1
)
SELECT ba.Case_Summary_ID
,ba.date_of_surgery
,ba.Organization_ID
,ba.Organization_Name
,ba.Patient_Name
,ba.Patient_First_Name
,ba.Patient_Last_Name
,ba.MRN
,ba.Physician_ID
,ba.Physician
,ba.[PROCEDURE]
,ba.Current_Payer
,ba.Current_Payer_Name
,ba.Primary_Payer
,ba.Primary_Payer_ID
,ba.Insurance_Plan_ID
,ba.Insurance_Plan_Type
,ba.Plan_Name
,ba.Speciality_ID
,ba.Speciality_Name
,ba.Transaction_Responsible_Party_ID
,ba.Charge_ID
,ba.Charge_Procedure_ID
,ba.Patient_Insurance_ID
,ba.Insurance_Type
,ba.Resposible_Patient_Name
,ba.Received_From
,ba.Guarantor_Master_ID
,ba.PG_Patient_Name
,ba.Batch_Description
,ba.Period_Name
,ba.Unassigned_payment
,ba.Charge
,ba.Charge_Corr
,ba.Debit
,ba.Payments
,ba.WriteOff
,ba.Balance
,ba.Corrected_TF
,ba.Period_ID
,ba.Batch_ID
,ba.transaction_type_id
,STUFF((
SELECT '; ' + [Procedure]
FROM [charts].[vw_billing_analysis] vba
WHERE vba.case_summary_id = ba.case_summary_id
AND vba.transaction_type_id = 1
ORDER BY ba.sort_order
FOR XML PATH('')
,TYPE
).value('.', 'NVARCHAR(MAX)'), 1, 2, '') AS Procedure_List
,trty1.Primary_Payer AS Primary_Payer_List
,trty1.Responsible_Party_List
,trty1.Physician AS Physician_List
,ba.Allowed_Amount
,IIF(ba.insurance_contract_id IS NULL
OR ba.Insurance_Plan_Type = 'Self-Pay', NULL, iif(ba.procedure_type IS NOT NULL, round(ba.[allowed_amount] - (ba.[allowed_amount] * ba.procedure_discount / 100.0), 3), ba.[procedure_amount])) AS [Expected_Amount]
,ba.Procedure_Amount
,ba.Generate_Bill
,trty1.Generate_Bill AS Generate_Bill_Case
,ba.Actual_Payment
,ba.First_Bill_Date
FROM [charts].[vw_billing_analysis] ba
left join cte_transaction_type1 trty1
ON ba.Case_Summary_ID=trty1.Case_Summary_ID
WHERE ba.Organization_ID =52
AND
(
(
(
(
ba.Period_Id = ('999999999')
OR '999999999' = '999999999'
)
AND 0 = 1
)
AND (
(
ba.Batch_Id IN ('999999999')
OR '999999999' = '999999999'
)
AND 0 = 1
)
)
OR (
0 = 0
AND ba.date_of_surgery >= '2018-02-01'
AND ba.date_of_surgery <= '2021-03-03'
)
)
AND (
CASE
WHEN ba.Insurance_Plan_Type IS NULL
AND ba.Insurance_Plan_ID IS NULL
AND ba.Current_Payer IS NULL
THEN '-1'
ELSE ba.Insurance_Plan_Type_Id
END IN ('999999999')
OR '999999999' = '999999999'
)
AND (
ba.Current_Payer IN ('999999999')
OR '999999999' = '999999999'
)
AND (
ba.Speciality_ID IN ('999999999')
OR '999999999' = '999999999'
)
AND (
ba.Physician_ID IN ('999999999')
OR '999999999' = '999999999'
)
AND (
isnull(ba.Primary_Payer_ID, - 1) IN ('999999999')
OR '999999999' = '999999999'
)
AND (
ba.appointment_type_id IN ('999999999')
OR '999999999' = '999999999'
)
AND (
SELECT SUM(CASE
WHEN vba.transaction_type_id = 1
THEN balance
WHEN vba.transaction_type_id = 3
THEN - balance
END)
FROM [charts].[vw_billing_analysis] vba
WHERE vba.case_summary_id = ba.case_summary_id
) >= 0
ORDER BY ba.Date_Of_Surgery DESC
,ba.MRN DESC
,ba.Patient_Name ASC
,ba.upd_tr_sort ASC
,ba.Sort_Order ASC

Is it possible to Pivot this query?

My query
SELECT DISTINCT dbo.tblacc_cv.cv_docno,
dbo.tblacc_cv.cv_docdate,
dbo.tblacc_cv.cv_suppliername,
dbo.tblacc_cv.cv_extdocno,
dbo.tblacc_chartaccount.coa_cvtype AS cvtype,
dbo.tblacc_glentry.gl_accountid,
( CASE
WHEN dbo.tblacc_glentry.gl_accountid = '201001' THEN Sum(
dbo.tblacc_glentry.gl_credit)
ELSE NULL
END ) AS AP,
( CASE
WHEN dbo.tblacc_glentry.gl_accountid = '208000' THEN Sum(
dbo.tblacc_glentry.gl_amount)
ELSE NULL
END ) AS EWT,
( CASE
WHEN dbo.tblacc_glentry.gl_accountid = '616000'
AND dbo.tblacc_chartaccount.coa_cvtype = 'S' THEN Sum(
dbo.tblacc_glentry.gl_debit)
ELSE NULL
END ) AS FABpur,
( CASE
WHEN dbo.tblacc_glentry.gl_accountid = '603000'
AND dbo.tblacc_chartaccount.coa_cvtype = 'S' THEN Sum(
dbo.tblacc_glentry.gl_debit)
ELSE NULL
END ) AS FABcre,
( CASE
WHEN dbo.tblacc_glentry.gl_accountid = '602000'
AND dbo.tblacc_chartaccount.coa_cvtype = 'S' THEN Sum(
dbo.tblacc_glentry.gl_debit)
ELSE NULL
END ) AS RawMat,
( CASE
WHEN dbo.tblacc_glentry.gl_accountid = '601003'
AND dbo.tblacc_chartaccount.coa_cvtype = 'S' THEN Sum(
dbo.tblacc_glentry.gl_debit)
ELSE NULL
END ) AS PurCre,
( CASE
WHEN dbo.tblacc_glentry.gl_accountid NOT IN (
'201001', '208000', '616000', '603000',
'602000', '601003' ) THEN
dbo.tblacc_glentry.gl_accountname
ELSE NULL
END ) AS GL_AccountName,
( CASE
WHEN dbo.tblacc_glentry.gl_accountid NOT IN (
'616000', '603000', '602000', '601003'
)
AND dbo.tblacc_chartaccount.coa_cvtype = 'M' THEN Sum(
dbo.tblacc_glentry.gl_debit)
ELSE NULL
END ) AS MOdr,
( CASE
WHEN dbo.tblacc_glentry.gl_accountid NOT IN (
'616000', '603000', '602000', '601003'
)
AND dbo.tblacc_chartaccount.coa_cvtype = 'F' THEN Sum(
dbo.tblacc_glentry.gl_debit)
ELSE NULL
END ) AS FDR,
( CASE
WHEN dbo.tblacc_glentry.gl_accountid NOT IN (
'201001', '208000', '616000', '603000',
'602000', '601003' )
AND dbo.tblacc_chartaccount.coa_cvtype = 'S' THEN Sum(
dbo.tblacc_glentry.gl_debit)
ELSE NULL
END ) AS SunDR,
( CASE
WHEN dbo.tblacc_glentry.gl_accountid NOT IN (
'201001', '208000', '616000', '603000',
'602000', '601003' )
AND dbo.tblacc_chartaccount.coa_cvtype = 'S' THEN Sum(
dbo.tblacc_glentry.gl_credit)
ELSE NULL
END ) AS SunCR
FROM dbo.tblacc_glentry
INNER JOIN dbo.tblacc_apv
ON dbo.tblacc_glentry.gl_docno = dbo.tblacc_apv.apv_docno
INNER JOIN dbo.tblacc_cv
INNER JOIN dbo.tblacc_cvdetail
ON dbo.tblacc_cv.cv_docno =
dbo.tblacc_cvdetail.cvd_cv_docno
ON dbo.tblacc_apv.apv_docno = dbo.tblacc_cvdetail.cvd_refdocno
INNER JOIN dbo.tblacc_chartaccount
ON dbo.tblacc_glentry.gl_accountid =
dbo.tblacc_chartaccount.coa_account_id
WHERE ( dbo.tblacc_cv.cv_docdate BETWEEN
'01/01/2018 00:00:00' AND '1/30/2018 23:59:59' )
GROUP BY dbo.tblacc_cv.cv_docno,
dbo.tblacc_cv.cv_docdate,
dbo.tblacc_cv.cv_suppliername,
dbo.tblacc_glentry.gl_accountname,
dbo.tblacc_cv.cv_extdocno,
dbo.tblacc_chartaccount.coa_cvtype,
dbo.tblacc_glentry.gl_accountid,
dbo.tblacc_glentry.gl_credit,
dbo.tblacc_glentry.gl_amount,
dbo.tblacc_glentry.gl_debit
ORDER BY dbo.tblacc_cv.cv_docdate ASC
Sample Output
Desired Ouput
Your desired output is not an example of a PIVOT.
Lacking a detailed explanation of your logic, I can tell you that you can get your desired output by GROUPing BY the first four columns, and applying MAX() to all the others. (Except cvtype which would be a MIN).

Cannot perform an aggregate function on an expression containing an aggregate or a subquery when taking SUM of a Column returned by CASE Statement

I am having a problem with the following SQL query. While executing, it gives error:
Msg 130, Level 15, State 1, Line 2
Cannot perform an aggregate function on an expression containing an aggregate or a subquery.
SELECT
sum(case when DESP_SCANNING.PALLET_NO != null then (select PALLETNO_MASTER.PALLET_WEIGHT from PALLETNO_MASTER where DESP_SCANNING.PALLET_NO= PALLETNO_MASTER.PALLET_NO and DESP_SCANNING.REGION_ID = PALLETNO_MASTER.REGION_ID) else (select CARTONNO_MASTER.CARTON_WEIGHT from CARTONNO_MASTER where DESP_SCANNING.CARTON_NO = CARTONNO_MASTER.CARTON_NO and DESP_SCANNING.REGION_ID = CARTONNO_MASTER.REGION_ID) end) as totweight
FROM CUSTOMER_MASTER, DESP_SCANNING, CARRIER_MASTER, DC_MASTER
WHERE CARRIER_MASTER.CARRIER_CODE = DESP_SCANNING.CARRIER_CODE AND
((DESP_SCANNING.PALLET_NO != null and DESP_SCANNING.PALLET_NO = (select PALLETNO_MASTER.PALLET_NO from PALLETNO_MASTER where DESP_SCANNING.REGION_ID = PALLETNO_MASTER.REGION_ID)) or(DESP_SCANNING.PALLET_NO = null and DESP_SCANNING.CARTON_NO = (select CARTONNO_MASTER.CARTON_NO from CARTONNO_MASTER where DESP_SCANNING.REGION_ID = CARTONNO_MASTER.REGION_ID))) AND
DESP_SCANNING.STATUS = 2
group by DESP_SCANNING.DC_NO, DESP_SCANNING.POD_DATE, CUSTOMER_MASTER.CUS_NAME, CARRIER_MASTER.CARRIER_NAME, DESP_SCANNING.AWB_NO,DC_MASTER.DESP_END_DATE_TIME, DESP_SCANNING.SCAN_DATE, DC_MASTER.PSR_LOAD_DATE_TIME
order by CARRIER_MASTER.CARRIER_NAME, DESP_SCANNING.POD_DATE
Error shows in this line:
sum(case when DESP_SCANNING.PALLET_NO != null then (select PALLETNO_MASTER. PALLET_WEIGHT from PALLETNO_MASTER where DESP_SCANNING.PALLET_NO= PALLETNO_MASTER.PALLET_NO and DESP_SCANNING.REGION_ID = PALLETNO_MASTER.REGION_ID) else (select CARTONNO_MASTER.CARTON_WEIGHT from CARTONNO_MASTER where DESP_SCANNING.CARTON_NO = CARTONNO_MASTER.CARTON_NO and DESP_SCANNING.REGION_ID = CARTONNO_MASTER.REGION_ID) end) as totweight,
So, what I can give instead of this.
It looks like you need to factor out the CASE ... construct (which has the subquery's that cause the error message) into the WHERE clause.
That is, first construct a single table that contains PALLET_WEIGHT or CARTON_WEIGHT as appropriate, in a single column, and do the SUM over that column. Genre SELECT SUM(x) FROM (your big complicated SELECT here).
(I'm not guaranteeing that this will be possible. It's just what I would try to do having read what the error message tells me. Bigger product experts than me could possibly tell you my idea won't work either.)
maybe this code will help you:
select sum(x.totweight) as totweight
from (
select
CARRIER_MASTER.CARRIER_NAME,
DESP_SCANNING.POD_DATE,
case when DESP_SCANNING.PALLET_NO != null then(
select PALLETNO_MASTER.PALLET_WEIGHT
from PALLETNO_MASTER
where DESP_SCANNING.PALLET_NO = PALLETNO_MASTER.PALLET_NO
and DESP_SCANNING.REGION_ID = PALLETNO_MASTER.REGION_ID
)
else(
select CARTONNO_MASTER.CARTON_WEIGHT
from CARTONNO_MASTER
where DESP_SCANNING.CARTON_NO = CARTONNO_MASTER.CARTON_NO
and DESP_SCANNING.REGION_ID = CARTONNO_MASTER.REGION_ID
)
end as totweight
from CUSTOMER_MASTER,
DESP_SCANNING,
CARRIER_MASTER,
DC_MASTER
where CARRIER_MASTER.CARRIER_CODE = DESP_SCANNING.CARRIER_CODE
and (
(
DESP_SCANNING.PALLET_NO != null
and DESP_SCANNING.PALLET_NO = (
select PALLETNO_MASTER.PALLET_NO
from PALLETNO_MASTER
where DESP_SCANNING.REGION_ID = PALLETNO_MASTER.REGION_ID
)
)
or (
DESP_SCANNING.PALLET_NO = null
and DESP_SCANNING.CARTON_NO = (
select CARTONNO_MASTER.CARTON_NO
from CARTONNO_MASTER
where DESP_SCANNING.REGION_ID = CARTONNO_MASTER.REGION_ID
)
)
)
and DESP_SCANNING.status = 2
group by DESP_SCANNING.DC_NO,
DESP_SCANNING.POD_DATE,
CUSTOMER_MASTER.CUS_NAME,
CARRIER_MASTER.CARRIER_NAME,
DESP_SCANNING.AWB_NO,
DC_MASTER.DESP_END_DATE_TIME,
DESP_SCANNING.SCAN_DATE,
DC_MASTER.PSR_LOAD_DATE_TIME
) x
order by x.CARRIER_NAME, x.POD_DATE

Oracle: Listagg

I'm new to SQL and I'm trying to group a set of remarks that is spread over several rows. Specifically I want to aggregate the RMK_TEXT column and order by RMK_LINENO.
Can anyone help me out?
SELECT
ALL_AGREEMENTS.AGMT_NUM,
PROVISION_TYPES.STIP_TYPE_DESC,
GENERAL_REMARK_TEXT.RMK_LINENO,
LISTAGG(GENERAL_REMARK_TEXT.RMK_TEXT) WITHIN GROUP (ORDER BY GENERAL_REMARK_TEXT.RMK_LINENO) RMK_TEXT
FROM
STIPULATION_PROVISIONS,
ALL_AGREEMENTS,
STIPULATION_TYPES PROVISION_TYPES,
GENERAL_REMARK_TEXT,
GENERAL_REMARKS
WHERE
( ALL_AGREEMENTS.ARRG_KEY=STIPULATION_PROVISIONS.ARRG_KEY(+) )
AND ( STIPULATION_PROVISIONS.STIP_TYPE_CODE=PROVISION_TYPES.STIP_TYPE_CODE(+) )
AND ( PROVISION_TYPES.STIP_CATG_CODE = 'PRV' )
AND ( GENERAL_REMARKS.GEN_RMK_FK(+)=STIPULATION_PROVISIONS.STIP_KEY AND GENERAL_REMARKS.RMK_TYPE_CATG(+) = 'PRV' )
AND ( GENERAL_REMARKS.RMK_KEY = GENERAL_REMARK_TEXT.RMK_KEY(+) )
AND ( GENERAL_REMARKS.RMK_TYPE_CATG (+) = 'PRV' )
AND ( ( ALL_AGREEMENTS.ARRG_ORG_KEY IN
(SELECT ARRG_ORG_KEY
FROM BO_USER_DATA_PRIVS
WHERE OBJECT_TYPE_CD = 'area'
AND SEC_USER_ID = UPPER('user'))
AND ALL_AGREEMENTS.SUBJ_CODE IN
(SELECT SUBJ_CODE
FROM BO_USER_DATA_PRIVS
WHERE OBJECT_TYPE_CD = 'SUBJ'
AND SEC_USER_ID = UPPER('user')) ) )
AND
(
(
ALL_AGREEMENTS.AGMT_NUM IN ( '00003000','00004000','00005001','00005002','00005003','00006000','00007000','00008000','00009000','00010000','00021000','00022000','00023000','00024000','00025000','00026000' )
)
AND
PROVISION_TYPES.STIP_TYPE_DESC IN ( 'provision' )
AND
STIPULATION_PROVISIONS.EXST_FLAG IN ( 'Y' )
)
GROUP BY ALL_AGREEMENTS.AGMT_NUM
Something like this:
SELECT
a.AGMT_NUM,
p.STIP_TYPE_DESC,
s.EXST_FLAG,
( SELECT LISTAGG( t.RMK_TEXT, CHR(13) || CHR(10) )
WITHIN GROUP ( ORDER BY t.RMK_LINENO )
FROM GENERAL_REMARKS r
INNER JOIN GENERAL_REMARK_TEXT t
ON ( r.RMK_KEY = t.RMK_KEY)
WHERE r.GEN_RMK_FK = s.STIP_KEY
AND r.RMK_TYPE_CATG = 'PRV'
) AS Remarks
FROM
ALL_AGREEMENTS a
LEFT OUTER JOIN STIPULATION_PROVISIONS s
ON ( a.ARRG_KEY = s.ARRG_KEY )
LEFT OUTER JOIN STIPULATION_TYPES p
ON ( s.STIP_TYPE_CODE = p.STIP_TYPE_CODE )
WHERE
p.STIP_CATG_CODE = 'PRV'
AND EXISTS ( SELECT 1
FROM BO_USER_DATA_PRIVS b
WHERE OBJECT_TYPE_CD = 'LAND'
AND SEC_USER_ID = UPPER('user')
AND a.ARRG_ORG_KEY = b.ARRG_ORG_KEY
)
AND EXISTS ( SELECT 1
FROM BO_USER_DATA_PRIVS b
WHERE OBJECT_TYPE_CD = 'SUBJ'
AND SEC_USER_ID = UPPER('user')
AND a.SUBJ_CODE = b.SUBJ_CODE
)
AND a.AGMT_NUM IN ( 'OH00003000','OH00004000','OH00005001','OH00005002','OH00005003','OH00006000','OH00007000','OH00008000','OH00009000','OH00010000','OH00021000','OH00022000','OH00023000','OH00024000','OH00025000','OH00026000' )
AND p.STIP_TYPE_DESC = 'provision'
AND s.EXST_FLAG = 'Y';