0

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 Apr 17, 2020 at 2:06
3
  • If you need overall sum only - simply remove GROUP BY and excess field(s) from output. If you need overall sum in addition to separate sums use GROUP BY WITH ROLLUP. Commented Apr 17, 2020 at 5:06
  • What did I code wrong here? The problem is not related to this query - server starts the citated query fragment from the char where the problem was found, so the problematic point is posessed before the keyword SELECT. I think you try to use multi-query with the method which does not allow this. Commented Apr 17, 2020 at 5:09
  • Thank you Akina. Based on your input, I was able to fix my issue. The solution was simpler than what I originally designed. SELECT SUM(quantity) AS 'Total' FROM T3101708 WHERE outputfilereceived IS NULL AND (SELECT * FROM 5YYAccounts WHERE 5YYAccounts.accountid = T3101708.accountid) IS NOT NULL; This was the solution that worked. Thank you. Bobby Commented Apr 17, 2020 at 15:43

1 Answer 1

0
SELECT 
 DISTINCT a.label
 ,(SELECT SUM(stat_column)/total_stat_col_records FROM table_a AS b WHERE b.label=a.label) AS frequency 
FROM table_a AS a 
ORDER BE frequency DESC;

Where you want the unique distinct label and the sum of the stat_column and the total is the number of stat_column records. This gets the percent based on number of records.

Rohit Gupta
2,1248 gold badges20 silver badges25 bronze badges
answered Sep 9, 2024 at 21:28

Your Answer

Draft saved
Draft discarded

Sign up or log in

Sign up using Google
Sign up using Email and Password

Post as a guest

Required, but never shown

Post as a guest

Required, but never shown

By clicking "Post Your Answer", you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.