postgresql - How to combine multiple sql queries into single query? -


i working on spring boot framework postgressql. below example queries 1,2,...n. these queries working fine. have combine these queries single query because table having huge data can not run query multiple times(in loop) due performance issue. not getting idea combine these query.

1. select product_name, count(*) products product_name='pro_a1' , price between 20 , 30 group product_name;  2. select product_name, count(*) products product_name='pro_b1' , price between 50 , 70 group product_name; 

i want combine above queries 1 below unable achieve.

select product_name, count(*) products product_name in("pro_a1", "pro_b1", ...) , price between (20,30) , (50,70) , on. group product_name; 

any idea task. beginner level developer in spring , hibernate world. please suggest solution or helpful link.

thanks in advance

you can use conditional aggregation.

select product_name ,count(case when product_name='pro_a1' , price between 20 , 30 1 end) ,count(case when product_name='pro_b1' , price between 50 , 70 1 end)  products  group product_name; 

Comments

Popular posts from this blog

Command prompt result in label. Python 2.7 -

javascript - How do I use URL parameters to change link href on page? -

amazon web services - AWS Route53 Trying To Get Site To Resolve To www -