I have the below table I need all value from colE to update to colA is it possible to do it with one SQL?
TableA
colA colB colC colD colE
a b x y z
b c d f h
c d f w v
v f f f f
to update colA with value from colE
TableA
colA colB colC colD colE
z b x y z
h c d f h
v d f w v
f f f f f
This is too simple:
update tableA set colA = colE;
Try to use this code:
update TableA
set colA = colE;
Update table like this
update tableA set
colA = colE;
it will update as you want
update [table]
set [colA] = [colE]
should work for you :) try on some test data before live data though
Related
I have a dataframe
col1 col2 col3 col4
A F F F
B F A B
C B A C
D S A F
I want to say if A and F in any of these columns then make a new column and enter "Found"
col1 col2 col3 col4 output
A F F F Found
B F A B Found
C B A C 0
D S A F Found
Use :
df['output']=np.where(df.eq('A').any(1) & df.eq('F').any(1),'Found',0)
Another approach:
df['output']=(df.eq('A').any(1) & df.eq('F').any(1)).map({True:'Found',False:0})
Output:
col1 col2 col3 col4 output
0 A F F F Found
1 B F A B Found
2 C B A C 0
3 D S A F Found
Try this:
df.loc[df.apply(lambda x: ((x=='F').any() & (x=='A').any()).any(),axis=1), 'output'] = 'Found'
df.fillna(0)
You can use pd.DataFrame.where():
df.where(lambda x: (x=='A') | (x=='F')).dropna(thresh=1)
I have a table A with following data:
A:
colA colB
a x
b x
c y
d y
e z
f z
I want the output as:
colA colA_1
a b
c d
e f
I.e. I want to group the data based on colB and fetch the values from colA. I know that the same value will appear exactly twice in colB.
What I am trying to do is:
SELECT a1.colA, a2.colA
FROM A a1
JOIN A a2
ON a1.colA != a2.colA and a1.colB=a2.colB;
But this gives the output as:
colA colA_1
a b
b a
c d
d c
e f
f e
How can I fix this to get the desired output?
No need to join, simply do a GROUP BY:
SELECT min(colA), max(colA)
FROM A
group by colB
SQL Server 2008
Table A looks like:
A_ID v1 v2 v3
---------------------------
1 d e f
1 a b c
1 a b d
2 d a b
2 e f g
3 d e f
3 e f g
3 d a b
and Table B is similar:
B_ID v1 v2 v3
---------------------------
Q a b c
Q b a c
Q a b d
R d e f
R a b c
R d e f
P e f g
P d a b
What I need back from these two tables are the (A_ID, B_ID) pairs, if any, where each row of Table B where B_ID = any one value has one matching row in Table A where A_ID = any one value. In other words, I need to the complete matching set in A for each full set of triples in B--no super sets or subsets. The value of B_ID and A_ID is immaterial.
I thought partitioning would be the way to go, since I already have the column that naturally partitions A and B, and I also thought I could pre-select which paritions where JOINed by ensuring only partitions with matching numbers of rows would be attempting. I haven't been able to do either--partitioning both tables was easy, but I see no way to tell the join to only act on the partitions.
In this example, (2,P) would be returned because all rows in Set P match all rows in Set 2. Result (1, R) would NOT be returned because all rows of Set R are not matched by all rows of Set 1, etc.
Using symetric difference:
SELECT DISTINCT a1.A_ID, b1.B_ID
FROM A a1,B b1
WHERE NOT EXISTS (
(SELECT v1,v2,v3
FROM A WHERE A.A_ID = a1.A_ID
EXCEPT
SELECT v1,v2,v3
FROM B WHERE B.B_ID = b1.B_ID
)
UNION ALL
(
SELECT v1,v2,v3
FROM B WHERE B.B_ID = b1.B_ID
EXCEPT
SELECT v1,v2,v3
FROM A WHERE A.A_ID = a1.A_ID)
);
LiveDemo
Consider the following table
CREATE TABLE #temp
(
color1 VARCHAR(10),color2 VARCHAR(10),color3 VARCHAR(10)
)
INSERT INTO #temp
VALUES ('R','R','R'),
('R','R','B'),
('R','B','B'),
('R','R','G'),
('R','G','G'),
('B','B','B'),
('B','B','G'),
('B','G','G'),
('B','B','R'),
('B','G','R'),
('G','G','G'),
('G','G','B'),
('G','B','B'),
('G','G','R'),
('G','R','R')
I need the order the output such that the rows that contain values 'R' (in any of the three columns) should be the top of the result. Any suggestions ?
Note : The following query dosn't help.
SELECT *
FROM #temp
ORDER BY color1 DESC,color2 DESC,color3 DESC
The expected output is
color1 color2 color3
R R R
R R B
R B B
R R G
R G G
B B R
B G R
G G R
G R R
G G G
G G B
G B B
B B B
B B G
B G G
Thanks in advance.
SELECT *
FROM TEMP
ORDER BY CASE WHEN Color1='R' OR Color2='R' OR Color3='R' THEN 0 ELSE 1 END
I need help on creating updating SQL script
TableA
colA colB colC colD colE
a b x g z
b c d g h
c d f g v
v f f g f
d a q o a
TableB
colA colB colC colD colE
a b x y a
b c d g b
c d f g c
d e s g d
v f f g e
I need TableB.colE to update to TableA.colE where TableB.colD = TableA.colD
Result shall be
TableA
colA colB colC colD colE
a b x g b
b c d g c
c d f g d
v f f g e
d a q o a
I have tried using
UPDATE TABLEA SET(TABLEA.COLE=TABLEB.COLE) WHERE TABLEA.COLD = TABLEB.COLD
it doesn't work.
Try this update:
UPDATE TableA a
SET COLE = ( SELECT COLE
FROM TableB b
WHERE b.COLD = a.COLD );
UPDATE TABLEA SET TABLEA.COLE=TABLEB.COLE from TABLEB WHERE TABLEA.COLD = TABLEB.COLD
You have missed to specify the second table name TABLEB in from clause. Try this.
You need to specify the table join in the first clause of the update
UPDATE
(
SELECT tablea.cole, tableb.cole as newvalue
FROM tablea
JOIN tableb
ON tablea.cold = tableb.cold
)
SET cole = newvalue