oracle连接查询问题

时间:2012-05-28 10:24:40

标签: sql oracle oracle10g

我想在两张桌子下面加入

学生表:

CREATE TABLE  "STUDENT" 
   (    "STUDENT_ID" NUMBER(5,0), 
    "STUDENT_NAME" VARCHAR2(20), 
     CONSTRAINT "STUDENT_PK" PRIMARY KEY ("STUDENT_ID") ENABLE
   )
/

数据

    SID STUDENT_NAME        
-   101 Rao         
-   102 G.Naidu                 
-   103 D.Sujith                
-   104 V.S.Raja                
-   105 R.Johnny                
-   106 V.Hemanth               
-   107 Ram                    
-   108 Aditya                 
-   109 bharat                 
-   110 Ramesh   

ATTENDANCE表:

CREATE TABLE  "ATTENDANCE" 
   (    "DAY" DATE, 
    "SID" NUMBER(5,0), 
    "PRESENT" NUMBER(5,0), 
     PRIMARY KEY ("SID", "DAY") ENABLE, 
     FOREIGN KEY ("SID")
      REFERENCES  "STUDENT" ("STUDENT_ID") ENABLE
   )
/

数据

-   ##DAY##     ##SID## ##PRESENT##
-   28-MAY-12   101     1
-   28-MAY-12   102     1
-   28-MAY-12   103     0
-   28-MAY-12   104     1
-   28-MAY-12   105     0
-   29-MAY-12   101     0
-   29-MAY-12   102     1
-   29-MAY-12   103     1
-   29-MAY-12   104     1
-   29-MAY-12   105     0

select sid,count(sid) from attendance where present=1 group by sid order by sid;

-   SID COUNT(SID)
-   101 2
-   102 2
-   104 2
-   105 1
-   106 1
-   107 2
-   108 2
-   109 2
-   111 2
-   112 1

我希望结果为...............

-   SID STUDENT_NAME        COUNT
-   101 Rao                     2
-   102 G.Naidu                 1
-   103 D.Sujith                2
-   104 V.S.Raja                1
-   105 R.Johnny                2
-   106 V.Hemanth               1
-   107 Ram                     2
-   108 Aditya                  2
-   109 bharat                  1
-   110 Ramesh                  2

1 个答案:

答案 0 :(得分:0)

你只需要加入sutend表:

  select sid, STUDENT_NAME name, count(sid) 
    from attendance 
         join student on STUDENT_ID= sid 
   where present=1 
group by sid, STUDENT_NAME 
order by sid;