Last active
December 14, 2015 21:28
-
-
Save paneq/5151007 to your computer and use it in GitHub Desktop.
SQL optymalizacja
This file contains hidden or 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
Znajdź dla każdego ze studentów 1,2,3,4 osobno | |
taki ich rekord LPL, który został stworzony jako | |
ostatni przed dniem 2013-02-28 |
This file contains hidden or 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
SELECT lpl.* | |
FROM lesson_progress_levels AS lpl | |
LEFT JOIN lesson_progress_levels AS lpl_after | |
ON lpl_after.created_at > lpl.created_at AND | |
lpl_after.created_at < '2013-03-01 00:00' AND | |
lpl_after.student_id = lpl.student_id | |
WHERE lpl_after.id IS NULL AND | |
lpl.created_at < '2013-03-01 00:00' AND | |
lpl.student_id IN (1,2,3,4) |
This file contains hidden or 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
SELECT oute.* | |
FROM lesson_progress_levels AS oute | |
JOIN ( | |
SELECT student_id, lesson_id, max(created_at) as mm | |
FROM lesson_progress_levels AS lpl | |
WHERE lpl.created_at < '2013-03-01 00:00' AND | |
lpl.student_id IN (1,2,3,4) | |
GROUP BY lpl.student_id, lpl.lesson_id | |
) as ine ON | |
oute.student_id = ine.student_id AND | |
oute.lesson_id = ine.lesson_id AND | |
oute.created_at = ine.mm |
This file contains hidden or 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
SELECT student_id, lesson_id, max(id) as id | |
FROM lesson_progress_levels AS lpl | |
WHERE lpl.created_at < '2013-03-01 00:00' AND | |
lpl.student_id IN (1,2,3,4) | |
GROUP BY lpl.student_id, lpl.lesson_id |
This file contains hidden or 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
create_table "lesson_progress_levels", :force => true do |t| | |
t.integer "lesson_id" | |
t.integer "student_id" | |
t.integer "training_id" | |
t.decimal "level", :precision => 5, :scale => 2, :default => 0.0 | |
t.datetime "created_at" | |
t.datetime "updated_at" | |
end |
This file contains hidden or 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
SELECT student_id, lesson_id, max(created_at) as mm | |
FROM lesson_progress_levels AS lpl | |
WHERE lpl.created_at < '2013-03-01 00:00' AND | |
lpl.student_id IN (1,2,3,4) | |
GROUP BY lpl.student_id, lpl.lesson_id | |
HAVING mm = max(created_at) |
This file contains hidden or 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
SELECT lpl.* | |
FROM lessons_progress_level AS lpl | |
LEFT JOIN lessons_progress_level AS lpl_before | |
ON lpl_before.created_at < lpl.created_at AND | |
lpl_before.student_id = lpl.student_id | |
WHERE lpl_before.id IS NULL AND | |
lpl.created_at < '2013-02-28' AND | |
lpl.student_id IN (1,2,3,4) |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment