Bad performance for muli joined sub-queries - sql-server-2008-r2

Could someone help me on this query. When generating the related report through web application it times out due to response time taking over 30seconds(standard http request time out).
select c.counterparty_shortname AS [Réference]
,cty.counterparty_type_name AS [Type ref]
,t.transaction_shortname AS [transaction_shortname]
,mitigant_shortname
,(SELECT ISNULL(SUM(ISNULL(tce.outstanding_amount,0)),0)
from transaction_credit_event tce
where tce.transaction_id=t.transaction_id) AS [Montant impayé]
,(select count(t1.transaction_id)
from [transaction] t1
where t1.counterparty_id=c.counterparty_id) AS [Nbre affaire]
,(select isnull(count(tce.credit_event_id),0)
from transaction_credit_event tce
where tce.transaction_id=t.transaction_id
and isnull(tce.outstanding_amount,0)>0
and tce.credit_event_type_id=51) AS [nbr impaye]
,TT.transaction_type_name AS [Type Produit]
,isnull(cm1.value1_text,'') AS [type garantie]
,isnull(cm2.value1_text,'') AS [marque]
,isnull(cm3.value1_text,'') AS [modele]
,(select top 1 tmp1.payment_posting_date
from (select pp.payment_posting_date
,tce.transaction_id
FROM payment_posting pp
inner JOIN transaction_credit_event tce
ON pp.credit_event_id=tce.credit_event_id) tmp1
where tmp1.transaction_id=t.transaction_id
order by tmp1.payment_posting_date desc) AS [Date dernier paiement]
,fate_shortname AS [sort]
,raf.recovery_action_fate_id
,ISNULL(t.outstanding_amount,0) AS [CRD]
,1 AS [ID]
,(select isnull(count(tce.credit_event_id),0)
from transaction_credit_event tce,[transaction] tt
where tce.transaction_id=tt.transaction_id
and tt.counterparty_id=c.counterparty_id
and isnull(tce.outstanding_amount,0)>0
and tce.credit_event_type_id=51) AS [nbr impaye tiers]
,convert(date,dispatch_start_date,103) AS [date debut]
,convert(date,dispatch_end_date,103) AS [date fin]
,c.counterparty_type_id
,t.transaction_type_id
from counterparty c
inner join wf_task_dispatch w on w.item_id=c.counterparty_id
inner join [transaction] t on c.counterparty_id=t.counterparty_id
LEFT join transaction_recovery tr on tr.counterparty_id=c.counterparty_id and tr.transaction_recovery_id=(select top 1 transaction_recovery_id from transaction_recovery where counterparty_id=c.counterparty_id order by idate desc)
LEFT join recovery_action_fate raf on raf.recovery_action_fate_id=tr.recovery_action_fate_id
left join counterparty_type cty on cty.counterparty_type_id=c.counterparty_type_id
left join [transaction_type] TT on t.transaction_type_id = TT.transaction_type_id
inner join mitigant_transaction mt on mt.transaction_id=t.transaction_id and mt.ddate is null
inner join mitigant m on m.mitigant_id=mt.mitigant_id and mitigant_screen='asset' and m.ddate is null
left join constant_matrix cm1 on cm1.criteria1_text=m.regulatory_mitigant_type and cm1.constant_matrix_shortname='CAIN' and cm1.ddate is null
left join constant_matrix cm2 on cm2.criteria1_text=m.valuation_frequency and cm2.constant_matrix_shortname='MARQ' and cm2.ddate is null
left join constant_matrix cm3 on cm3.criteria1_text=m.valuation_source and cm3.constant_matrix_shortname='GMOD' and cm3.ddate is null
where (select [dbo].FN_GetPhase)=81

Related

postgres error 42803 with subquery and group by

Hi all postgres developer,
below code can run success
select p.* from
(select p.*, count(distinct p1.id) n from TMB p
left join TMB p1 on p.id = p1.pid
left join TUR u on p.id = any(u.jks)
group by p.id) p
join TUR u on u.id = p.uid
but, below code with error message
[42803] ERROR: column "p.xxxx" must appear in the GROUP BY clause or be used in an aggregate function
select p.* from
(select p.*, count(distinct p1.id) n from (select * from TMB) p
left join TMB p1 on p.id = p1.pid
left join TUR u on p.id = any(u.jks)
group by p.id) p
join TUR u on u.id = p.uid
I want to do some where filter on TMB table before left join, so I think can speed up left join.
I think (select * from TMB) is a subquery equal as TMB. I can not understand why this error message. anyone can tell me detail?
The difference is that without the subquery, PostgreSQL can deduce that id is the primary key of tmb, so you need not add all columns of tmb to the GROUP BY clause. With the subquery, PostgreSQL cannot make that deduction, so you have to add all columns.

