0

我正在用 oracle 编写 sql 查询。我的要求是我想用大小写有条件地更新数据库列。下面是我在三个表中使用的示例数据。

create table z_product(uuid int, variationvalue varchar(10));
create table z_listprice(productid int, price int);
create table z_variation(masterproductid int, productid int);

insert into z_product values(1,null);
insert into z_product values(2,null);
insert into z_product values(3,null);
insert into z_product values(4,null);
insert into z_product values(5,null);
insert into z_product values(6,null);

insert into z_listprice values(1,10);
insert into z_listprice values(3,20);
insert into z_listprice values(5,10);
insert into z_listprice values(6,19);

insert into z_variation values(2, 1);
insert into z_variation values(2, 3);
insert into z_variation values(4, 5);
insert into z_variation values(4, 6);

如果“masterproductid”和“eqaul”的所有价格值都相等,如果价格与“notequal”消息不同,我想更新“variationvalue”列。

我试过的查询是

UPDATE z_product pr SET pr.variationvalue =
        CASE WHEN ( SELECT numberofrows FROM (SELECT COUNT(*)  AS numberofrows, pricecount , productmasterid
                              FROM ( SELECT COUNT(prl.price) AS pricecount , prl.price AS price, prv.masterproductid AS productmasterid
                                        FROM  z_variation prv, z_listprice prl
                                        WHERE prv.masterproductid = (SELECT prv.masterproductid
                                                                        FROM z_variation prv
                                                                        WHERE prv.productid = prl.productid GROUP BY prv.masterproductid)
                                        AND prl.productid = prv.productid
                                        GROUP BY prl.price, prv.masterproductid)
                    GROUP BY pricecount , productmasterid)) = '1'
                    AND 
                    ( SELECT pricecount FROM (SELECT COUNT(*)  AS numberofrows, pricecount , productmasterid
                              FROM ( SELECT COUNT(prl.price) AS pricecount , prl.price AS price, prv.masterproductid AS productmasterid
                                        FROM  z_variation prv, z_listprice prl
                                        WHERE prv.masterproductid = (SELECT prv.masterproductid
                                                                        FROM z_variation prv
                                                                        WHERE prv.productid = prl.productid GROUP BY prv.masterproductid)
                                        AND prl.productid = prv.productid
                                        GROUP BY prl.price, prv.masterproductid)
                    GROUP BY pricecount , productmasterid)) >1
        THEN 'equal'
        ELSE 'notequal' END
WHERE pr.uuid = (SELECT prv.masterproductid
                    FROM z_variation prv, z_listprice prl
                    WHERE prv.productid = prl.productid 
                    AND pr.uuid = prv.masterproductid GROUP BY prv.masterproductid)

这个查询有什么问题。我在哪里做错了。任何建议将不胜感激

4

2 回答 2

1

进行更多调查,看起来您要返回多个 PRODUCTMASTERID,从而返回 2 个结果——请参阅此小提琴:http ://sqlfiddle.com/#!4/da309/8

结果如下所示:

NUMBEROFROWS PRICECOUNT PRODUCTMASTERID
2            1           4
2            1           2

这些子查询应该只返回一行以与 CASE 语句一起使用。而且由于您只检查第一列,因此添加 DISTINCT 有效。但是,如果 NUMBEROFROWS 是不同的值,那么 DISTINCT 将无法单独工作。

