postgresql - Postgres how to maintain order of rows using CTEs -


i have 2 tables

students:        id   | name | age       1      abc   20       2      xyz   21  scores:     id | studentid | marks     1       1        20     2       2        22     3       2        20     4       1        22     5       1        20 

where studentid foreign key students table

when

select studentid  scores  marks=20; 

i following result 1, 2, 1

but if want name of student name , when join using

 select t1.name   students t1   inner join scores t2 on t1.id = t2.studentid   t2.marks=20;  

i xyz,abc,abc though ouput correct there way can maintain order in scores listed in scores table? should abc,xyz,abc output. tried using subquery

 select name   students   id in ( select studentid scores marks=20) ; 

but did not give me correct order. how can achieved using ctes (common table expressions)? tried follownig cte did not work

with cte as(     select t2.id, t1.name       students t1       inner join scores t2 on t1.id = t2.studentid       t2.marks=20) select name cte order id 

you can order column not present in select list:

select t1.name  students t1  inner join scores t2 on t1.id = t2.student_id  t2.marks=20 order t2.id;    name  ------  abc  xyz  abc (3 rows) 

Comments

Popular posts from this blog

php - How to add and update images or image url in Volusion using Volusion API -

javascript - jQuery UI Splitter/Resizable for unlimited amount of columns -

javascript - IE9 error '$'is not defined -