English 中文(简体)
创建一个完整的多个 2 - 多个矩阵
原标题:create a full many-2-many matrix

I have a 2 tables with a many-2-many relationship:
tableA (ida, valA)
tableB (idb, valB)
tableAB (ida, idb)

I need one query that should return in every tupple:
(ida, idb, 0/1 if related or not)

Here is some input / output example: tableA

ida   valA
1     b
2     s
3     l

<强 > 表格B

idb   valB
11    aaa
22    bbb
33    ccc

<强度表AB

ida   idb
1     11
2     33

预期成果:

ida      idb      is_exists
1        11       1
2        11       0
1        33       0
2        33       1

Note that I don t need permutations which are always 0, for example ida=3 or idb=22
this is because it means that in a matrix the whole row or column is 0 (this shows that the row has no relation whatsoever with the other table)

    ida |     |     |  
idb     |  1  |  2  |  3
---------------------------
11       |  1  |  0  |  0
---------------------------
22       |  0  |  0  |  0
---------------------------
33       |  0  |  1  |  0
最佳回答

您需要的是一张有各种可能配对的驾驶表。 一种方法是从表A和表B获得这个, 使用交叉连接 :

select allAB.aid, allAB.bid, max(case when ab.aid is not null then 1 else 0 end) as HasPair
from (select distinct a.id as aid, b.id as bid
      from TableA a cross join
           TableB b
     ) as allAB left outer join
     TableAB ab
     on allAB.aid = ab.aid and
        allAB.bid = ab.bid
group by allAB.aid, allAB.bid

在此之后,查询只是总结和确定表AB中是否有相应的记录。

问题回答

暂无回答




相关问题
Export tables from SQL Server to be imported to Oracle 10g

I m trying to export some tables from SQL Server 2005 and then create those tables and populate them in Oracle. I have about 10 tables, varying from 4 columns up to 25. I m not using any constraints/...

Connecting to Oracle 10g with ODBC from Excel VBA

The following code works. the connection opens fine but recordset.recordCount always returns -1 when there is data in the table. ANd If I try to call any methods/properties on recordset it crashes ...

How to make a one to one left outer join?

I was wondering, is there a way to make a kind of one to one left outer join: I need a join that matches say table A with table B, for each record on table A it must search for its pair on table B, ...

Insert if not exists Oracle

I need to be able to run an Oracle query which goes to insert a number of rows, but it also checks to see if a primary key exists and if it does, then it skips that insert. Something like: INSERT ALL ...

How can I store NULLs in NOT NULL field?

I just came across NULL values in NOT-NULL fields in our test database. How could they get there? I know that NOT-NULL constraints can be altered with NOVALIDATE clause, but that would change table s ...

Type reference scope

I m studying databases and am currently working on a object-relational DB project and I ve encountered a small problem with the number of possible constraints in an object table. I m using "Database ...

OracleParameter and DBNull.Value

we have a table in an Oracle Database which contains a column with the type Char(3 Byte). Now we use a parameterized sql to select some rows with a DBNull.Value and it doesn t work: OracleCommand ...

热门标签