简体   繁体   English

SQL查询基于表中其他列数据的列更新

[英]SQL Query for an update of a column based on other column's data in a Table

I need to write an SQL query to take either from one of two column's data(based on whichever is available and not null) and append it to a static text into another column within the same table. 我需要编写一个SQL查询,以从两列数据中的一个获取数据(基于可用的值,而不是空值),并将其附加到同一表中另一列的静态文本中。

Can anybody tell me how to write this? 有人可以告诉我该怎么写吗?

Example Data : 示例数据:

ID  Type   Barcode  Serial No  Location

1   Test    ABCD     1234       LOC1  
2   Test    EFGH     NULL       LOC2  
3   Test    NULL     5678       LOC3  
4   Test    NULL     NULL       LOC1  

Final Data Reqd in Format 最终数据格式要求

ID  Type  Barcode  Serial No  Location

1   Test  ABCD     1234       LOC1-ABCD   (Append barcode if its not null)  
2   Test  EFGH     NULL       LOC2-EFGH   (Append barcode if its not null)  
3   Test  NULL     5678       LOC3-5678   (Append serial no since barcode is null)  
4   Test  NULL     NULL       LOC1        (Both r Null keep loc as it is)  

Please help me on it....really stuck on this :( 请帮助我。。。。。

Database is Oracle 10. 数据库是Oracle 10。

Supra 超人

Update 1 : 更新1:

Thanks a lot Marco for your help....the location field only needs to updated in the table after appending the barcode/serial no from the same table into location field. 非常感谢Marco的帮助。...仅在将同一表的条形码/序列号附加到location字段后,才需要更新table中的location字段。 Your edited query is not working :(...please let me know if I need to give some more info/data. 您编辑的查询无效:(...请让我知道是否需要提供更多信息/数据。

Final Update : 最终更新:

Shesek's Answer worked perfect :D...you are the man :)...Thanks a ton :D 谢谢克的答案很完美:D ...你是男人:)...谢谢

Try this: 尝试这个:

SELECT ID, Type, Barcode, "Serial No",
CASE
  WHEN Barcode IS NOT NULL THEN Location || '-' || Barcode
  WHEN "Serial No" IS NOT NULL THEN Location || '-' || "Serial No"
  ELSE Location
FROM your_table

Take a look at CASE function and String Concat . 看一下CASE函数String Concat
I'm not sure the way I escaped Serial No field, anyway look here 我不确定我逃脱了“ 序列号”字段的方式,无论如何请看这里

EDITED: 编辑:
Try this: 尝试这个:

UPDATE your_table SET Location =
CASE
  WHEN Barcode IS NOT NULL THEN Location || '-' || Barcode
  WHEN "Serial No" IS NOT NULL THEN Location || '-' || "Serial No"
  ELSE Location

Don't have Oracle installed, so can't test query, but this should work.... 没有安装Oracle,因此无法测试查询,但这应该可以工作。

You could use NVL function, but it's good for 2 options (use value a if it's not null, and if it is, use value b) 您可以使用NVL函数,但这对2个选项很有用(如果不为null,则使用值a;如果不是,则使用值b)

update myTable
set myCol = NVL(colA,colB)

Since you however want to concatenate if and only if you get a non-null value from either column, you need to get a little trickier and combine using nested NVL() 但是,由于您仅当从任一列中获得非空值时才想进行连接,因此您需要增加一些技巧,并使用嵌套的NVL()进行合并

update myTable
set myCol = myCol || CASE NVL(NVL(colA,colB),0) WHEN 0 THEN '' ELSE '-' || NVL(colA,colB)

Explanation: 说明:

  • NVL(colA,colB) will evaluate to colA first, colB if colA is null, and null if both are null NVL(colA,colB)将首先计算为colA,如果colA为null,则colB,如果两者均为null,则为null
  • In all situations I return the column's value 在任何情况下,我都返回列的值
  • I then append - if both are null, I essentially am appending 2 empty strings ('' and NULL); 然后我追加-如果两个都为null,则实质上是在追加2个空字符串(''和NULL); otherwise, I'm appending '-' and the value I evaluate to 否则,我要附加'-'和我求值的值
  • I use a CASE statement to do an IF .. ELSE to figure if I need '-' 我使用CASE语句执行IF .. ELSE来确定是否需要'-'
  • I use nested NVL functions to assign 0 if both are null to make the CASE logic easy 我使用嵌套的NVL函数(如果两者都为null)则分配0,以简化CASE逻辑
  • I then append my evaluated value 然后,我附加我的评估值

As I said before, this is untested, so there could be some syntax issues, but the logic is correct :-) 如我之前所说,这未经测试,因此可能存在一些语法问题,但逻辑是正确的:-)

According to your comment on the other answer, 根据您对其他答案的评论,

UPDATE Network_Plant_Items
    SET FULL_ADDRESS = 'foobar' || COALESCE(BARCODE, MANUF_SERIAL_NUMBER)
    WHERE BARCODE IS NOT NULL OR MANUF_SERIAL_NUMBER IS NOT NULL

If you want to append this to the current value of FULL_ADDRESS, as I understand from the original question, 如果您想将此值附加到FULL_ADDRESS的当前值上,那么根据我对原始问题的理解,

UPDATE Network_Plant_Items
    SET FULL_ADDRESS = FULL_ADDRESS || COALESCE(BARCODE, MANUF_SERIAL_NUMBER)
    WHERE BARCODE IS NOT NULL OR MANUF_SERIAL_NUMBER IS NOT NULL

COALESCE() returns the first non-NULL argument you pass to it. COALESCE()返回您传递给它的第一个非NULL参数。 See Oracle's manual page on it . 参见Oracle的手册页

Just as a general FIY, NVM() that was suggested by another answers is the old Oracle-specific version of COALESCE() , which works kinda the same - but it only supports two arguments and evaluates the second argument even if the first one is non-null (or in other words, its not short-circuit evaluated). 就像一般的FIY一样,另一个答案建议的NVM()是旧的Oracle特定版本的COALESCE() ,它的工作原理大致相同-但它仅支持两个参数,并且即使第一个参数是非空(或换句话说,未评估短路)。 Generally, it should be avoided and the standard COALESCE should be used instead, unless you explicitly need to evaluate all the arguments even when there's no need for it. 通常,应该避免使用标准COALESCE应该使用标准COALESCE ,除非您明确需要评估所有参数,即使在不需要时也是如此。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM