0

I prepared a flexible search query. Here, I developed a condition like:

  1. Order status is completed in orders

  2. And order entries which are present in order

  3. Getting products which are in orderentries

For this I wrote a query

select {p.pk} from {
  order as o 
  join OrderStatus as os on {os.pk}={o.status}
  join orderentry as oe on{oe.order}={o.pk}
  join product as p on {oe.product}={p.pk}
}
where {os.code}='COMPLETED' 
AND {o.date}>'2020-08-16 00:00:00.000' AND{o.date}<'2020-09-30 00:00:00.000' 
group by{p.pk} order by count({oe.pk}) desc limit 10

here in this query what I want is I want to get all product information like

select * from Product}

How to modify this query get all products?

David Buck
  • 3,752
  • 35
  • 31
  • 35

1 Answers1

0

You can do this using a subselect. The first query you posted above will be the subselect. You simply need to add another select to fetch the product information for all the PKs returned in the subselect.

select * from {Product as prod} where {prod.pk} in 
({{ 
  select 
    top 10 {p.pk} 
  from 
    {
      Order as o join 
      OrderStatus as os on {os.pk} = {o.status} join 
      OrderEntry as oe on {oe.order} = {o.pk} join 
      Product as p on {oe.product} = {p.pk}
    }
  where 
    {os.code} = 'COMPLETED' and 
    {o.date} > '2020-08-16 00:00:00.000' and 
    {o.date} < '2020-09-30 00:00:00.000'
  group by {p.pk} 
  order by count({oe.pk}) desc
}})
Oisín
  • 81
  • 4
  • that was great thank you. but i am also adding the limit , wich was i am taking from another table can i include it it heare like .select {numberOfProducts} from {topsellingproductcomponent}. it returns the number(like 10).so i can limit the values wich are comming – mani sanakar Oct 06 '20 at 13:02
  • See my edited query above. Just needed a slight modification. Should do the trick – Oisín Oct 06 '20 at 13:52
  • Also, I don't think there's any way to do what you're asking with the {topsellingproductcomponent} value in the select statement. A groovy script would probably be the best way of achieving that. – Oisín Oct 06 '20 at 14:19