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
Related
We created a view in Postgres and I am getting strange result.
View Name: event_puchase_product_overview
When I try to get records with *, I get the correct result. but when I try to get specific fields, I get wrong values.
I hope the screens attached here can explain the problem well.
select *
from event_purchase_product_overview
where id = 15065;
select id, departure_id
from event_puchase_product_overview
where id = 15065;
VIEW definition:
CREATE OR REPLACE VIEW public.event_puchase_product_overview AS
SELECT row_number() OVER () AS id,
e.id AS departure_id,
e.type AS event_type,
e.name,
p.id AS product_id,
pc.name AS product_type,
product_date.attribute AS option,
p.upcomming_date AS supply_date,
pr.date_end AS bid_deadline,
CASE
WHEN (pt.categ_id IN ( SELECT unnest(tt.category_ids) AS unnest
FROM ( SELECT string_to_array(btrim(ir_config_parameter.value, '[]'::text), ', '::text)::integer[] AS category_ids
FROM ir_config_parameter
WHERE ir_config_parameter.key::text = 'trip_product_flight.product_category_hotel'::text) tt)) THEN e.maximum_rooms
WHEN (pt.categ_id IN ( SELECT unnest(tt.category_ids) AS unnest
FROM ( SELECT string_to_array(btrim(ir_config_parameter.value, '[]'::text), ', '::text)::integer[] AS category_ids
FROM ir_config_parameter
WHERE ir_config_parameter.key::text = 'trip_product_flight.product_category_flight'::text) tt)) THEN e.maximum_seats
WHEN (pt.categ_id IN ( SELECT unnest(tt.category_ids) AS unnest
FROM ( SELECT string_to_array(btrim(ir_config_parameter.value, '[]'::text), ', '::text)::integer[] AS category_ids
FROM ir_config_parameter
WHERE ir_config_parameter.key::text = 'trip_product_flight.product_category_bike'::text) tt)) THEN e.maximum_bikes
ELSE e.maximum_seats
END AS departure_qty,
CASE
WHEN now()::date > pr.date_end AND po.state::text = 'draft'::text THEN true
ELSE false
END AS is_deadline,
pl.product_qty::integer AS purchased_qty,
pl.comments,
pl.price_unit AS unit_price,
rp.id AS supplier,
po.id AS po_ref,
po.state AS po_state,
po.date_order AS po_date,
po.user_id AS operator,
pl.po_state_line AS line_status
FROM event_event e
LEFT JOIN product_product p ON p.related_departure = e.id
LEFT JOIN product_template pt ON pt.id = p.product_tmpl_id
LEFT JOIN product_category pc ON pc.id = pt.categ_id
LEFT JOIN purchase_order_line pl ON pl.product_id = p.id
LEFT JOIN purchase_order po ON po.id = pl.order_id
LEFT JOIN purchase_order_purchase_requisition_rel prr ON prr.purchase_order_id = po.id
LEFT JOIN purchase_requisition pr ON pr.id = prr.purchase_requisition_id
LEFT JOIN res_partner rp ON rp.id = po.partner_id
LEFT JOIN ( SELECT p_1.id AS product_id,
pav.name AS attribute
FROM product_product p_1
LEFT JOIN product_attribute_value_product_product_rel pa ON pa.prod_id = p_1.id
LEFT JOIN product_attribute_value pav ON pav.id = pa.att_id
LEFT JOIN product_attribute pat ON pat.id = pav.attribute_id
WHERE pat.name::text <> ALL (ARRAY['Date'::character varying, 'Departure'::character varying]::text[])) product_date ON product_date.product_id = p.id
WHERE (p.id IN ( SELECT DISTINCT mrp_bom_line.product_id
FROM mrp_bom_line)) AND p.active
ORDER BY e.id, pt.categ_id, p.id;
If I add new event_event or new product_product I'll get a new definition of row_number in my view, then the column ID of my view is not stable.
at least you can't use row_number as Id of the view,
If you insist to use row_number, you can use the Order By "creation DATE" by this way all new records will be as last lines in the view and this will not change the correspondency between ID (row_number) and other columns.
Hope that helps !
Very likely the execution plan of your query depends on the columns you select. Compare the execution plans!
Your id is generated using the row_number window function. Now window functions are executed before the ORDER BY clause, so the order will depend on the execution plan and hence on the columns you select.
Using row_number without an explicit ordering doesn't make any sense.
To fix that, don't use
row_number() OVER ()
but
row_number() OVER (ORDER BY e.id, pt.categ_id, p.id)
so that you have a reliable ordering.
In addition, you should omit the ORDER BY clause at the end.
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
I had a query similar to the following and was wondering that DB2 complained about the correlation use in the ORDER BY clause. It errored with something like
[42703][-206] "A.ID" is not valid in the context where it is used..
SQLCODE=-206, SQLSTATE=42703
I was able to rewrite the query to avoid the correlation usage but I couldn't find a reference in the documenation about this. Is this a bug or am I just not able to find details on the expected behavior?
SELECT a.id
FROM A a
ORDER BY (
SELECT COUNT(*)
FROM B b
WHERE b.id = a.id
)
You can't use correlated query in order by clause. However there is many ways to get same result, for example
select count(*) as count_num ,a.ID
from
a join b on a.ID=b.ID
GROUP BY a.ID
order by 1 DESC
solution 1:
SELECT a.id, (select count(*) from B where B.id=a.id) nbOFB
FROM A
order by 2
solution 2:
select * from (
SELECT a.id, (select count(*) from B where B.id=a.id) nbOFB
FROM A
) tmp
order by nbOFB
Solution 3:
SELECT a.id, c.nb
FROM A
inner join lateral
(
select count(*) nb from B where B.id=a.id
) c on 1=1
order by c.nb
Solution 4 :
SELECT a.id, ifnull(c.nb, 0) nb
FROM A
left outer join
(
select b.id, count(*) nb from B group by b.id
) c on a.id=c.id
order by ifnull(c.nb, 0)
Solution 5:
with c as (
select b.id, count(*) nb from B group by b.id
)
SELECT a.id, ifnull(c.nb, 0) nb
FROM A left outer join c on a.id=c.id
order by ifnull(c.nb, 0)
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
I'm tring to grab all fields from the latest Cash record, and then all fields from the related TransactionInfo record. I can't quite get this to work yet:
select t.*, top 1 c.* from Cash c
inner join TransactionInfo t
on c.TransactionID = t.id
order by c.createdOn desc
select top 1 *
from Cash c
inner join TransactionInfo t on c.TransactionID = t.id
order by createdOn desc
What's that top 1 doing there? If you only want one row then the TOP(1) must come first:
SELECT TOP(1) t.*, c.*
FROM Cash c
INNER JOIN TransactionInfo t
ON c.TransactionID = t.id
ORDER BY c.createdOn DESC
select t.,c.
from (Select top 1 * from Cash order by createdOn desc
) c
inner join TransactionInfo t
on c.TransactionID = t.id
order by createdOn desc
DOn;t use select * especially with a join, it wastes server resources.
SELECT c.*, t.* FROM cash c, transactioninfo t
WHERE c.infoid = t.id AND c.createdOn = (SELECT max(createdOn) FROM cash WHERE infoId = t.id) ORDER BY transactiontabledate desc
You need to find the record with the latest date from the cash table for each transactionId and use that also to filter it out in your query.