This is the next version of Sum on distinct and group by
But It goes deeper
I have this :
CREATE TABLE active_connections(id serial PRIMARY KEY, source text NOT NULL, user_id int NOT NULL); INSERT INTO active_connections VALUES (0, 'web-mobile', 1); INSERT INTO active_connections VALUES (1, 'web-desktop', 1); INSERT INTO active_connections VALUES (2, 'web-desktop', 2); INSERT INTO active_connections VALUES (3, 'web-desktop', 3); INSERT INTO active_connections VALUES (4, 'web-desktop', 3); INSERT INTO active_connections VALUES (5, 'native', 3); INSERT INTO active_connections VALUES (6, 'native', 4);
What I’m trying to do is to group distinct user per
I would like to get
source user_count native 2 web 3
I’m able to do it only if I alias the
source column to something different. I don’t understand why
SELECT CASE WHEN source IN ('web-mobile', 'web-desktop') THEN 'web' ELSE source END AS source_2, COUNT(DISTINCT user_id) AS user_count FROM active_connections GROUP BY source_2;
First why I can say
END AS source? Second can I do it without using a subquery like below to be able to have
WITH mapped_source AS ( SELECT CASE WHEN source IN ('web-mobile', 'web-desktop') THEN 'web' ELSE source END AS web_or_native, COUNT(DISTINCT(user_id)) AS user_count FROM active_connections GROUP BY web_or_native ) SELECT web_or_native AS source, user_count FROM mapped_source;
Rextester : http://rextester.com/FRB66163
✓ Extra quality
ExtraProxies brings the best proxy quality for you with our private and reliable proxies
✓ Extra anonymity
Top level of anonymity and 100% safe proxies – this is what you get with every proxy package
✓ Extra speed
1,ooo mb/s proxy servers speed – we are way better than others – just enjoy our proxies!
USA proxy location
We offer premium quality USA private proxies – the most essential proxies you can ever want from USA
Our proxies have TOP level of anonymity + Elite quality, so you are always safe and secure with your proxies
Use your proxies as much as you want – we have no limits for data transfer and bandwidth, unlimited usage!
Superb fast proxy servers with 1,000 mb/s speed – sit back and enjoy your lightning fast private proxies!
99,9% servers uptime
Alive and working proxies all the time – we are taking care of our servers so you can use them without any problems
No usage restrictions
You have freedom to use your proxies with every software, browser or website you want without restrictions
Perfect for SEO
We are 100% friendly with all SEO tasks as well as internet marketing – feel the power with our proxies
Buy more proxies and get better price – we offer various proxy packages with great deals and discounts
We are working 24/7 to bring the best proxy experience for you – we are glad to help and assist you!