Need equivalent JPA query for a sql query

I want to write equivalent JPA query to the following sql query, Could you please help?
select s.id, s.spec_system, s.pc, s.end_date
FROM context_service_subscription s
LEFT JOIN ctxsrv_sub_spec_variants sv
ON s.id = sv.subscription_id
LEFT JOIN ctxsrv_sub_change_variants cv
ON s.id = cv.subscription_id
WHERE s.end_date >= now()
order by s.id
I have tried below, but does not work
<named-query name="findAllSubscriptionVariant">
<query>SELECT DISTINCT s FROM Subscription s LEFT JOIN FETCH s.specVariants sv LEFT JOIN FETCH
s.changeVariants cv order by s.id</query>
</named-query>
can this
select s.id, s.spec_system, s.pc, s.end_date
FROM context_service_subscription s
LEFT JOIN ctxsrv_sub_spec_variants sv
ON s.id = sv.subscription_id
LEFT JOIN ctxsrv_sub_change_variants cv
ON s.id = cv.subscription_id
WHERE s.end_date >= now()
order by s.id
be rewritten into
select s.id, s.spec_system, s.pc, s.end_date
FROM context_service_subscription s
WHERE s.end_date >= now()
order by s.id
?
if yes your jpql query should become
select s.id, s.spec_system, s.pc, s.end_date
FROM Subscription s
WHERE s.end_date >= now()
order by s.id
where you should change id, spec_system, pc, end_date into the names of the members of Subscription

Postgres Procedure / function dependencies

