根据条件填写的列

时间:2015-07-08 06:43:57

标签: sql oracle

我有以下数据

PROD_NO    PROD_CAT     PROD_DESCRIPTION
X23        PENS          N/A
X23        PENCIL        in warehouse
X23        INK           "
X30        BOOKS         "
X30        DRAWINGS      not in warehouse
X30        ERASERS       "

我想要实现的是,如果PROD_DESCRIPTION有N / A或",那么我希望那些行被填充注释存在 对于那个prod_no。例如仓库中的X23 将为所有行填充。对于所有行填写X30 不在仓库

PROD_NO    PROD_CAT     PROD_DESCRIPTION
X23        PENS          in warehouse
X23        PENCIL        in warehouse
X23        INK           in warehouse
X30        BOOKS         not in warehouse
X30        DRAWINGS      not in warehouse
X30        ERASERS       not in warehouse

我该怎么做? 每个prod_no都有很多prod_no和prod_description不同

6 个答案:

答案 0 :(得分:1)

试试这个并告诉我

Out-file $logfile -append

这也应该有用。

UPDATE YOUR_TABLE SET 
 PROD_DESCRIPTION=decode(PROD_NO,'X23','in warehouse','X30','not in warehouse')
 WHERE PROD_DESCRIPTION='N/A' or PROD_DESCRIPTION=''

<强>被修改

假设所有生产没有两种类型的描述。相应地传递 UPDATE YOUR_TABLE SET PROD_DESCRIPTION= CASE WHEN PROD_NO='X23' THEN 'in warehouse' WHEN PROD_NO='X30' THEN 'not in warehouse' END WHERE PROD_DESCRIPTION IN('N/A','') 中的所有prod_no。

IN()

答案 1 :(得分:1)

试试这个:

select prod_no
     , prod_cat 
     , case when prod_no = 'X23' 
       then 'in warehouse' 
       else 'not in warehouse' end as prod_description 
from [your_table_name]

答案 2 :(得分:1)

UPDATE [YourTable]
SET PROD_DESCRIPTION = 
( CASE  
WHEN (PROD_DESCRIPTION = N/A and PROD_NO = X23) THEN 'in warehouse' 
WHEN (PROD_DESCRIPTION = "" and PROD_NO = X23) THEN 'in warehouse' 
WHEN (PROD_DESCRIPTION = N/A and PROD_NO = X30) THEN 'not in warehouse' 
WHEN (PROD_DESCRIPTION = "" and PROD_NO = X30) THEN 'not in warehouse' 
ELSE  (PROD_DESCRIPTION)
END )

答案 3 :(得分:1)

SELECT使用相关的子查询:

select t1.PROD_NO,
       t1.PROD_CAT     
       (select max(t2.PROD_DESCRIPTION)
        from tablename t2
        where t2.PROD_NO = t1.PROD_NO
          and t2.PROD_DESCRIPTION <> 'N/A'
          and t2.PROD_DESCRIPTION <> ''),
from tablename t1

JOIN版本,GROUP BY找不到仓库&#39;或者&#39;在仓库&#39;:

select t1.PROD_NO,
       t1.PROD_CAT,
       t2.PROD_DESCRIPTION
from tablename t1
LEFT JOIN (select PROD_NO, max(PROD_DESCRIPTION)
           from tablename
           where PROD_DESCRIPTION <> 'N/A'
             and PROD_DESCRIPTION <> ''
           group by PROD_NO) t2
    ON t1.PROD_NO = t2.PROD_NO

如果prod_no在仓库中同时拥有&#39;&#39;并且&#39;不在仓库中,MAX将返回&#39;不在仓库&#39;作为价值。如果你想要在仓库里&#39;相反,切换到子选择中的min(t2.PROD_DESCRIPTION)

答案 4 :(得分:1)

我认为这会有所帮助:)

SELECT P.PROD_NO,P.PROD_CAT,(SELECT PROD_DESCRIPTION FROM PRODUCT WHERE
PROD_DESCRIPTION NOT IN ('"','N/A') AND  PROD_NO = P.PROD_NO limit 1 ) 
AS DESCRIPTION  FROM PRODUCT P

我使用内部查询,因为我认为单个产品可能有多个描述,而不是'“'或'N / A'”如果情况并非如此,那么你可以使用这个

 SELECT P.PROD_NO,P.PROD_CAT, P1.PROD_DESCRIPTION FROM PRODUCT P
 JOIN PRODUCT P1 ON P.PROD_NO = P1.PROD_NO 
 WHERE P1.PROD_DESCRIPTION NOT IN ('"','N/A')

答案 5 :(得分:1)

您应为每个PROD_NO创建一个包含MAXIMUM PROD_DESCRIPTION的JOIN表,然后使用DECODE将此值输出为NULL,''或'N / A':

SELECT T.PROD_NO,
       T.PROD_CAT,
       DECODE (T.PROD_DESCRIPTION, NULL,TMAX.PROD_DESC_MAX, 
                                     '',TMAX.PROD_DESC_MAX,
                                  'N/A',TMAX.PROD_DESC_MAX,
                                        T.PROD_DESCRIPTION) 
       PROD_DESCRIPTION
FROM T
LEFT JOIN (SELECT PROD_NO, 
                  MAX(PROD_DESCRIPTION) PROD_DESC_MAX
            FROM T GROUP BY PROD_NO) TMAX
     ON T.PROD_NO = TMAX.PROD_NO

SQLFiddle demo