The question is simple. How do i combine the following 2 sql queries into one.
I am using MYSQL v8.0.16 on the MYSQL Workbase UI.
SELECT Node_Type, COUNT(*) AS `count`
FROM node_table_vis
WHERE Node_SpinePlanID = "1"
GROUP BY Node_Type;
RETURNS
TYPE - COUNT
-----------
AGN 18
TJ 26
DSLAM 15
PON 18
CCJ 17
and
SELECT DISTINCT Node_SpinePlanID
FROM node_table_vis
WHERE Node_Exchange = "Exchange 1";
Thats returns
SpinePlanID
------------
1
5
10
So essentially what i want is query that looks like this?
SELECT Node_Type, COUNT(*) AS `count`
FROM node_table_vis
WHERE Node_SpinePlanID =
(
SELECT DISTINCT Node_SpinePlanID
FROM node_table_vis
WHERE Node_Exchange = "Exchange 1";
)
GROUP BY Node_Type;
So I get table that looks like
TYPE - 1 - 5 - 10
-----------------------
AGN 18 x y
TJ 26 x y
DSLAM 15 x y
PON 18 x y
CCJ 17 x y
So this is just throwing errors and not producing the goods. I was able to find the answer the top query, i was able to make the bottom, however i am unable to find an answer to combine both.
Any advice would be really appreciated.
UPDATE/EDIT
I have the following ...
SET @@group_concat_max_len = 10000;
SET @sql = null;
SELECT group_concat(distinct
concat(
'SUM(Node_SpinePlanID = ''',
Node_SpinePlanID,
''',) AS ',
Node_SpinePlanID
)
) INTO @sql
FROM node_table_vis;
SET @sql = CONCAT('SELECT Node_Type, ', @sql, ' FROM node_table_vis GROUP BY Node_Type');
PREPARE stmt FROM @sql;
EXECUTE stmt;
DEALLOCATE PREPARE stmt;
However the PREPARE stmt is not working? Getting error Code 1064 You have an SQL Error Syntax?
Any Advice on this one?