Skip to content
Advertisement

calculating percentage on multiple sql tables

There are 2 tables mobile and web

Table name: Mobile

Table name: web

I need to calculate percentage of users who only visited : mobile, web and both. That is, the percentage of users who are only in the mobile table, only in the web table and in both tables. The sum should be 1.

I tried using union but I am unsure if it is the right way. How can I calculate the percentage?

Advertisement

Answer

First get all the rows, including a column indicating where the row comes from, of both tables with UNION ALL.
Then group by user to get if the user is in only 1 table or both.
Finally use conditional aggregation to get the percentages:

See the demo.
Results:

User contributions licensed under: CC BY-SA
5 People found this is helpful
Advertisement