In pgAdmin tool, when I click on any user-defined function or a procedure on the left side panel, I'm able to see the dependent DB objects like user-defined Types, Schema, Language, Roles etc. in the 'Dependencies' tab.
Is it possible to get the underlying query for those dependencies? Because I need to take the dependent objects for all the procedures and functions from a set of schemas.
Below is the code used in pgAdmin to show Dependents for PostgreSQL 12 plus versions.
This shows up all dependents for all objects. Needs to be filtered out for required objects using oid.
SELECT DISTINCT dep.classid, dep.objid, dep.objsubid, dep.refclassid, dep.refobjid,
dep.deptype,
cl.relkind, ad.adbin, pg_get_expr(ad.adbin, ad.adrelid) as adsrc,
COALESCE(coc.relname, clrw.relname) AS ownertable,
CASE WHEN cl.relname IS NOT NULL AND att.attname IS NOT NULL THEN cl.relname || COALESCE('.' || att.attname, '')
ELSE COALESCE(cl.relname, co.conname, pr.proname, tg.tgname, ty.typname, la.lanname, rw.rulename, ns.nspname,
fs.srvname, fdw.fdwname, evt.evtname, col.collname, ftsc.cfgname, ftsd.dictname, ftsp.prsname,
ftst.tmplname, ext.extname, syn.synname, pl.polname)
END AS refname,
COALESCE(nsc.nspname, nso.nspname, nsp.nspname, nst.nspname, nsrw.nspname, colns.nspname, ftscns.nspname,
ftsdns.nspname, ftspns.nspname, ftstns.nspname, synns.nspname) AS dep_obj_schema,
CASE WHEN cl.relkind IS NOT NULL THEN CASE WHEN cl.relkind = 'r' THEN cl.relkind || COALESCE(dep.objsubid::text, '') ELSE cl.relkind END
WHEN tg.oid IS NOT NULL THEN CASE WHEN tg.tgpackageoid != 0 THEN 'Tc'::text ELSE 'Tr'::text END
WHEN ty.oid IS NOT NULL THEN CASE WHEN ty.typtype = 'd' THEN 'd'::text ELSE 'Ty'::text END
WHEN ns.oid IS NOT NULL THEN CASE WHEN ns.nspparent != 0 AND ns.nspcompoundtrigger = false THEN 'Pa'::text ELSE 'n'::text END
WHEN pr.oid IS NOT NULL AND (prtyp.typname = 'trigger' OR prtyp.typname = 'event_trigger') THEN 'Pt'::text
WHEN pr.oid IS NOT NULL THEN CASE WHEN pr.prokind = 'p' THEN 'Procedure'::text ELSE 'Function'::text END
WHEN la.oid IS NOT NULL THEN 'l'::text
WHEN rw.oid IS NOT NULL THEN 'Rl'::text
WHEN co.oid IS NOT NULL THEN CASE WHEN co.contypid > 0 THEN 'Cd' ELSE 'C'::text || contype END
WHEN ad.oid IS NOT NULL THEN 'A'::text
WHEN fs.oid IS NOT NULL THEN 'Fs'::text
WHEN fdw.oid IS NOT NULL THEN 'Fw'::text
WHEN evt.oid IS NOT NULL THEN 'Et'::text
WHEN col.oid IS NOT NULL THEN 'Co'::text
WHEN ftsc.oid IS NOT NULL THEN 'Fc'::text
WHEN ftsp.oid IS NOT NULL THEN 'Fp'::text
WHEN ftsd.oid IS NOT NULL THEN 'Fd'::text
WHEN ftst.oid IS NOT NULL THEN 'Ft'::text
WHEN ext.oid IS NOT NULL THEN 'Ex'::text
WHEN syn.oid IS NOT NULL THEN 'Sy'::text
WHEN pl.oid IS NOT NULL THEN 'Rs'::text
ELSE ''
END AS dep_obj_type
CASE WHEN inhits.inhparent IS NOT NULL THEN '1' ELSE '0' END AS is_inherits,
CASE WHEN inhed.inhparent IS NOT NULL THEN '1' ELSE '0' END AS is_inherited
FROM pg_catalog.pg_depend dep
LEFT JOIN pg_catalog.pg_class cl ON dep.objid=cl.oid
LEFT JOIN pg_catalog.pg_attribute att ON dep.objid=att.attrelid AND dep.objsubid=att.attnum
LEFT JOIN pg_catalog.pg_namespace nsc ON cl.relnamespace=nsc.oid
LEFT JOIN pg_catalog.pg_proc pr ON dep.objid=pr.oid
LEFT JOIN pg_catalog.pg_namespace nsp ON pr.pronamespace=nsp.oid
LEFT JOIN pg_catalog.pg_trigger tg ON dep.objid=tg.oid
LEFT JOIN pg_catalog.pg_type ty ON dep.objid=ty.oid
LEFT JOIN pg_catalog.pg_namespace nst ON ty.typnamespace=nst.oid
LEFT JOIN pg_catalog.pg_constraint co ON dep.objid=co.oid
LEFT JOIN pg_catalog.pg_class coc ON co.conrelid=coc.oid
LEFT JOIN pg_catalog.pg_namespace nso ON co.connamespace=nso.oid
LEFT JOIN pg_catalog.pg_rewrite rw ON dep.objid=rw.oid
LEFT JOIN pg_catalog.pg_class clrw ON clrw.oid=rw.ev_class
LEFT JOIN pg_catalog.pg_namespace nsrw ON clrw.relnamespace=nsrw.oid
LEFT JOIN pg_catalog.pg_language la ON dep.objid=la.oid
LEFT JOIN pg_catalog.pg_namespace ns ON dep.objid=ns.oid
LEFT JOIN pg_catalog.pg_attrdef ad ON ad.oid=dep.objid
LEFT JOIN pg_catalog.pg_foreign_server fs ON fs.oid=dep.objid
LEFT JOIN pg_catalog.pg_foreign_data_wrapper fdw ON fdw.oid=dep.objid
LEFT JOIN pg_catalog.pg_type prtyp ON prtyp.oid = pr.prorettype
LEFT JOIN pg_catalog.pg_inherits inhits ON (inhits.inhrelid=dep.objid)
LEFT JOIN pg_catalog.pg_inherits inhed ON (inhed.inhparent=dep.objid)
LEFT JOIN pg_catalog.pg_event_trigger evt ON evt.oid=dep.objid
LEFT JOIN pg_catalog.pg_collation col ON col.oid=dep.objid
LEFT JOIN pg_catalog.pg_namespace colns ON col.collnamespace=colns.oid
LEFT JOIN pg_catalog.pg_ts_config ftsc ON ftsc.oid=dep.objid
LEFT JOIN pg_catalog.pg_namespace ftscns ON ftsc.cfgnamespace=ftscns.oid
LEFT JOIN pg_catalog.pg_ts_dict ftsd ON ftsd.oid=dep.objid
LEFT JOIN pg_catalog.pg_namespace ftsdns ON ftsd.dictnamespace=ftsdns.oid
LEFT JOIN pg_catalog.pg_ts_parser ftsp ON ftsp.oid=dep.objid
LEFT JOIN pg_catalog.pg_namespace ftspns ON ftsp.prsnamespace=ftspns.oid
LEFT JOIN pg_catalog.pg_ts_template ftst ON ftst.oid=dep.objid
LEFT JOIN pg_catalog.pg_namespace ftstns ON ftst.tmplnamespace=ftstns.oid
LEFT JOIN pg_catalog.pg_extension ext ON ext.oid=dep.objid
LEFT JOIN pg_catalog.pg_synonym syn ON syn.oid=dep.objid
LEFT JOIN pg_catalog.pg_namespace synns ON syn.synnamespace=synns.oid
LEFT JOIN pg_catalog.pg_policy pl ON pl.oid=dep.objid
WHERE
classid IN ( SELECT oid FROM pg_catalog.pg_class WHERE relname IN
('pg_class', 'pg_constraint', 'pg_conversion', 'pg_language', 'pg_proc', 'pg_rewrite', 'pg_namespace',
'pg_trigger', 'pg_type', 'pg_attrdef', 'pg_event_trigger', 'pg_foreign_server', 'pg_foreign_data_wrapper',
'pg_collation', 'pg_ts_config', 'pg_ts_dict', 'pg_ts_parser', 'pg_ts_template', 'pg_extension',
'pg_synonym', 'pg_policy'));

