2

I want to run the following query:

CREATE TABLE dummy AS
SELECT x, y, z
FROM table1
WHERE y!=0

then I want to do the following:

if z column has a non-null entry do:
1. copy the content of z into A
2. drop z
SELECT x,A
FROM Dummy 

What the second query essentially tries to do is that copy those rows of z into A which have no missing values. My question is how can we write query 2 and if there is a way to merge these two queries into one for optimisation?

Any help/advice much appreciated.

3
  • Do you mean (update/delete & select) in one single query? And where column a comes from?
    – McNets
    Commented Nov 5, 2018 at 16:36
  • Yes, I guess. This is what I mean. Column A is created based on the condition if z column has non-null entries.
    – T.H.
    Commented Nov 5, 2018 at 16:39
  • There's no way to Update and Drop in the same query, no. Commented Nov 5, 2018 at 16:50

1 Answer 1

3

Since you aren't using column y, you can ignore it in your first query. Then, you can simplify this with a single statement:

SELECT 
    x, 
    A = z
FROM table1
WHERE y!=0 and z is not null
2
  • Thanks for this but I am sorry I did not understand the solution. I think I was not clear in my question. I am trying to create a new column 'A' which has the contents of column z only when there are non-null entries in z (i.e z column has no missing data).
    – T.H.
    Commented Nov 5, 2018 at 16:48
  • @T.H. then the simplified edit should work
    – S3S
    Commented Nov 5, 2018 at 16:55

Not the answer you're looking for? Browse other questions tagged or ask your own question.