By using db<>fiddle, you agree to license everything you submit by Creative Commons CC0.
CREATE TABLE bond(
secid INTEGER NOT NULL PRIMARY KEY
, secidtype integer
);
INSERT INTO bond(secid)
VALUES
(1)
, (2)
, (3)
, (4)
, (5)
, (6)
, (7)
, (8)
, (9)
, (10)
;
CREATE TABLE securities(
secid INTEGER NOT NULL PRIMARY KEY
);
INSERT INTO securities(secid)
VALUES
(1)
, (2)
, (3)
;
SELECT
coalesce(secidtype,1) secidtype
, count(DISTINCT secid) num_of
, (SELECT count(DISTINCT secid) FROM securities) other_table
, TRUNCATE (
CONCAT (
count(DISTINCT secid) / (
SELECT count(DISTINCT secid)
FROM securities
) * 100
, '%'
)
, 2
) AS Percentage
FROM bond
GROUP BY coalesce(secidtype,1);
secidtype | num_of | other_table | Percentage |
---|---|---|---|
1 | 10 | 3 | 333.33 |