Make sql query run faster - sql

Is it possible to make some optimization on such query ?
with t (CurrentPrice, [Weight])
as ( select CurrentPrice, [Weight] from SomeTable where GroupingId = SomeBigIntId )
SELECT MaxExactPrice = ( select MAX(CurrentPrice) FROM t Where [Weight] > 0 ),
MinExactPrice = ( select MIN(CurrentPrice) FROM t Where [Weight] > 0 ),
MaxSimilarPrice = ( select MAX(CurrentPrice) FROM t Where [Weight] = 0 ),
MinSimilarPrice = ( select MIN(CurrentPrice) FROM t Where [Weight] = 0 ),
ExactCount = ( select Count(*) FROM t Where [Weight] > 0 ),
SimilarCount = ( select Count(*) FROM t Where [Weight] = 0 ),
Count(*) as TotalCount
FROM t
Thank you.

Do this instead. this way you do not have to hit same table 8 different times.
select MAX(CASE WHEN [Weight] > 0 THEN CurrentPrice ELSE NULL END) AS MaxExactPrice
,MIN(CASE WHEN [Weight] > 0 THEN CurrentPrice ELSE NULL END) AS MinExactPrice
,MAX(CASE WHEN [Weight] = 0 THEN CurrentPrice ELSE NULL END) AS MaxSimilarPrice
,MIN(CASE WHEN [Weight] = 0 THEN CurrentPrice ELSE NULL END) AS MinSimilarPrice
,COUNT(CASE WHEN [Weight] > 0 THEN 1 ELSE NULL END ) AS ExactCount
,COUNT(CASE WHEN [Weight] = 0 THEN 1 ELSE NULL END ) AS SimilarCount
,COUNT(*)AS TotalCount
from SomeTable where GroupingId = SomeBigIntId

Related

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

Postgres Error: More than one row returned by a subquery used as an expression. 21000

I'm trying to insert distinct rows into a table however, I'm receiving the error in the title. What I don't understand is that I have set the LIMIT to 1 as I assumed this would solve the issue.
Can anyone please shed some light on this?
ERROR: more than one row returned by a subquery used as an expression.
INSERT INTO reports.sem_attribution_v2
SELECT DISTINCT
tsa.user_id,
tsa.transfer_date,
tsa.engine,
tsa.campaign_id,
tsa.adgroup_id,
(CASE
WHEN tsa.engine = 'quora' THEN NULL
WHEN tsa.source ILIKE '%uac%' THEN (
SELECT
account
FROM reports.lookup_uac_elements
WHERE campaign_id = tsa.campaign_id
LIMIT 1)
WHEN tsa.engine = 'google' THEN (
SELECT
pse.account_name
FROM reports.lookup_paid_search_elements pse
WHERE pse.campaign_id = tsa.campaign_id
AND pse.adgroup_id = tsa.adgroup_id
LIMIT 1)
WHEN tsa.engine = 'bing' THEN (
SELECT
lbe.account_name
FROM reports.lookup_bing_elements lbe
WHERE lbe.campaign_id = tsa.campaign_id
AND lbe.adgroup_id = tsa.adgroup_id
LIMIT 1
)
ELSE NULL
END) as account_name,
(
CASE
WHEN tsa.engine = 'quora' THEN NULL
WHEN tsa.source ILIKE '%uac%' THEN (
SELECT
lue.campaign
FROM reports.lookup_uac_elements lue
WHERE lue.campaign_id = tsa.campaign_id
)
WHEN tsa.engine = 'google' THEN (
SELECT
pse.campaign_name
FROM reports.lookup_paid_search_elements pse
WHERE pse.campaign_id = tsa.campaign_id
AND pse.adgroup_id = tsa.adgroup_id
LIMIT 1
)
WHEN tsa.engine = 'bing' THEN (
SELECT
lbe.campaign_name
FROM reports.lookup_bing_elements lbe
WHERE lbe.campaign_id = tsa.campaign_id
AND lbe.adgroup_id = tsa.adgroup_id
LIMIT 1
)
ELSE NULL
END
) as campaign_name,
(CASE
WHEN tsa.engine = 'quora' THEN NULL
WHEN tsa.source ILIKE '%uac%' THEN NULL
WHEN tsa.engine = 'google' THEN (
SELECT
pse.adgroup_name
FROM reports.lookup_paid_search_elements pse
WHERE pse.campaign_id = tsa.campaign_id
AND pse.adgroup_id = tsa.adgroup_id
LIMIT 1
)
WHEN tsa.engine = 'bing' THEN (
SELECT
lbe.adgroup_name
FROM reports.lookup_bing_elements lbe
WHERE lbe.campaign_id = tsa.campaign_id
AND lbe.adgroup_id = tsa.adgroup_id
LIMIT 1
)
ELSE NULL
END) as adgroup_name,
tsa.creation_platform,
tsa.profile_country,
tsa.ltr_12m,
tsa.costs_12m,
tsa.ltv_12m,
tsa.first_paid_touch,
tsa.even_weights,
tsa.first_team_touch,
(CASE
WHEN tsa.profile_type = 'business' THEN 1
ELSE 0
END) as business,
tsa.content,
tsa.ltr_12m_r,
tsa.costs_12m_r,
tsa.ltv_12m_r,
(CASE
WHEN tsa.profile_type = 'consumer' THEN 1
ELSE 0
END) as consumer
FROM tmp_sem_attribution tsa;