Problems with Postgresql ERROR: subquery uses ungrouped column "ev.title" from outer query

I have a query like this:
select c.id, c.name, c.website, c.longdescription, c.description, c.email,
(SELECT jsonb_agg(ev) FROM
(SELECT ev.title, ev.description, ev.longdescription,
(SELECT jsonb_agg(ed) FROM
(SELECT ed.startdate, ed.enddate, ed.id WHERE ed.id notnull)ed) as dates, ev.id WHERE ev.id notnull) ev) as events,
(SELECT jsonb_agg(ca) FROM (SELECT ct.zip, ca.id, ca.street1, ca.street2, ca.addresstype_id, ST_Y(ca.geopoint::geometry) as latitude, ST_X(ca.geopoint::geometry) as longitude
WHERE ca.id notnull)ca) as addresses
FROM companies c
LEFT JOIN events ev ON ev.company_id = c.id
LEFT JOIN companyaddresses ca ON ca.company_id = c.id
LEFT JOIN cities ct ON ct.id = ca.city_id
LEFT JOIN eventdates ed ON ed.event_id = ev.id
GROUP by c.id
I am getting the error "ERROR: subquery uses ungrouped column "ev.title" from
outer query Position: 125".
Can't figure out how to group it correctly for the subqueries. Any suggestions?
Give this a try:
SELECT c.id, c.name, c.website, c.longdescription, c.description, c.email,
    (SELECT jsonb_agg(ev) FROM
        (SELECT even.title, even.description, even.longdescription,
            (SELECT jsonb_agg(ed) FROM
                (SELECT eventdates.startdate, eventdates.enddate, eventdates.id FROM eventdates WHERE eventdates.event_id = even.id)ed) as dates,
        even.id FROM events even WHERE even.company_id = c.id) ev) as events,
jsonb_agg((SELECT ca FROM (SELECT ct.zip, ca.id, ca.street1, ca.street2, ca.addresstype_id, ST_Y(ca.geopoint::geometry) as latitude, ST_X(ca.geopoint::geometry) as longitude WHERE ca.id notnull)ca)) as addresses
FROM companies c
LEFT JOIN companyaddresses ca ON ca.company_id = c.id
LEFT JOIN cities ct ON ct.id = ca.city_id
Group by c.id

Why do these two queries return a different number of rows

I'm using adventureworks2012:
The first query returns 43 rows and no nulls, whereas the second returns over 19 thousand and lots of nulls.
I thought an outer join returned rows from the left side of the query even if the condition is not met, and therefore the two queries should be equivalent?
--1
SELECT c.CustomerID, s.SalesOrderID, s.OrderDate
FROM Sales.Customer AS c
LEFT OUTER JOIN Sales.SalesOrderHeader AS s ON c.CustomerID = s.CustomerID
WHERE s.OrderDate = '2005/07/01';
--2
WITH orders AS (
SELECT SalesOrderID, CustomerID, OrderDate
FROM Sales.SalesOrderHeader
WHERE OrderDate = '2005/07/01'
)
SELECT c.CustomerID, orders.SalesOrderID, orders.OrderDate
FROM Sales.Customer AS c
LEFT OUTER JOIN orders ON c.CustomerID = orders.CustomerID
ORDER BY orders.OrderDate DESC;
It's because of WHERE clause in query 1. The WHERE clause filters the final result of the two tables being joined. You need to move the condition from WHERE clause into ON, which filters the records (on table Sales.SalesOrderHeader) first before joining it to another table (Sales.Customer), you can also find it here.
SELECT c.CustomerID, s.SalesOrderID, s.OrderDate
FROM Sales.Customer AS c
LEFT OUTER JOIN Sales.SalesOrderHeader AS s
ON c.CustomerID = s.CustomerID AND
s.OrderDate = '2005/07/01';
You are killing the outer in the first by referencing a table in the where
Just move that condition into the join
SELECT c.CustomerID, s.SalesOrderID, s.OrderDate
FROM Sales.Customer AS c
LEFT OUTER JOIN Sales.SalesOrderHeader AS s
ON c.CustomerID = s.CustomerID
AND s.OrderDate = '2005/07/01';