Reputation: 389
I have the following table
A Home Away
D Lisa Jill
D Jack Andre
C Jack Kirk
C Jane Jill
and want to get the following result
A Home Away Count
D Lisa Jill 1
D Jack Andre 2
C Jack Kirk 2
C Jane Jill 1
is there a way to do so in sql?
Upvotes: 0
Views: 23
Reputation: 13049
Use a window function:
select
"A", "Home", "Away",
count(*) over (partition by "Home") as "Count"
from _table;
Upvotes: 2
Reputation: 2143
You can try something like that:
WITH table_counts AS (
SELECT
Home,
count(*) as table_count
FROM your_table
GROUP BY Home
)
SELECT
t1.A,
t1.Home,
t1.Away,
t2.table_count as Count
FROM your_table t1
JOIN table_counts t2 ON t2.Home = t1.Home;
Upvotes: 0