简体   繁体   中英

How to merge two sql queries results? One query has GROUP BY

I have a table like below

id, date, type, quantity, vendor
'1','2020-04-05','2424A','200','vendor1'

'2','2020-04-05','2424','350','vendor1'

'3','2020-04-05','2424A1','150','vendor1'

'4','2020-04-05','2425','400','vendor1'

'5','2020-04-05','MA5878','200','vendor2'

I am using Java as my backend. I have tried these queries

  1. SELECT vendor, type, quantity FROM reports;
vendor, type, quantity
'vendor1', '2511', '200'

'vendor1', '5120', '350'

'vendor1', '2520', '150'

'vendor1', '5114', '400'
  1. SELECT vendor, SUM(quantity) FROM reports where date = '2020-04-05' GROUP BY vendor;
vendor, SUM(quantity)
'vendor1', '1100'

'vendor2', '20600'

I need to combine the above 2 queries results. Can someone guide me to proceed further.

Give this a try:

WITH a AS (
    SELECT vendor, ont_type, quantity 
    FROM cpe_portal.ontfereports
), b as (
    SELECT vendor, SUM(quantity) as quantityb 
    FROM cpe_portal.ontfereports 
    WHERE date = '2020-04-05' 
    GROUP BY vendor
)
SELECT a.vendor, a.ont_type, a.quantity, b.quantityb
FROM a join b ON a.vendor = b.vendor

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM