2013-03-07 53 views
5

我無法解析此查詢。Sql Query:無法對來自不同表格的數據進行分組

表是:

tblStandard1students 
tblStandard2students 
tblStandard3students  
tblCandidateinfo 

tblStandard1students,tblStandard2students和tblStandard3students TBL包含有關錄取標準1,2名學生和3

tblStandars1students

Candid admitted 
    1  Y 
    2  N 
    3  Y 


tblCandidateinfo 

Candid gender Division 
    1  M  1 
    2  F  2 

等信息.. 。

現在我想要桌子這樣

Gender Students(Standard1) Students(Standard2) Students(Standard3) 
------------------------------------------------------------------------ 
Male   10     20      30  
Female   10     30      40 

我試過,但這個並沒有給我的錯誤:

SELECT case when Gender='M' then 'Male' 
      when Gender='F' then 'Female' 
     END AS Gender, 

(SELECT count(*) 
FROM tblStandard1students A 
where A.Candid=B.Candid 
) AS Students(Standard1), 

(SELECT count(*) 
FROM tblStandard2students A 
    where A.Candid=B.Candid 
) AS Students(Standard2), 

(SELECT count(*) 
FROM tblStandard3students A 
    where A.Candid=B.Candid 
) AS Students(Standard3) 


FROM tblCandidateinfo B 
group by Gender 
+0

我沒有看到發佈的錯誤消息或使用了'ORDER BY'。你的意思是「GROUP」而不是「SORT」嗎? – 2013-03-07 07:07:50

+0

@ pst對不起!編輯了這個問題。 – user1274646 2013-03-07 07:11:54

回答

1

SQLFiddle demo

select 
case when Gender='M' then 'Male' 
      when Gender='F' then 'Female' 
     END AS Gender, 
sum(T.std1) as [Students(Standard1)], 
sum(T.std2) as [Students(Standard2)], 
sum(T.std3) as [Students(Standard3)] 
from 
tblCandidateinfo as C 

JOIN 
(
select Candid, 1 as std1, 0 as std2, 0 as std3 
from tblStandars1students 
union all 
select Candid, 0 as std1, 1 as std2, 0 as std3 
from tblStandars2students 
union all 
select Candid, 0 as std1, 0 as std2, 1 as std3 
from tblStandars3students 
) as T on (C.Candid=T.Candid) 

GROUP BY GENDER 
+0

我收到錯誤爲「不明確的列名'std1'。」 – user1274646 2013-03-07 09:24:08

+0

我已修復查詢 – valex 2013-03-07 10:12:26

+0

感謝它的工作! – user1274646 2013-03-07 11:16:08

0

在這種情況下,你也可以使用PIVOT操作

SELECT CASE WHEN Gender='M' then 'Male' 
      WHEN Gender='F' then 'Female' 
     END AS Gender, [Students(Standard1)], [Students(Standard2)], [Students(Standard3)] 
FROM (
     SELECT (SELECT gender FROM tblCandidateinfo i WHERE x.Candid = i.Candid) AS gender, List 
     FROM (
      SELECT Candid, 'Students(Standard1)' AS List 
      FROM dbo.tblStandard1students 
      UNION ALL 
      SELECT Candid, 'Students(Standard2)' 
      FROM dbo.tblStandard2students 
      UNION ALL 
      SELECT Candid, 'Students(Standard3)' 
      FROM dbo.tblStandard3students 
      ) x 
    ) x2 
PIVOT 
(
    COUNT(List) FOR List IN ([Students(Standard1)], [Students(Standard2)], [Students(Standard3)]) 
) p 

演示SQLFiddle

1

此查詢語法適用於Oracle。基本上,我使用了正確的外部連接。

select case when Gender='M' then 'Male' 
      when Gender='F' then 'Female' END AS Gender, 
     count(A.candid) AS "Students(Standard1)", 
     сount(A1.candid) AS "Students(Standard2)", 
     сount(A2.candid) AS "Students(Standard3)" 
from tblStandard1students A, 
    tblStandard2students A1, 
    tblStandard3students A2, 
    tblCandidateinfo B 
where A.candid(+)=B.candid and A1.candid(+)=B.candid and A2.candid(+)=B.candid 
group by gender 
+0

謝謝你的工作太...而且簡單也理解 – user1274646 2013-03-07 11:16:29

相關問題