sql grouping into one column

I wrote following sql query for a report.
Select AT.OTHER_GL_CODE As ACC,
GL.GL_ID,
GL.GL_NAME,
(Case When (
AT.OTHER_TRN_DEC = 'Cash'
And AT.OTHER_CR_DR = 'CR'
) Then AT.OTHER_AMOUNT
Else '0'
End
) As 'CASHCR',
(Case When (
AT.OTHER_TRN_DEC = 'Cash'
And AT.OTHER_CR_DR = 'DR'
) Then AT.OTHER_AMOUNT
Else '0'
End
) As 'CASHDR',
(Case When (
AT.OTHER_TRN_DEC <> 'Cash'
And AT.OTHER_CR_DR = 'CR'
) Then AT.OTHER_AMOUNT
Else '0'
End
) As 'OTHERCR',
(Case When (
AT.OTHER_TRN_DEC <> 'Cash'
And AT.OTHER_CR_DR = 'DR'
) Then AT.OTHER_AMOUNT
Else '0'
End
) As 'OTHERDR',
AT.OTHER_BRN_CODE BRNCODE,
(
Select Sum( Case When TR.CR_DR = 'DR'
And TR.BRANCH_CODE = 1000
And TR.TRAN_DATE < '2017-07-01' Then TR.GL_TRN_AMT
Else 0
End
)
From COREBANKER1.dbo.GL_DAILY_TRN As TR
Where TR.GL_CODE = AT.OTHER_GL_CODE
) As DRBRFORE,
(
Select Sum( Case When TR.CR_DR = 'CR'
And TR.BRANCH_CODE = 1000
And TR.TRAN_DATE < '2017-07-01' Then TR.GL_TRN_AMT
Else 0
End
)
From COREBANKER1.dbo.GL_DAILY_TRN As TR
Where TR.GL_CODE = AT.OTHER_GL_CODE
) As CRBRFORE
From COREBANKER1.dbo.MAIN_OTHER_TRN As AT,
COREBANKER1.dbo.GL_MAIN As GL
Where AT.OTHER_BRN_CODE = 1000
And GL.GL_ID = AT.OTHER_GL_CODE
And AT.OTHER_BANK_DATE
Between '2017-07-01' And '2017-07-30';
this is the output
but my desired output is here
So how I change my query for desired result(grouping to ACC and GL_ID)?
The quick and dirty approach is to wrap the entire query and group by, this would prevent duplicate values in your 3,4,5, 6th columns.
The cleaner approach would be to take those 4 columns and wrap each one in a Select SUM(...) as you did for the later columns.
In short, no GROUP BY will leave you with duplicates - I took the liberty of giving you the syntax for the quick and dirty approach, see below.
SELECT
ACC
,GL_ID
,GL_NAME
,SUM(CASHCR) AS CASHCR
,SUM(CASHDR) AS CASHDR
,SUM(OTHERCR) AS OTHERCR
,SUM(OTHERDR) AS OTHERDR
,BRNCODE
,DRBRFORE
,CRBRFORE
FROM (
Select AT.OTHER_GL_CODE As ACC,
GL.GL_ID,
GL.GL_NAME,
(Case When (
AT.OTHER_TRN_DEC = 'Cash'
And AT.OTHER_CR_DR = 'CR'
) Then AT.OTHER_AMOUNT
Else '0'
End
) As 'CASHCR',
(Case When (
AT.OTHER_TRN_DEC = 'Cash'
And AT.OTHER_CR_DR = 'DR'
) Then AT.OTHER_AMOUNT
Else '0'
End
) As 'CASHDR',
(Case When (
AT.OTHER_TRN_DEC <> 'Cash'
And AT.OTHER_CR_DR = 'CR'
) Then AT.OTHER_AMOUNT
Else '0'
End
) As 'OTHERCR',
(Case When (
AT.OTHER_TRN_DEC <> 'Cash'
And AT.OTHER_CR_DR = 'DR'
) Then AT.OTHER_AMOUNT
Else '0'
End
) As 'OTHERDR',
AT.OTHER_BRN_CODE BRNCODE,
(
Select Sum( Case When TR.CR_DR = 'DR'
And TR.BRANCH_CODE = 1000
And TR.TRAN_DATE < '2017-07-01' Then TR.GL_TRN_AMT
Else 0
End
)
From COREBANKER1.dbo.GL_DAILY_TRN As TR
Where TR.GL_CODE = AT.OTHER_GL_CODE
) As DRBRFORE,
(
Select Sum( Case When TR.CR_DR = 'CR'
And TR.BRANCH_CODE = 1000
And TR.TRAN_DATE < '2017-07-01' Then TR.GL_TRN_AMT
Else 0
End
)
From COREBANKER1.dbo.GL_DAILY_TRN As TR
Where TR.GL_CODE = AT.OTHER_GL_CODE
) As CRBRFORE
From COREBANKER1.dbo.MAIN_OTHER_TRN As AT,
COREBANKER1.dbo.GL_MAIN As GL
Where AT.OTHER_BRN_CODE = 1000
And GL.GL_ID = AT.OTHER_GL_CODE
And AT.OTHER_BANK_DATE
Between '2017-07-01' And '2017-07-30'
)A
GROUP BY
ACC
,GL_ID
,GL_NAME
,CASHCR
,CASHDR
,OTHERCR
,OTHERDR
,BRNCODE
,DRBRFORE
,CRBRFORE
;
You could surround your query with a CTE then aggregate the fields you require.
WITH this as
(
Select AT.OTHER_GL_CODE As ACC,
GL.GL_ID,
GL.GL_NAME,
(Case When (
AT.OTHER_TRN_DEC = 'Cash'
And AT.OTHER_CR_DR = 'CR'
) Then AT.OTHER_AMOUNT
Else '0'
End
) As 'CASHCR',
(Case When (
AT.OTHER_TRN_DEC = 'Cash'
And AT.OTHER_CR_DR = 'DR'
) Then AT.OTHER_AMOUNT
Else '0'
End
) As 'CASHDR',
(Case When (
AT.OTHER_TRN_DEC <> 'Cash'
And AT.OTHER_CR_DR = 'CR'
) Then AT.OTHER_AMOUNT
Else '0'
End
) As 'OTHERCR',
(Case When (
AT.OTHER_TRN_DEC <> 'Cash'
And AT.OTHER_CR_DR = 'DR'
) Then AT.OTHER_AMOUNT
Else '0'
End
) As 'OTHERDR',
AT.OTHER_BRN_CODE BRNCODE,
(
Select Sum( Case When TR.CR_DR = 'DR'
And TR.BRANCH_CODE = 1000
And TR.TRAN_DATE < '2017-07-01' Then TR.GL_TRN_AMT
Else 0
End
)
From COREBANKER1.dbo.GL_DAILY_TRN As TR
Where TR.GL_CODE = AT.OTHER_GL_CODE
) As DRBRFORE,
(
Select Sum( Case When TR.CR_DR = 'CR'
And TR.BRANCH_CODE = 1000
And TR.TRAN_DATE < '2017-07-01' Then TR.GL_TRN_AMT
Else 0
End
)
From COREBANKER1.dbo.GL_DAILY_TRN As TR
Where TR.GL_CODE = AT.OTHER_GL_CODE
) As CRBRFORE
From COREBANKER1.dbo.MAIN_OTHER_TRN As AT,
COREBANKER1.dbo.GL_MAIN As GL
Where AT.OTHER_BRN_CODE = 1000
And GL.GL_ID = AT.OTHER_GL_CODE
And AT.OTHER_BANK_DATE
Between '2017-07-01' And '2017-07-30';
)
)
SELECT ACC ,GL_ID ,GL_NAME, SUM(CASHCR) AS CASHCR ,SUM(CASHDR) AS CASHDR ,SUM(OTHERCR) AS OTHERCR ,SUM(OTHERDR) AS OTHERDR, BRNCODE, DRBRFORE, CRBRFORE
FROM this
GROUP BY ACC ,GL_ID ,GL_NAME /*,CASHCR ,CASHDR ,OTHERCR ,OTHERDR */,BRNCODE
,DRBRFORE ,CRBRFORE

