By using db<>fiddle, you agree to license everything you submit by Creative Commons CC0.
CREATE TABLE T(
cost int,
columnA varchar(50),
columnB varchar(50)
);
INSERT INTO T VALUES (250,'Foo','Bar');
INSERT INTO T VALUES (200,'Foo','Bar');
INSERT INTO T VALUES (150,'Bar','Bar');
INSERT INTO T VALUES (250,'Foo','Bar');
SELECT
SUM(cost),
(
SELECT tt.columnA
FROM T tt
GROUP BY tt.columnA
ORDER BY COUNT(*) DESC
LIMIT 1
) AS columnA_dominant,
(
SELECT tt.columnB
FROM T tt
GROUP BY tt.columnB
ORDER BY COUNT(*) DESC
LIMIT 1
) AS columnB_dominant,
(
SELECT COUNT(*)
FROM T tt
GROUP BY tt.columnA
ORDER BY COUNT(*) DESC
LIMIT 1
) / COUNT(*) AS columnA_percentage,
(
SELECT COUNT(*)
FROM T tt
GROUP BY tt.columnB
ORDER BY COUNT(*) DESC
LIMIT 1
) / COUNT(*) AS columnB_percentage
FROM T t1
SUM(cost) | columnA_dominant | columnB_dominant | columnA_percentage | columnB_percentage |
---|---|---|---|---|
850 | Foo | Bar | 0.7500 | 1.0000 |
SELECT SUM(cost) OVER(),
FIRST_VALUE(columnA) OVER (ORDER BY counter1 DESC) columnA_dominant,
FIRST_VALUE(columnB) OVER (ORDER BY counter2 DESC) columnB_dominant,
FIRST_VALUE(counter1) OVER (ORDER BY counter1 DESC) / COUNT(*) OVER() columnA_percentage,
FIRST_VALUE(counter2) OVER (ORDER BY counter2 DESC) / COUNT(*) OVER() columnB_percentage
FROM (
SELECT *,
COUNT(*) OVER (PARTITION BY columnA) counter1,
COUNT(*) OVER (PARTITION BY columnB) counter2
FROM T
) t1
LIMIT 1
SUM(cost) OVER() | columnA_dominant | columnB_dominant | columnA_percentage | columnB_percentage |
---|---|---|---|---|
850 | Foo | Bar | 0.7500 | 1.0000 |