Sql 正在完成不存在行的表

Sql 正在完成不存在行的表,sql,postgresql,Sql,Postgresql,我有一张这样的桌子: buyer product quantity tom skirt 2 anna skirt 3 tom jeans 5 declare @tb table (buyer varchar(150), product varchar(150), quantity int) insert into @tb values('tom','skir

我有一张这样的桌子:

buyer        product        quantity
tom          skirt          2
anna         skirt          3
tom          jeans          5
declare @tb table (buyer varchar(150), product varchar(150), quantity int)

insert into @tb 
values('tom','skirt',2),
('anna','skirt',3),
('tom','jeans',5)


select *
from @tb a
left join(  select 
        distinct(product) 
        from @tb) b on a.product = a.product

        select b.buyer, p.p, isnull(t.quantity,0)
        from (select distinct buyer from @tb) b cross join
            (select distinct product p from @tb) p left join
            @tb t
            on t.buyer = b.buyer and t.product = p.p
        --where t.buyer is null
独特的(产品)=裙子和牛仔裤

我想要一个表,当所有可能的产品都不存在
元组时,它可以插入另一列
quantity=0

因此,结果将是:

buyer        product        quantity
tom          skirt          2
anna         skirt          3
tom          jeans          5
anna         jeans          0
这看起来并不复杂,但我不知道怎么做

更新
我发现了一个额外的并发症

我的产品实际上由两个字段定义:类和产品。Product可以为null,并且当Product字段为null时,我不需要丢失信息量(现在发生交叉连接)

如果我有这个:

buyer       class       product        quantity
tom         clothes     skirt          2
anna        clothes     skirt          3
tom         clothes     jeans          5
jim         shoes       NULL           7
我需要:

buyer       class       product        quantity
tom         clothes     skirt          2
anna        clothes     skirt          3
tom         clothes     jeans          5
anna        clothes     jeans          0
jim         shoes       NULL           7
jim         clothes     skirt          0
jim         clothes     jeans          0
tom         shoes       NULL           0
anna        shoes       NULL           0

谢谢大家,我知道我把事情搞复杂了

您可以使用
交叉联接
生成买家和产品的所有可能组合。然后使用
左联接
(或
不存在
)筛选出表中已有的联接:

insert into table(buyer, product, quantity)
    select b.buyer, p.product, 0
    from (select distinct buyer from table) b cross join
         (select distinct product p from table) p left join
         table t
         on t.buyer = b.buyer and t.product = p.product
    where t.buyer is null;
编辑:

如果需要返回所有行的查询,则可以使用非常类似的方法:

    select b.buyer, p.product, coalesce(t.qty, 0) as qty
    from (select distinct buyer from table) b cross join
         (select distinct product p from table) p left join
         table t
         on t.buyer = b.buyer and t.product = p.product;
编辑二:

如果买家和/或产品的值为
NULL
,则使用
NULL
安全比较:

    select b.buyer, p.product, coalesce(t.qty, 0) as qty
    from (select distinct buyer from table) b cross join
         (select distinct product p from table) p left join
         table t
         on t.buyer is not distinct from b.buyer and
            t.product is not distinct from p.product;

(作为一个次要的旁注:我真的不喜欢在这个结构中使用
distinct
。为什么Postgres(ANSI?)给它起了这么复杂的名字?

@Gordon的解决方案几乎满了,我这样编辑:

buyer        product        quantity
tom          skirt          2
anna         skirt          3
tom          jeans          5
declare @tb table (buyer varchar(150), product varchar(150), quantity int)

insert into @tb 
values('tom','skirt',2),
('anna','skirt',3),
('tom','jeans',5)


select *
from @tb a
left join(  select 
        distinct(product) 
        from @tb) b on a.product = a.product

        select b.buyer, p.p, isnull(t.quantity,0)
        from (select distinct buyer from @tb) b cross join
            (select distinct product p from @tb) p left join
            @tb t
            on t.buyer = b.buyer and t.product = p.p
        --where t.buyer is null

试试看

你还有一张给客户的桌子吗?结构是什么?我不知道,但如果需要的话,我可以创建一个不同买家的表格。是否有提供买家/类别/产品相关值完整列表的表格?我假设没有的每个类都默认为一行
product NULL
quantity 0
?另外,请始终提供您的Postgres版本。是的,有一个产品表。是的,每个没有的类默认为一行,产品为空,数量为0.5。你有一些错误。这样,您只会得到不存在的行。如下编辑:选择b.buyer,p.p,isnull(t.quantity,0)from(从@tb中选择不同的买方)b交叉连接(从@tb中选择不同的产品p)p左连接@tb t on t.buyer=b.buyer和t.product=p。p@bugs2919 . . . OP只需要表中不存在的行。重读这个问题。我认为不是,它说的是‘结果应该是:’,而且是完整的。但是,答案很好。我还没有想过交叉连接+太棒了!嗨,谢谢你们两位。您还知道我问题中更新的答案吗?在标识符中使用
@
无效(标准)SQL,在Postgres中不起作用。顺便说一句:
distinct
不是一个函数<代码>独特(产品)没有意义。