I have a table of 3 columns A,B,C
initially column C is completely empty and for every entry either A has a number or B has a number (never both in the same row)
I want to create a view that checks for every row if A=x and B is null or 0 then write the value of A in col.
EXAMPLE:
Can someone help guide me, I am still new to sql
You can'z use a VIEW for that but an UPDATE
UPDATE mytable
SET C = CASE WHEN A > 0 AND (B IS NULL OR B = 0) THEN A
ELSE B END
Thsi will not include what happens wehen A > 0 and B > 0 as you haven't specify what to do so this query will always take A before B
Let's assume a table ABC with column A, B and C as you mentioned
create table abc (a int ,b int ,c int )
And you want to display column C as value of either A or B based upon value
then you can create View by two methods to achieve the desired result
Using CASE
create view abc_v1
as
select a, b, case when isnull(a,0)=0 then b else a end "c"
from
abc
using Coalesce: considering the values would be either 0 or some value, we can mark column A/B as NULL when value is zero and use Coalesce
create view abc_v2
as
select a,b, coalesce(nullif(a,0),nullif(b,0)) as "c"
from abc
Or else,
If you want to update Column C with Value of col A/B then
Update ABC
set c = coalesce(nullif(a,0),nullif(b,0))
Try this view. It spells out your requirement.
CREATE OR REPLACE VIEW abc_with_c AS
SELECT a, b,
CASE WHEN a = 0 OR a IS NULL THEN b
WHEN b = 0 OR b IS NULL THEN a
ELSE NULL
END AS c
FROM abc;
It's a good idea in SQL to write statements so they're easy to read and reason about.
Related
First of apologies if this question is too basic as im still a beginner
I have 3 tables a,b and c
table a has 2 columns tr_id, cp_id
table b has tr_id, cde
table c has
cde, value
condition to load a.cp_id is
{ case when b.cde = "some_value" then a.cp_id = c.value else NULL }
how do i write a query to check whether the a.cp_id is loaded correctly as per the above condition
hi i have two query result:
Table A
Id u
1 50,00
2 60,00
3 70,00
and
Table B
id c
4 110,00
5 120,01
6 130,02
Now i have doing two query on this table and i want sum their query result.
I want update column c from table B with 160 that is sum of(110+50).
Table B
Id c
4 160,00
Table B and Table A they have nothing in common.
Now i have doing two query for select their value ed for sum two data:
$data=number_format($row['c']+$row1['u']);
$query_updatee="update B set c= (integer)$data where c=110,00";
Can i sum the data from two different table that don't have nothing in comon?
My output is pg_query(): Query failed: ERRORE: syntax error at or near "1" LINE 1: update B set punti = (integer)1680,00 where c=110,00 ^ in C:\xampp\htdocs\table_A.php on line 81
You can use a subquery to fetch the increment. The rest seems to just be filtering:
update B
set c = c + (select a.u from a where a.id = 1)
where B.id = 4;
Obviously, you can use where b.c = 110.00 if you want to filter by a number (or use a comma if that is how the database is set up).
I have two columns (A and B) on table 1, and I want to concatenate them into another column (C) only if the beginning of B is not A, and if that is not the case, just copy B into C.
The key point here is that A and B do not have a fixed length, so I don't think I can use left(), since it needs a specific length.
For example:
ID A B
1 5 48721
2 98 98555
3 98 136
4 841 8417740313
5 841 133889
In this case, column C should include:
For ID=1: 548721
For ID=2: 98555
For ID=3: 98136
For ID=4: 8417740313
For ID=5: 841133889
I was trying:
UPDATE 1
SET C = B
WHERE LEFT (B) = A
UPDATE 1
SET C = concat(A,B)
WHERE LEFT(B) <> A
But it doesn't work, since I need to give left() a fixed length. What would you guys do?
You seem to want something like this:
UPDATE t
SET C = (CASE WHEN B LIKE A || '%' THEN B ELSE A || B END);
That is, you can use LIKE for the comparison.
Step1:
update table
set col_c = col_a||substr(col_b,length(col_a))
where
substr(col_b,1,length(col_a))=col_a;
Step2:
update table
set col_c = col_a||col_b
where
substr(col_b,1,length(col_a))<>col_a;
you can try this and let us know,
the earlier given solution is also correct; but what if the same eg: 98 in col_a is present in the middle of col_b instead of in the start ?
I need to write a query in MS Access where all the three columns should not be equal .
For example there are three columns A B C . Each column should not be equal to each other all should have a separate value.
How can I write such a query?
SELECT a, b, c
FROM my_table
WHERE a<>b AND a<>c AND b<>c
If your fields are non-nullable, all you need to check is that A != B, A != C, and B != C:
SELECT *
FROM test
WHERE A <> B AND A <> C AND B <> C
The same query would be OK if fields are nullable, but NULLs are not considered a valid value.
I want to update table A in such a way that if the attribute of the table column is desired then only it will change otherwise it wont change..
Update table A set B="abcd" ,C= (case when C="abc" then C="abcd" else C end) where column =1;
means C should be only change when in column=1 and C value is abc otherwise C should not be update ..it should be dropped and only B changes. but if the C
value get matched i.e abc give me the output 0 .. not changing to the abcd
Inside the THEN part, C="abcd" compares C with the value, and returns either 1 or 0.
The entire CASE expression should just return a value that then gets written into the C column, so you want just 'abcd' in this place:
UPDATE tableA
SET B = 'abcd',
C = CASE
WHEN C = 'abc' THEN 'abcd'
ELSE C
END
WHERE column = 1;
If I understand you correctly, you're trying to do two separate things:
UPDATE A set B = 'abcd' WHERE column = 1
UPDATE A set C = 'abcd' WHERE C = 'abc' AND column = 1
Is that right? If so, can you do it as two simple statements instead of one complicated statement?