我下面有两个表:
表A
+--------+--------+--------+
| color | shape | size |
+--------+--------+--------+
| Red | Square | Small |
| Blue | Square | Small |
| Yellow | Square | Small |
| Red | Circle | Small |
| Blue | Circle | Small |
| Yellow | Circle | Small |
| Yellow | Square | Medium |
| Red | Circle | Medium |
| Blue | Circle | Medium |
| Yellow | Circle | Medium |
| Red | Square | Large |
| Red | Circle | Large |
| Blue | Circle | Large |
| Yellow | Circle | Large |
+--------+--------+--------+
表B
+---------------+
| edge |
+---------------+
| Straight Line |
| Dotted Line |
| Squiggly Line |
+---------------+
我正在运行以下查询:
WITH TableA (color, shape, size)
AS (SELECT DISTINCT color, shape, size
FROM TableA),
TableB (edge)
AS (SELECT DISTINCT edge
FROM TableB)
SELECT COUNT(*) FROM
(SELECT a.*,
b.*
FROM TableA AS a
CROSS JOIN TableB AS b) as x;
我希望发生的事情:它将从表A的每一列中选择所有不同的值(颜色,形状,大小),然后从表B的每一列中选择每个唯一值(边),然后进行交叉联接产生颜色,形状,大小和边缘的每种组合。
我检查了查询选择唯一值的部分是否可以独立工作,但是一起运行将产生比我预期更少的行。
使用3种颜色,2种形状,3种尺寸和3条边缘,我应该得到54种可能的组合/行,但只有42种。
答案 0 :(得分:1)
您需要独立选择列:
select c.olor, s.shape, sz.size, e.edge
from (select distinct color from a) c cross join
(select distinct shape from a) s cross join
(select distinct size from a) sz cross join
(select distinct edge from b) e