Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- state_demo2=# select * from subtest_registration where sid='635340410';
- id | sid | subtest_id | create_date | modified_date
- --------------------------------------+-----------+--------------------------------------+----------------------------+----------------------------
- 45a0c920-3938-11e0-a84e-ff2c4691c4a2 | 635340410 | e696b230-10e0-11df-80f4-1a7fbecf21f7 | 2011-02-15 13:17:47.02386 | 2011-02-15 13:17:47.02386
- 45a16984-3938-11e0-a84e-ff2c4691c4a2 | 635340410 | e6982d22-10e0-11df-80f4-1a7fbecf21f7 | 2011-02-15 13:17:47.027687 | 2011-02-15 13:17:47.027687
- 45a1c42e-3938-11e0-a84e-ff2c4691c4a2 | 635340410 | e6994f9a-10e0-11df-80f4-1a7fbecf21f7 | 2011-02-15 13:17:47.029989 | 2011-02-15 13:17:47.029989
- 45a21c6c-3938-11e0-a84e-ff2c4691c4a2 | 635340410 | e69a7f8c-10e0-11df-80f4-1a7fbecf21f7 | 2011-02-15 13:17:47.032258 | 2011-02-15 13:17:47.032258
- 45a27eb4-3938-11e0-a84e-ff2c4691c4a2 | 635340410 | e69b9674-10e0-11df-80f4-1a7fbecf21f7 | 2011-02-15 13:17:47.034803 | 2011-02-15 13:17:47.034803
- 45a2e016-3938-11e0-a84e-ff2c4691c4a2 | 635340410 | e69cb7b6-10e0-11df-80f4-1a7fbecf21f7 | 2011-02-15 13:17:47.037245 | 2011-02-15 13:17:47.037245
- 45a344ca-3938-11e0-a84e-ff2c4691c4a2 | 635340410 | e69dbdaa-10e0-11df-80f4-1a7fbecf21f7 | 2011-02-15 13:17:47.039853 | 2011-02-15 13:17:47.039853
- 45a3a9c4-3938-11e0-a84e-ff2c4691c4a2 | 635340410 | e69ef03a-10e0-11df-80f4-1a7fbecf21f7 | 2011-02-15 13:17:47.042446 | 2011-02-15 13:17:47.042446
- 45a403d8-3938-11e0-a84e-ff2c4691c4a2 | 635340410 | e6a02c8e-10e0-11df-80f4-1a7fbecf21f7 | 2011-02-15 13:17:47.044716 | 2011-02-15 13:17:47.044716
- 45a46472-3938-11e0-a84e-ff2c4691c4a2 | 635340410 | e6a129cc-10e0-11df-80f4-1a7fbecf21f7 | 2011-02-15 13:17:47.04769 | 2011-02-15 13:17:47.04769
- 45a4d25e-3938-11e0-a84e-ff2c4691c4a2 | 635340410 | e6a23a38-10e0-11df-80f4-1a7fbecf21f7 | 2011-02-15 13:17:47.050039 | 2011-02-15 13:17:47.050039
- 45a53230-3938-11e0-a84e-ff2c4691c4a2 | 635340410 | e6a35a58-10e0-11df-80f4-1a7fbecf21f7 | 2011-02-15 13:17:47.052472 | 2011-02-15 13:17:47.052472
- 45a58fa0-3938-11e0-a84e-ff2c4691c4a2 | 635340410 | e6a48680-10e0-11df-80f4-1a7fbecf21f7 | 2011-02-15 13:17:47.054859 | 2011-02-15 13:17:47.054859
- 45a5f134-3938-11e0-a84e-ff2c4691c4a2 | 635340410 | e6a5bf8c-10e0-11df-80f4-1a7fbecf21f7 | 2011-02-15 13:17:47.05736 | 2011-02-15 13:17:47.05736
- 45a65070-3938-11e0-a84e-ff2c4691c4a2 | 635340410 | e6a6e268-10e0-11df-80f4-1a7fbecf21f7 | 2011-02-15 13:17:47.059806 | 2011-02-15 13:17:47.059806
- 45a6b326-3938-11e0-a84e-ff2c4691c4a2 | 635340410 | e6a7e8ca-10e0-11df-80f4-1a7fbecf21f7 | 2011-02-15 13:17:47.062346 | 2011-02-15 13:17:47.062346
- 45a70e7a-3938-11e0-a84e-ff2c4691c4a2 | 635340410 | e6a91de4-10e0-11df-80f4-1a7fbecf21f7 | 2011-02-15 13:17:47.064709 | 2011-02-15 13:17:47.064709
- 45a76cee-3938-11e0-a84e-ff2c4691c4a2 | 635340410 | e6aa3b02-10e0-11df-80f4-1a7fbecf21f7 | 2011-02-15 13:17:47.067085 | 2011-02-15 13:17:47.067085
- 45a7c9a0-3938-11e0-a84e-ff2c4691c4a2 | 635340410 | e6ab4f92-10e0-11df-80f4-1a7fbecf21f7 | 2011-02-15 13:17:47.069467 | 2011-02-15 13:17:47.069467
- 45a8294a-3938-11e0-a84e-ff2c4691c4a2 | 635340410 | e6ac5acc-10e0-11df-80f4-1a7fbecf21f7 | 2011-02-15 13:17:47.07191 | 2011-02-15 13:17:47.07191
- 45a885ac-3938-11e0-a84e-ff2c4691c4a2 | 635340410 | e6ad6318-10e0-11df-80f4-1a7fbecf21f7 | 2011-02-15 13:17:47.074263 | 2011-02-15 13:17:47.074263
- 45a8dd5e-3938-11e0-a84e-ff2c4691c4a2 | 635340410 | e6ae8cfc-10e0-11df-80f4-1a7fbecf21f7 | 2011-02-15 13:17:47.077227 | 2011-02-15 13:17:47.077227
- 45a95b44-3938-11e0-a84e-ff2c4691c4a2 | 635340410 | e6af8ff8-10e0-11df-80f4-1a7fbecf21f7 | 2011-02-15 13:17:47.079699 | 2011-02-15 13:17:47.079699
- 45a9b4e0-3938-11e0-a84e-ff2c4691c4a2 | 635340410 | e6b09952-10e0-11df-80f4-1a7fbecf21f7 | 2011-02-15 13:17:47.082032 | 2011-02-15 13:17:47.082032
- 45aa1034-3938-11e0-a84e-ff2c4691c4a2 | 635340410 | e6b84e72-10e0-11df-80f4-1a7fbecf21f7 | 2011-02-15 13:17:47.0844 | 2011-02-15 13:17:47.0844
- 45aa6dd6-3938-11e0-a84e-ff2c4691c4a2 | 635340410 | e6b97e82-10e0-11df-80f4-1a7fbecf21f7 | 2011-02-15 13:17:47.086802 | 2011-02-15 13:17:47.086802
- 45aadcc6-3938-11e0-a84e-ff2c4691c4a2 | 635340410 | e6ba7e68-10e0-11df-80f4-1a7fbecf21f7 | 2011-02-15 13:17:47.089612 | 2011-02-15 13:17:47.089612
- 45ab3c3e-3938-11e0-a84e-ff2c4691c4a2 | 635340410 | e6bba6bc-10e0-11df-80f4-1a7fbecf21f7 | 2011-02-15 13:17:47.092028 | 2011-02-15 13:17:47.092028
- 45ab9508-3938-11e0-a84e-ff2c4691c4a2 | 635340410 | e6bcd6cc-10e0-11df-80f4-1a7fbecf21f7 | 2011-02-15 13:17:47.094316 | 2011-02-15 13:17:47.094316
- 45abf35e-3938-11e0-a84e-ff2c4691c4a2 | 635340410 | e6bde206-10e0-11df-80f4-1a7fbecf21f7 | 2011-02-15 13:17:47.096736 | 2011-02-15 13:17:47.096736
- 45ac4b1a-3938-11e0-a84e-ff2c4691c4a2 | 635340410 | e6bf0ac8-10e0-11df-80f4-1a7fbecf21f7 | 2011-02-15 13:17:47.099908 | 2011-02-15 13:17:47.099908
- (31 rows)
- Taking one of those:
- state_demo2=# select * from subtest_registration_result where subtest_registration_id ='45a0c920-3938-11e0-a84e-ff2c4691c4a2';
- subtest_registration_id | score_type_id | score | score_description
- --------------------------------------+---------------+-------+-------------------
- 45a0c920-3938-11e0-a84e-ff2c4691c4a2 | 1 | 1 |
- 45a0c920-3938-11e0-a84e-ff2c4691c4a2 | 2 | | W
- (2 rows)
- Running that WITH() query:
- WITH totals AS
- (
- SELECT score_description,
- COUNT( sid) AS student_count,
- ( SELECT COUNT( sid) FROM subtest_registration ) AS total_students
- FROM subtest_registration_result
- INNER JOIN subtest_registration ON subtest_registration_id=id
- WHERE score_type_id = 2
- GROUP BY score_description, score
- )
- SELECT score_description, student_count,
- total_students,
- ( student_count * 100.00 ) / total_students as percentage
- FROM totals
- bsd:state afra$ time postgres state_demo2 < g1.sql
- score_description | student_count | total_students | percentage
- -------------------+---------------+----------------+---------------------
- M | 7803 | 40321 | 19.3521986061853625
- B | 7996 | 40321 | 19.8308573696088887
- E | 14596 | 40321 | 36.1994990203615982
- W | 9926 | 40321 | 24.6174450038441507
- (4 rows)
- This seems correct.
- But 40321 students did not take test. 40321 is the number of entries in subtest_registration but a student can have multiple entries (one for each subtest).
- state_demo2=# select count(distinct sid) from subtest_registration;
- count
- -------
- 1193
- (1 row)
- Do I do:
- COUNT(DISTINCT sid) AS student_count,
- But then the values are whacked:
- WITH totals AS
- (
- SELECT score_description,
- COUNT( distinct sid) AS student_count,
- ( SELECT COUNT( distinct sid) FROM subtest_registration ) AS total_students
- FROM subtest_registration_result
- INNER JOIN subtest_registration ON subtest_registration_id=id
- WHERE score_type_id = 2
- GROUP BY score_description, score
- )
- SELECT score_description, student_count,
- total_students,
- ( student_count * 100.00 ) / total_students as percentage
- FROM totals
- bsd:state afra$ time postgres state_demo2 < g1.sql
- score_description | student_count | total_students | percentage
- -------------------+---------------+----------------+----------------------
- B | 1193 | 1193 | 100.0000000000000000
- E | 1142 | 1193 | 95.7250628667225482
- M | 1163 | 1193 | 97.4853310980720872
- W | 1091 | 1193 | 91.4501257334450964
- (4 rows)
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement