Created
October 29, 2018 15:59
-
-
Save richlv/6acd4fb266b3e4d9827033d85ec120de to your computer and use it in GitHub Desktop.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Tables: | |
thing | |
thingid, thingname | |
10, athing | |
11, bthing | |
12, cthing | |
entity | |
entityid, entityname, thingid | |
100, someentity, 10 | |
101, someentity, 11 | |
102, someentity, 12 | |
103, someotherentity, 12 | |
value | |
valueid, entityid, value | |
1000, 100, 13 | |
1001, 100, 14 | |
1002, 102, 33 | |
1003, 103, 55 | |
1004, 102, 55 | |
1005, 101, 33 | |
Getting the top entities by the number of values: | |
select t.thingname,e.entityname,v.entityid,count(*) from value v | |
left join entity e on v.entityid=e.entityid | |
left join thing t on t.thingid=e.thingid | |
group by v.entityid order by count(*) desc; | |
athing, someentity, 100, 2 | |
cthing, someentity, 102, 2 | |
bthing, someentity, 101, 1 | |
cthing, someotherentity, 103, 1 | |
How to get the top things by their corresponding number of values? | |
cthing, 3 | |
athing, 2 | |
bthing, 1 | |
Creating test setup: | |
create database thingtest; | |
use thingtest; | |
create table thing (thingid int, thingname varchar(32)); | |
create table entity (entityid int, entityname varchar(32), thingid int); | |
create table value (valueid int, entityid int, value int); | |
insert into thing values (10,'athing'),(11,'bthing'),(12,'cthing'); | |
insert into entity values (100,'someentity',10),(101,'someentity',11),(102,'someentity',12),(103,'someotherentity',12); | |
insert into value values (1000,100,13),(1001,100,14),(1002,102,33),(1003,103,55),(1004,102,55),(1005,101,33); |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment