交叉连接两个表

时间:2012-11-15 20:15:56

标签: sql

我有一个看起来像这样的表,让我们调用这个表B

id    boardid    schoolid     subject     cnt1   cnt2  cnt3 ....
=================================================================
1       20         21           f     
2       20         21           r
3       20         21           w
4       20         21           m
5       20         30           r
6       20         30           w
7       20         30           m

假设计数只是整数。请注意subject = f没有schoolid = 30。同样,对于大多数学校来说,存在一些subject剂量。您的schoolid可能只有r, w或某些只有r, m, f ..

所以我想要做的是每个学校有4个一致的行,而dosnt存在的行我想要虚拟值。我考虑过创建一个辅助表

drop table #A
Select * into #A FROM 
(
select [subject_s] = 'r', orderNo = 1
union all
select [subject_s] = 'w', orderNo = 2
union all
select [subject_s] = 'm', orderNo = 3
union all
select [subject_s] = 'f', orderNo = 4
) z

并加入了他们,但我没有在哪里。我尝试了内连接,左外连接,交叉连接,一切。我甚至试图制作笛卡尔积。我认为我的笛卡尔产品搞砸了,因为我在那里有orderno所以它在主表中每行产生16行。实际输入这个,我意识到如果我删除orderno,应用笛卡尔积,然后在稍后添加orderno,它可能会有效但我有兴趣看看你们可以想出什么。我很难过。

最终结果

id    boardid    schoolid     subject     cnt1   cnt2  cnt3 ....
=================================================================
1       20         21           r     
2       20         21           w
3       20         21           m
4       20         21           f
5       20         30           r
6       20         30           w
7       20         30           m
7       20         30           f

3 个答案:

答案 0 :(得分:1)

尝试以下方法:

SELECT S.boardid, S.schoolid, A.[subject], B.cnt1, B.cnt2, B.cnt3
FROM (SELECT DISTINCT boardid, schoolid FROM YourTable) S
CROSS JOIN #A A
LEFT JOIN YourTable B
    ON B.boardid = S.boardid AND  B.schoolid = S.schoolid 
    AND A.[subject] = B.[subject]

答案 1 :(得分:0)

由于我不知道您使用的是哪个RDBMS,因此我使用sqlite和更简单的表格尝试了以下内容:

sqlite> create table schools (name varchar, subject varchar, teacher varchar);

sqlite> select * from schools;
School1|Maths|Mr Smith
School2|English|Jack
School3|English|Jimmy
School3|Maths|Jane
School4|Computer Science|Bob

sqlite> select
            schoolnames.name, 
            subjects.subject, 
            ifnull(teachers.teacher, "Unknown") 
        from (select distinct name from schools) schoolnames
        join (select distinct subject from schools) subjects
        left join schools teachers
           on schoolnames.name = teachers.name
              and subjects.subject = teachers.subject;

School1|Maths|Mr Smith
School1|English|Unknown
School1|Computer Science|Unknown

School2|Maths|Unknown
School2|English|Jack
School2|Computer Science|Unknown

School3|Maths|Jane
School3|English|Jimmy
School3|Computer Science|Unknown

School4|Maths|Unknown
School4|English|Unknown
School4|Computer Science|Bob

答案 2 :(得分:-1)

我会用:

SELECT 
    boardid, schoolid, dist_subject, id, cnt1, ...
FROM
    (SELECT 
        boardid, schoolid, dist_subject
    FROM
        (SELECT 
            DISTINCT subject AS dist_subject 
        FROM b ) s full outer join
        (SELECT 
            boardid, schoolid 
        FROM b 
        GROUP BY 
            boardid, schoolid   ) g ) sg LEFT OUTER JOIN
    b ON 
    sg.boardID = b.boardID AND
    sg.schoolid = b.schoolID
    sg.dist_subject = b.subject