How can I aggregate this SQL table into another table?

Freakishly

If i have the data table below:

COLUMN 1    COLUMN2
Country1    NULL
Country1    0
Country1    NULL
Country1    0
Country1    NULL
Country1    0
Country1    NULL
Country1    18
Country1    NULL
Country1    12
Country1    NULL
Country1    0
Country1    NULL
Country1    0
Country1    NULL
Country2    0
Country2    NULL
Country2    7
Country2    NULL
Country2    0
Country2    NULL
Country2    0
Country2    NULL
Country2    0

Ignoring the rows with second column as NULL, how can I get the following values out of it:

Country1 has 7 values, out of which 5 are zero, 1 is 18 and 1 is 12. Country2 has 5 values, out of which 4 are zero, 1 is 7

How can I get a table that looks like this?

COUNTRY VALUE   PERCENT
Country1    0   71.43 (i.e. 5/7)
Country1    18  14.29 (i.e. 1/7)
Country1    12  14.29 (i.e. 1/7)
Country2    0   80 (i.e. 4/5)
Country2    7   20 (i.e. 1/5)
Gordon Linoff

You can do this using simple aggregation and window functions:

select country, value,
       cast(count(*) as float) / sum(count(value)) over (partition by country) as ratio
from table t
group by country, value;

This gives the third column as a ratio. You can format it as a percent in various ways, such as by using str().

Collected from the Internet

Please contact [email protected] to delete if infringement.

edited at
0

Comments

0 comments
Login to comment

Related

How can i write sql code to bring values from another table and use count function?

How can I aggregate metrics per day in a Grafana table?

How can I order query by another table?

In my SQL query, how can I have a column with values queried from another table?

How can I update this table by SQL?

How can I update a value in a table using a condition on values in another table in Sql server

How can I create SQL that does a Left Outer Join and also a count from another table?

How can I calculate dates from another column and another table?

How can I parse data from one column in a table and put the result into another with SQL Server?

How can I union with another table linq

how can i get record in another table (SQL)

How can I reshape a table in SQL Server?

Can I group-aggregate-calculate from another table directly in razor view?

Can I use a WITH table AS inside another WITH table AS?

How can I make SQL automatically copy certain rows in a table to another table in the same Database?

SQL: How can I pick a cell value from one table as a condition to select another table

how can I summarize occurence from another table in a pbi table

MS Access SQL – How can I count the occurrence of a number from one table in strings of another table

How can I aggregate one column in one table after applying a filter based on a column in another table in Power BI?

How can I create a query in SQL Server, using as base table a date function and linking it to another table?

R - How can i create graphs using an Aggregate data table?

How can I insert into from one table to another with autoincrement in SQL Server

how can I display a SQL table in flask?

How can i insert into table result from another sql statement that returns 2 rows

How can I insert rows from one table to another in PL SQL using the cursor for loop?

i would like to aggregate a table in sql

How can i aggregate text from one table by array of id's in another?

Create table with aggregate data from another table, using SQL

How can I write a SQL select query where condition depends upon another table?