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
Related
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
Given I have columns A B C D ….. Z
I want to Group-By on A, B , C Having Count(*) > 1 and then for each of those rows, I want to SELECT the rest of the Columns as well that were not included in the aggregate.
The result would be like
Occurrences A B C D E F G ------- Z
3 e e k q r y e ------- j
3 e e k f t d t ------- y
3 e e k w e q c ------- d
2 f r s w e q c ------- d
2 f r s w e q c ------- d
How can I do that?
You don't want GROUP BY, you want ORDER BY. To get the first column, you can use window functions, which are ANSI standard and supported by most databases:
select t.*
from (select count(*) over (partition by a, b, c) as occurrences,
t.*
from t
order by a, b, c
) t
where occurrences > 1;
I have 2 tables joined with inner join with a column
Now rows would have
A B
C D
E G
P Q
Z F
This row I need to compare with Master Relation Table
Column1 Column2
A B
D C
E F
So based on the above condition I need to show records in a report
A B Do Not Show
C D Do Not Show
E G Show
P Q Show
Z F Show
You can do it this using LEFT JOIN:
SELECT T1.Col1,T1.Col2,
CASE WHEN T2.Column1 IS NOT NULL THEN 'Do Not Show' ELSE 'Show' END AS Result
FROM Table1 T1 LEFT JOIN
Table2 T2 ON
(T1.Col1=T2.Column1 AND T1.Col2=T2.Column2)
OR (T1.Col1=T2.Column2 AND T1.Col2=T2.Column1)
Result:
Col1 Col2 Result
---------------------------
A B Do Not Show
C D Do Not Show
E G Show
P Q Show
Z F Show
Sample result in SQL Fiddle
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
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