UPDATE z_product pr SET pr.variationvalue =
        CASE WHEN ( SELECT DISTINCT numberofrows FROM (SELECT COUNT(*)  AS numberofrows, pricecount , productmasterid
                              FROM ( SELECT COUNT(prl.price) AS pricecount , prl.price AS price, prv.masterproductid AS productmasterid
                                        FROM  z_variation prv, z_listprice prl
                                        WHERE prv.masterproductid = (SELECT prv.masterproductid
                                                                        FROM z_variation prv
                                                                        WHERE prv.productid = prl.productid GROUP BY prv.masterproductid)
                                        AND prl.productid = prv.productid
                                        GROUP BY prl.price, prv.masterproductid)
                    GROUP BY pricecount , productmasterid)) = '1'
                    AND 
                    ( SELECT DISTINCT pricecount FROM (SELECT COUNT(*)  AS numberofrows, pricecount , productmasterid
                              FROM ( SELECT COUNT(prl.price) AS pricecount , prl.price AS price, prv.masterproductid AS productmasterid
                                        FROM  z_variation prv, z_listprice prl
                                        WHERE prv.masterproductid = (SELECT prv.masterproductid
                                                                        FROM z_variation prv
                                                                        WHERE prv.productid = prl.productid GROUP BY prv.masterproductid)
                                        AND prl.productid = prv.productid
                                        GROUP BY prl.price, prv.masterproductid)
                    GROUP BY pricecount , productmasterid)) >1
        THEN 'equal'
        ELSE 'notequal' END
WHERE pr.uuid = (SELECT prv.masterproductid
                    FROM z_variation prv, z_listprice prl
                    WHERE prv.productid = prl.productid 
                    AND pr.uuid = prv.masterproductid GROUP BY prv.masterproductid)

- 编辑 -

如果我正确理解了您的要求 - 也就是说,对于任何给定的 masterproductid,如果 listprice 表中的任何价格不同,那么您希望将variationvalue 更新为 notequal,但如果价格相同,那么您想要将列更新为相等,然后试试这个:

MERGE
INTO    z_product
USING   (
        SELECT COUNT(DISTINCT l.price) cnt,
          v.masterproductid
        FROM z_variation v
          JOIN z_listprice l on v.productid = l.productid
        GROUP BY v.masterproductid
        )
ON      (uuid = masterproductid)
WHEN MATCHED THEN
UPDATE
SET     variationvalue = CASE WHEN cnt = 1 THEN 'Equal' ELSE 'Not Equal' END

http://sqlfiddle.com/#!4/098ad/1

希望这可以帮助!

于 2013-02-14T23:52:28.757 回答
0

回答:

UPDATE z_product pr SET pr.variationvalue =
        CASE WHEN ( SELECT numberofrows FROM (SELECT COUNT(*)  AS numberofrows, pricecount , productmasterid
                              FROM ( SELECT COUNT(prl.price) AS pricecount , prl.price AS price, prv.masterproductid AS productmasterid
                                        FROM  z_variation prv, z_listprice prl
                                        WHERE prv.masterproductid = (SELECT prv.masterproductid
                                                                        FROM z_variation prv
                                                                        WHERE prv.productid = prl.productid GROUP BY prv.masterproductid)
                                        AND prl.productid = prv.productid
                                        GROUP BY prl.price, prv.masterproductid)
                    GROUP BY pricecount , productmasterid) WHERE pr.uuid = productmasterid) = '1'
                    AND 
                    ( SELECT pricecount FROM (SELECT COUNT(*)  AS numberofrows, pricecount , productmasterid
                              FROM ( SELECT COUNT(prl.price) AS pricecount , prl.price AS price, prv.masterproductid AS productmasterid
                                        FROM  z_variation prv, z_listprice prl
                                        WHERE prv.masterproductid = (SELECT prv.masterproductid
                                                                        FROM z_variation prv
                                                                        WHERE prv.productid = prl.productid GROUP BY prv.masterproductid)
                                        AND prl.productid = prv.productid
                                        GROUP BY prl.price, prv.masterproductid)
                    GROUP BY pricecount , productmasterid) WHERE pr.uuid = productmasterid) >1
        THEN 'equal'
        ELSE 'notequal' END
WHERE pr.uuid = (SELECT prv.masterproductid
                    FROM z_variation prv, z_listprice prl
                    WHERE prv.productid = prl.productid 
                    AND pr.uuid = prv.masterproductid GROUP BY prv.masterproductid)
于 2013-02-15T01:05:25.800 回答