ERROR ORA-00905: missing keyword (microsoft ole db provider for oracle)

can anyone see what keyword is missing from the pl/sql query below?
I am getting the error ORA-00905: missing keyword (microsoft ole db provider for oracle)
not sure what keyword is missing - this is an odbc query to oracle database
SELECT "STAT"."ORD"."SAMPLEID",
MAX(CASE WHEN "STAT"."ORDANAEL"."MC"='QAERS' THEN N"STAT"."ORDANAEL"."RES_TXT" ELSE
'' END) AS ERSID,
"STAT"."ORDMCSTP_V"."SEQNB", "STAT"."ORD"."ORDPATNAME",
"STAT"."ORD"."ORDPATBIRTHDT", "STAT"."ORD"."CORORDNB", "STAT"."ORD"."PROJNB",
Max(CASE WHEN "STAT"."ORDANAEL"."MC"='AMMOL' THEN 'AMMOL'
WHEN "STAT"."ORDANAEL"."MC"='AMMOLR' THEN 'AMMOLR' ELSE '' END) AS TEST,
Max(CASE WHEN "STAT"."ORDANAEL"."MC"='AMMOL' THEN N"STAT"."ORDRESMCEL"."MC_RES"
WHEN "STAT"."ORDANAEL"."MC"='AMMOLR' THEN N"STAT"."ORDRESMCEL"."MC_RES" ELSE ''
END) AS MC_RES,
Max( CASE WHEN "STAT"."ORDMCSTP_V"."STEPMC"='AMIMI' THEN N"STAT"."ORDMCSTPEL"."RES"
ELSE '' END) AS IMI,
Max( CASE WHEN "STAT"."ORDMCSTP_V"."STEPMC"='AMIMP' THEN N"STAT"."ORDMCSTPEL"."RES"
ELSE '' END) AS IMP,
Max( CASE WHEN "STAT"."ORDMCSTP_V"."STEPMC"='AMNDM' THEN N "STAT"."ORDMCSTPEL"."RES"
ELSE '' END) AS NDM,
Max( CASE WHEN "STAT"."ORDMCSTP_V"."STEPMC"='AMOX48' THEN
N "STAT"."ORDMCSTPEL"."RES" ELSE '' END) AS OXA48,
Max( CASE WHEN "STAT"."ORDMCSTP_V"."STEPMC"='AMSPM' THEN N "STAT"."ORDMCSTPEL"."RES"
ELSE '' END) AS SPM,
Max( CASE WHEN "STAT"."ORDMCSTP_V"."STEPMC"='AMVIM' THEN N "STAT"."ORDMCSTPEL"."RES"
ELSE '' END) AS VIM,
Max( CASE WHEN "STAT"."ORDMCSTP_V"."STEPMC"='AMDIM' THEN N "STAT"."ORDMCSTPEL"."RES"
ELSE '' END) AS DIM,
Max( CASE WHEN "STAT"."ORDMCSTP_V"."STEPMC"='AMKPC' THEN N "STAT"."ORDMCSTPEL"."RES"
ELSE '' END) AS KPC,
Max( CASE WHEN "STAT"."ORDMCSTP_V"."STEPMC"='AMGES' THEN N "STAT"."ORDMCSTPEL"."RES"
ELSE '' END) AS GES,
Max( CASE WHEN "STAT"."ORDMCSTP_V"."STEPMC"='AMSME' THEN N "STAT"."ORDMCSTPEL"."RES"
ELSE '' END) AS SME,
Max( CASE WHEN "STAT"."ORDMCSTP_V"."STEPMC"='AMFRI1' THEN
N "STAT"."ORDMCSTPEL"."RES" ELSE '' END) AS FRI1,
Max( CASE WHEN "STAT"."ORDMCSTP_V"."STEPMC"='AMSIM' THEN N "STAT"."ORDMCSTPEL"."RES"
ELSE '' END) AS SIM,
Max( CASE WHEN "STAT"."ORDMCSTP_V"."STEPMC"='AMGIM' THEN N "STAT"."ORDMCSTPEL"."RES"
ELSE '' END) AS GIM,
Max( CASE WHEN "STAT"."ORDANAEL"."MC"='AMMOL' THEN N "STAT"."ORDANAEL"."VALMED" ELSE
'' END) AS VALMED_AMMOL,
Max( CASE WHEN "STAT"."ORDANAEL"."MC"='AMMOL' AND
"STAT"."ORDANAEL_D"."DFIELD"='RR' THEN N "STAT"."ORDANAEL_D"."DVALUE" ELSE '' END)
AS RPT_DT_AMMOL,
"STAT"."ORDANAEL"."RES_ST"
FROM (("STAT"."ORDMCSTPEL" INNER JOIN ("STAT"."ORDMCSTP_V" INNER JOIN
("STAT"."ORD" INNER JOIN "STAT"."ORDANAEL" ON ("STAT"."ORD"."LID" =
"STAT"."ORDANAEL"."LID") AND ("STAT"."ORD"."LPERIOD" =
"STAT"."ORDANAEL"."LPERIOD") AND ("STAT"."ORD"."ORDNB" =
"STAT"."ORDANAEL"."ORDNB")) ON ("STAT"."ORDMCSTP_V"."LID" = "STAT"."ORD"."LID")
AND ("STAT"."ORDMCSTP_V"."LPERIOD" = "STAT"."ORD"."LPERIOD") AND
("STAT"."ORDMCSTP_V"."ORDNB" = "STAT"."ORD"."ORDNB")) ON ("STAT"."ORDMCSTPEL"."BC"
= "STAT"."ORDMCSTP_V"."BC") AND ("STAT"."ORDMCSTPEL"."CURRENT_STEPNB" =
"STAT"."ORDMCSTP_V"."CURRENT_STEPNB")) INNER JOIN "STAT"."ORDANAEL_D" ON
("STAT"."ORDANAEL"."NBINT" = "STAT"."ORDANAEL_D"."DNBINT") AND
("STAT"."ORDANAEL"."SPSEQ" = "STAT"."ORDANAEL_D"."DSPSEQ") AND
("STAT"."ORDANAEL"."ORDNB" = "STAT"."ORDANAEL_D"."DORDNB") AND
("STAT"."ORDANAEL"."LPERIOD" = "STAT"."ORDANAEL_D"."DLPERIOD") AND
("STAT"."ORDANAEL"."LID" = "STAT"."ORDANAEL_D"."DLID")) INNER JOIN
"STAT"."ORDRESMCEL" ON ("STAT"."ORDMCSTP_V"."SEQNB" = "STAT"."ORDRESMCEL"."SEQNB")
AND ("STAT"."ORDANAEL"."ORDNB" = "STAT"."ORDRESMCEL"."ORDNB") AND
("STAT"."ORDANAEL"."LPERIOD" = "STAT"."ORDRESMCEL"."LPERIOD") AND
("STAT"."ORDANAEL"."LID" = "STAT"."ORDRESMCEL"."LID")
WHERE (("STAT"."ORD"."LID")='H1')
AND (("STAT"."ORD"."LPERIOD")>'5200')
AND (("STAT"."ORD"."PROJNB")='ACPOERS')
GROUP BY "STAT"."ORD"."SAMPLEID", "STAT"."ORDMCSTP_V"."SEQNB",
"STAT"."ORD"."ORDPATNAME", "STAT"."ORD"."ORDPATBIRTHDT", "STAT"."ORD"."CORORDNB",
"STAT"."ORD"."PROJNB", "STAT"."ORDANAEL"."RES_ST"
HAVING ((("STAT"."ORDMCSTP_V"."SEQNB") Is Not Null) AND (("STAT"."ORD"."PROJNB")
='ACPOERS') AND (("STAT"."ORDANAEL"."RES_ST")>'4'))
Just too big for comment. Just cleaned up the double quotes and the N and it comes clean in a SQL formatter.
SELECT STAT.ORD.SAMPLEID,
MAX ( CASE WHEN STAT.ORDANAEL.MC = 'QAERS' THEN STAT.ORDANAEL.RES_TXT ELSE '' END ) AS ersid,
STAT.ORDMCSTP_V.SEQNB,
STAT.ORD.ORDPATNAME,
STAT.ORD.ORDPATBIRTHDT,
STAT.ORD.CORORDNB,
STAT.ORD.PROJNB,
MAX (
CASE
WHEN STAT.ORDANAEL.MC = 'AMMOL' THEN 'AMMOL'
WHEN STAT.ORDANAEL.MC = 'AMMOLR' THEN 'AMMOLR'
ELSE ''
END )
AS test,
MAX (
CASE
WHEN STAT.ORDANAEL.MC = 'AMMOL' THEN STAT.ORDRESMCEL.MC_RES
WHEN STAT.ORDANAEL.MC = 'AMMOLR' THEN STAT.ORDRESMCEL.MC_RES
ELSE ''
END )
AS mc_res,
MAX ( CASE WHEN STAT.ORDMCSTP_V.STEPMC = 'AMIMI' THEN STAT.ORDMCSTPEL.RES ELSE '' END ) AS imi,
MAX ( CASE WHEN STAT.ORDMCSTP_V.STEPMC = 'AMIMP' THEN STAT.ORDMCSTPEL.RES ELSE '' END ) AS imp,
MAX ( CASE WHEN STAT.ORDMCSTP_V.STEPMC = 'AMNDM' THEN STAT.ORDMCSTPEL.RES ELSE '' END ) AS ndm,
MAX ( CASE WHEN STAT.ORDMCSTP_V.STEPMC = 'AMOX48' THEN STAT.ORDMCSTPEL.RES ELSE '' END ) AS oxa48,
MAX ( CASE WHEN STAT.ORDMCSTP_V.STEPMC = 'AMSPM' THEN STAT.ORDMCSTPEL.RES ELSE '' END ) AS spm,
MAX ( CASE WHEN STAT.ORDMCSTP_V.STEPMC = 'AMVIM' THEN STAT.ORDMCSTPEL.RES ELSE '' END ) AS vim,
MAX ( CASE WHEN STAT.ORDMCSTP_V.STEPMC = 'AMDIM' THEN STAT.ORDMCSTPEL.RES ELSE '' END ) AS dim,
MAX ( CASE WHEN STAT.ORDMCSTP_V.STEPMC = 'AMKPC' THEN STAT.ORDMCSTPEL.RES ELSE '' END ) AS kpc,
MAX ( CASE WHEN STAT.ORDMCSTP_V.STEPMC = 'AMGES' THEN STAT.ORDMCSTPEL.RES ELSE '' END ) AS ges,
MAX ( CASE WHEN STAT.ORDMCSTP_V.STEPMC = 'AMSME' THEN STAT.ORDMCSTPEL.RES ELSE '' END ) AS sme,
MAX ( CASE WHEN STAT.ORDMCSTP_V.STEPMC = 'AMFRI1' THEN STAT.ORDMCSTPEL.RES ELSE '' END ) AS fri1,
MAX ( CASE WHEN STAT.ORDMCSTP_V.STEPMC = 'AMSIM' THEN STAT.ORDMCSTPEL.RES ELSE '' END ) AS sim,
MAX ( CASE WHEN STAT.ORDMCSTP_V.STEPMC = 'AMGIM' THEN STAT.ORDMCSTPEL.RES ELSE '' END ) AS gim,
MAX ( CASE WHEN STAT.ORDANAEL.MC = 'AMMOL' THEN STAT.ORDANAEL.VALMED ELSE '' END )
AS valmed_ammol,
MAX ( CASE
WHEN STAT.ORDANAEL.MC = 'AMMOL'
AND STAT.ORDANAEL_D.DFIELD = 'RR' THEN
STAT.ORDANAEL_D.DVALUE
ELSE
''
END )
AS rpt_dt_ammol,
STAT.ORDANAEL.RES_ST
FROM ((STAT.ORDMCSTPEL
INNER JOIN (STAT.ORDMCSTP_V
INNER JOIN (STAT.ORD
INNER JOIN STAT.ORDANAEL
ON ( STAT.ORD.LID = STAT.ORDANAEL.LID )
AND ( STAT.ORD.LPERIOD = STAT.ORDANAEL.LPERIOD )
AND ( STAT.ORD.ORDNB = STAT.ORDANAEL.ORDNB ))
ON ( STAT.ORDMCSTP_V.LID = STAT.ORD.LID )
AND ( STAT.ORDMCSTP_V.LPERIOD = STAT.ORD.LPERIOD )
AND ( STAT.ORDMCSTP_V.ORDNB = STAT.ORD.ORDNB ))
ON ( STAT.ORDMCSTPEL.BC = STAT.ORDMCSTP_V.BC )
AND ( STAT.ORDMCSTPEL.CURRENT_STEPNB = STAT.ORDMCSTP_V.CURRENT_STEPNB ))
INNER JOIN STAT.ORDANAEL_D
ON ( STAT.ORDANAEL.NBINT = STAT.ORDANAEL_D.DNBINT )
AND ( STAT.ORDANAEL.SPSEQ = STAT.ORDANAEL_D.DSPSEQ )
AND ( STAT.ORDANAEL.ORDNB = STAT.ORDANAEL_D.DORDNB )
AND ( STAT.ORDANAEL.LPERIOD = STAT.ORDANAEL_D.DLPERIOD )
AND ( STAT.ORDANAEL.LID = STAT.ORDANAEL_D.DLID ))
INNER JOIN STAT.ORDRESMCEL
ON ( STAT.ORDMCSTP_V.SEQNB = STAT.ORDRESMCEL.SEQNB )
AND ( STAT.ORDANAEL.ORDNB = STAT.ORDRESMCEL.ORDNB )
AND ( STAT.ORDANAEL.LPERIOD = STAT.ORDRESMCEL.LPERIOD )
AND ( STAT.ORDANAEL.LID = STAT.ORDRESMCEL.LID )
WHERE ( ( STAT.ORD.LID ) = 'H1' )
AND ( ( STAT.ORD.LPERIOD ) > '5200' )
AND ( ( STAT.ORD.PROJNB ) = 'ACPOERS' )
GROUP BY STAT.ORD.SAMPLEID,
STAT.ORDMCSTP_V.SEQNB,
STAT.ORD.ORDPATNAME,
STAT.ORD.ORDPATBIRTHDT,
STAT.ORD.CORORDNB,
STAT.ORD.PROJNB,
STAT.ORDANAEL.RES_ST
HAVING ( ( ( STAT.ORDMCSTP_V.SEQNB ) IS NOT NULL )
AND ( ( STAT.ORD.PROJNB ) = 'ACPOERS' )
AND ( ( STAT.ORDANAEL.RES_ST ) > '4' ) );
You can use N in front of a string literal to mark it as an nvarchar2. Example:
select N'abc' from dual
But you can't use it in front of an identifier the way you are doing it. If you want to read a certain column value as an nvarchar2, you'll need to cast it as such. Example:
... THEN cast("STAT"."ORDANAEL"."RES_TXT" as nvarchar2(50)) ...

