Sample Header Ad - 728x90

Select SUM from another Select SUM result in mySQL

0 votes
1 answer
1224 views
I have one Select SUM command that works: # SIM Orders Pending by Customers for 3101708 IMSI SIMs with 5YY MSISDNs SELECT accountname AS 'Account Name', IF ((SELECT * FROM 5YYAccounts WHERE 5YYAccounts.accountid = T3101708.accountid) IS NULL , 'N','Y') AS '5YY MSISDN', LPAD(CONCAT(FORMAT(SUM(quantity), 0)),15,' ') AS 'Quantity' FROM T3101708 WHERE outputfilereceived IS NULL AND (SELECT * FROM 5YYAccounts WHERE 5YYAccounts.accountid = T3101708.accountid) IS NOT NULL GROUP BY accountname, accountid ORDER BY SUM(quantity) DESC; After running this, I get following result: +---------------------------------+------------+-----------------+ | Account Name | 5YY MSISDN | Quantity | +---------------------------------+------------+-----------------+ | FCA - SXM - AT&T | Y | 48,000 | | Numerex - AT&T | Y | 34,000 | | Mytrex Inc. - AT&T | Y | 24,000 | | Honda US - AT&T | Y | 18,000 | +---------------------------------+------------+-----------------+ 3 rows in set (0.03 sec) Based on the result above, I want to SUM the last column by having following Select SUM command: SELECT SUM( SELECT SUM(quantity) FROM T3101708 WHERE outputfilereceived IS NULL AND (SELECT * FROM 5YYAccounts WHERE 5YYAccounts.accountid = T3101708.accountid) IS NOT NULL GROUP BY accountname, accountid ); For this time, I am getting an error in mySQL: > ERROR 1064 (42000): You have an error in your SQL syntax; check the > manual that corresponds to your MySQL server version for the right > syntax to use near 'SELECT SUM(quantity) > FROM T3101708 > WHERE outputfilereceived IS NULL AND' at line 2 What did I code wrong here? Thank you. Bobby
Asked by Bobby Richardson (1 rep)
Apr 17, 2020, 02:06 AM
Last activity: Jul 5, 2025, 06:09 PM