简体   繁体   中英

group rows with equal data in one column and summing its related data in another column [ORACLE SQL]

ORACLE SQL

i have a table CUSTOMER_UNITS

CUSTOMER_ID    UNITS_SOLD
AAAA    350
BBBB    150
CCCC    650
DDDD    250
AAAA    950
BBBB    450
CCCC    450
DDDD    350

i need to write a query that will SUM(UNITS_SOLD) per CUSTOMER_ID, WHERE UNITS_SOLD >= 350 and give me a total. so basically the results should be:

CUSTOMER_ID    UNITS_SOLD
AAAA    1300
BBBB    450
CCCC    1100
DDDD    350

I tired the following code:

SELECT
    CUSTOMER_ID
   ,SUM(UNITS_SOLD)
FROM
    (SELECT DISTINCT CUSTOMER_ID, UNITS_SOLD FROM CUSTOMER_UNITS)
WHERE UNITS_SOLD >= 350
GROUP BY CUSTOMER_ID;

but it returns an error; ORA-00904: "UNITS_SOLD": invalid identifier

i'm sorry for asking this i know it must be super simple but i've tried at least 15 suggestions with no luck..

this is for select the valuue with more of 350 using having ..

select CUSTOMER_ID,  sum(UNITS_SOLD) 
from CUSTOMER_UNITS
group by CUSTOMER_ID
having sum(UNITS_SOLD) >= 350 ;

I have no idea what i was thinking.. The following returned what i wanted:

SELECT
    CUSTOMER_ID
   ,SUM(UNITS_SOLD)
FROM
    CUSTOMER_UNITS
WHERE
    UNITS_SOLD >= 350
GROUP BY
    CUSTOMER_ID;

Thanks for the suggestions. I guess sometimes you go the difficult way when it's actually really simple

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