-
Notifications
You must be signed in to change notification settings - Fork 0
/
badges-by-district.sql
37 lines (33 loc) · 1.27 KB
/
badges-by-district.sql
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
.mode csv
.separator \t
.import scoutbook.tsv sb
.import scoutnet.tsv sn
.separator ,
CREATE VIEW mbc as select sb.BSAMemberID as MID, sb.Districts, sb.'Merit Badges' as mbs, sn.organizations from sb left join sn on sb.BSAMemberID=sn.memberid;
update sb set "Merit Badges"=replace("Merit Badges",'Signs, Signals, and Codes', 'Signs Signals and Codes');
update sb set "Merit Badges"=replace("Merit Badges",', ', '|');
update sb set "Merit Badges"=replace("Merit Badges",'Signs Signals and Codes','Signs, Signals, and Codes');
.mode list
select '| District | Merit Badge | Count |';
select '| --- | --- | ---: |';
WITH RECURSIVE splitdistrict(district, mbs, rest) AS (
SELECT '', mbs, organizations || ',' FROM mbc
UNION ALL
SELECT ltrim(substr(rest, 0, instr(rest, ','))),
mbs,
substr(rest, instr(rest, ',')+1)
FROM splitdistrict
WHERE rest <> ''),
splitmb(district, mb, rest) AS (
SELECT district, '', mbs || '|' FROM splitdistrict
UNION ALL
SELECT district,
substr(rest, 0, instr(rest, '|')),
substr(rest, instr(rest, '|')+1)
FROM splitmb
WHERE rest <> '')
SELECT '',district, mb, count(*),''
FROM splitmb
WHERE mb <> '' AND district <> ''
GROUP BY district, mb
ORDER BY district, mb;