Adding a count of values

Is there another way I can sum up counts with less code?
I'm using a view (my only option) to try to find out if a customer spent money during any two of the last 5 calendar years
Table name:
V_PERSON
V_REVENUE
Columns:
V_PERSON.ID
V.REVENUE.PersonID
V.REVENUE.Year1 revenue for the year (currently 2016)
V.REVENUE.Year2 revenue for the year (currently, 2015)
V.REVENUE.Year3
V.REVENUE.Year4
V.REVENUE.Year5
Here's what I've tried:
SELECT V_PERSON.ID
FROM V_PERSON
WHERE
(
(
SELECT '1'
FROM V_REVENUE
WHERE V_REVENUE.PersonID = V_PERSON.ID
AND V_REVENUE.Year1 > 0
)
+
(
SELECT '1'
FROM V_REVENUE
WHERE V_REVENUE.PersonID = V_PERSON.ID
AND V_REVENUE.Year2 > 0
)
+
(
SELECT '1'
FROM V_REVENUE
WHERE V_REVENUE.PersonID = V_PERSON.ID
AND V_REVENUE.Year3 > 0
)
+
(
SELECT '1'
FROM V_REVENUE
WHERE V_REVENUE.PersonID = V_PERSON.ID
AND V_REVENUE.Year4 > 0
)
+
(
SELECT '1'
FROM V_REVENUE
WHERE V_REVENUE.PersonID = V_PERSON.ID
AND V_REVENUE.Year5 > 0
)
) >= 2
Here's one option using exists with multiple case statements:
select id
from person p
where exists (
select 1
from revenue r
where p.id = r.personid
and case when r.year1 > 0 then 1 else 0 end +
case when r.year2 > 0 then 1 else 0 end +
case when r.year3 > 0 then 1 else 0 end +
case when r.year4 > 0 then 1 else 0 end +
case when r.year5 > 0 then 1 else 0 end >= 2
)
How about a CASE statement:
SELECT V_PERSON.ID
FROM V_PERSON
WHERE
(
SELECT (case when V_REVENUE.Year1 > 0 then 1 else 0 end) +
(case when V_REVENUE.Year2 > 0 then 1 else 0 end) +
(case when V_REVENUE.Year3 > 0 then 1 else 0 end) +
(case when V_REVENUE.Year4 > 0 then 1 else 0 end) +
(case when V_REVENUE.Year5 > 0 then 1 else 0 end)
FROM V_REVENUE
WHERE V_REVENUE.PersonID = V_PERSON.ID) >= 2