I have a table like this
Clubcard
AssoicaticeCard MainCard custID
A A 1
B B 1
C A 1
AA AA 2
BC BC 2
CC CC 2
Ar Ar 3
Bs Bs 3
Cv Cv 3
Now I need the result to be set like this where my AssoicaticeCard and MainCard have are be linked up properly.
I need the result like this to be shown. How to write an update statement for this to get achieved?
AssoicaticeCard MainCard custID
A A 1
B A 1
C A 1
AA AA 2
BC AA 2
CC AA 2
Ar Ar 3
Bs Ar 3
Cv Ar 3
I have an table with data like with which is inconsistency, now need to write an update statement to solve the issue. My table have around 1 million records.
update T1
set MainCard = T2.AssoicaticeCard
from YourTable as T1
inner join
(
select min(AssoicaticeCard) as AssoicaticeCard,
custID
from YourTable
group by custID
) T2
on T1.custID = T2.CustID
update clubcard set Maincard = 'A' where custID = 1;
update clubcard set Maincard = 'AA' where custID = 2;
update clubcard set Maincard = 'Ar' where custID = 3;
Related
I have 4 tables:
1. matchsal
ID salaray
1 1000
2 2000
3 3000
4 4000
5 5000
2. TABLE1
ID C
1 NA
2 NA
3 NA
4 NA
5 NA
3. TABLE2
ID lfs1 lfs2
1 2 3
2 3 1
3 3 1
4 3 1
5 2 3
4. TABLE3
ID_NIC filternn
1 private
2 public
3 private
4 Private
5 public
what i want is to update table1 with the salary values from matchsal table
with conditions in the others tables, i tried this query:
update TABLE1 LFS
SET C1= (Select SALARY from matchsal ss )
WHERE LFS."ID" IN
( SELECT "ID" from
TABLE2 lfs,
TABLE3 NIC
WHERE lfs."ID"=NIC."ID_NIC"
and lfs.lfs1 <> LFS.lfs2
and filternn in ( 'Private'))
and i got this error:
ERROR: more than one row returned by a subquery used as an expression
SQL state: 21000
The subquery in the SET is causing the problem. It is possible that you want:
SET C1 = (Select ss.SALARY from matchsal ss where ss.id = lfs.id)
But that is just a guess.
Try this:
update TABLE1 LFS
SET C1= (Select ss.SALARY from matchsal ss where lfs.id=ss.id)
WHERE lfs.ID IN
( SELECT ID from
TABLE2 lfs,
TABLE3 NIC
WHERE lfs.ID=NIC.ID_NIC
and lfs.lfs1 <> LFS.lfs2
and filternn in ( 'Private'))
I found the answer ^^"
It is simple thing
update TABLE1 LFS
SET C1= (Select SALARY from matchsal ss where ss.id=LFS."ID" limit 1)
WHERE LFS."ID" IN
( SELECT "ID" from
TABLE2 lfs,
TABLE3 NIC
WHERE lfs."ID"=NIC."ID_NIC"
and lfs.lfs1 <> LFS.lfs2
and filternn in ( 'Private'))
I Just add a limit to the sub query
I have three tables which store a simple onet-to-many relationship.
Table1 idT1 Name
1 A
2 B
3 B
Table2 idT2 Value
1 AA
2 BB
3 CC
4 DD
Table3 idT1 idT2
1 4
1 2
2 1
2 4
2 3
3 4
I need to return those rows in Table3 which only have one relationship between Table1 and and some rows from Table2.
Something like this:
If I look for rows that have a relation idT2 = 4, the query should return only this row:
Table3 idT1 idT2
3 4
I figured out this query but I think there must be something easier:
;WITH tb AS(SELECT idT1 FROM Table3 WHERE idT2 IN(4))
SELECT t.idT1 FROM T3 t INNER JOIN tb ON t.idT1 = tb.idT1
GROUP BY idT1 HAVING COUNT(t.idT2) = 1
Any help will be appreciated, what do you think?
One method is to use aggregation and just look for the values that appear once for a given idT2:
select idT1, max(idT2) as idT2
from t3
group by idT1
having min(idT2) = 4 and max(idT2) = 4;
You could also do this using not exists:
select t3.*
from t3
where t3.idT2 = 4 and
not exists (select 1 from t3 tt3 where tt3.idT1 = t3.idT1 and tt3.idT2 <> 4);
how can i update this table (zz col):
id xx yy zz n
---- ----- ----- --- -----
1 AA 20 0 0
2 AA 10 0 1
3 AA 10 0 2
4 BB 45 0 0
5 BB 15 0 1
6 BB 15 0 2
7 BB 15 0 3
[zz = yy where n = 0]
To:
id xx yy zz n
---- ----- ----- --- -----
1 AA 20 20 0
2 AA 10 20 1
3 AA 10 20 2
4 BB 45 45 0
5 BB 15 45 1
6 BB 15 45 2
7 BB 15 45 3
Thanks
update tablename set zz=yy where n=0;
You can join on the same table to do your update.
UPDATE A1
SET A1.zz = A2.yy
from
A AS A1
inner join A AS A2 ON A1.xx = A2.xx AND A2.n = 0
You may need to try this way (not tested, but this should help you)
Your UPDATE statement needs to be a correlated update-- you need some key in A that tells you which row in B to go to in order to retrieve the new value. Your WHERE clause should also be an IN or an EXISTS since it needs to return multiple rows.
UPDATE table1 a
SET a.zz = (SELECT b.yy
FROM table1 b
WHERE b.n = 0
AND a.id = b.id)
WHERE EXISTS (SELECT 1
FROM table1 b
WHERE a.id = b.id);
Assuming the join results in a key-preserved view, you could also
UPDATE (SELECT a.id,
a.yy oldval,
x.yy newval
FROM table1 a,
(SELECT b.id,
b.xx,
b.yy
FROM table1 b
WHERE b.n = 0) x
WHERE a.xx = x.xx)
SET oldval = newval;
Try this query
update tablename
set a.zz=(select a.yy
from tablename a
where a.xx=tablename.xx and a.n=0)
you can try this:
UPDATE table1
SET table1.zz= table2.xx
FROM table2
JOIN table1
ON table1.id=table2.id
I have a question on SQL join which involve multiple condition in second joined table. Below is the table details
Table 1
pId status keyVal
---- ------- ------
100 1 45
101 1 46
Table 2
pId mode modeVal
100 2 5
100 3 6
101 2 7
101 3 8
I have above two tables and I am trying to join based on below condition to get pId's
pId's which has keyVal = 45 and status = 1 joined with table2 which has mode = 2 and modeVal 5 and mode =3 and modeVal = 6
the result I am expecting is to return pid = 100
Can you please help me with a join query ?
One way is to use GROUP BY with HAVING to count that the number of rows found is 2, of which 2 are matching the condition;
WITH cte AS (SELECT DISTINCT * FROM Table2)
SELECT t1."pId"
FROM Table1 t1 JOIN cte t2 ON t1."pId" = t2."pId"
WHERE t1."status" = 1 AND t1."keyVal" = 45
GROUP BY t1."pId"
HAVING SUM(
CASE WHEN t2."mode"=2 AND t2."modeVal"=5 OR t2."mode"=3 AND t2."modeVal"=6
THEN 1 END) = 2 AND COUNT(*)=2
If the values in t2 are already distinct, you can just remove the cte and select directly from Table2.
An SQLfiddle to test with.
SELECT columns
FROM table1 a, table2 B
WHERE a.pid = B.pid
AND a.keyval = 45
AND a.status = 1
AND (
(B.mode = 2 AND B.modeval = 5)
OR
(B.mode = 3 AND B.modeval = 6)
)
Below query should work for you perfectly
select distinct table1.pid FROM table1 JOIN table2
on table1.pid = table2.pid
WHERE table2.modeValue IN (5,6) AND table2.mode IN (2,3) AND table1.keyVal=45 and table1.status=1;
I have one product table with following info.
ID ProductId Name OtherData
1 0 A data1
2 0 B data2
3 1 A1 NULL
4 1 A2 NULL
I need all data with detail ProductId is relationship with ID column.
I need result like below
ID ProductId Name OtherData
1 0 A data1
2 0 B data2
3 1 A1 data1
4 1 A2 data1
What kind of join or query I should use?
SELECT s.ID, s.ProductId, s.Name,
OtherData = COALESCE(s.OtherData, r.OtherData)
FROM dbo.Products AS s
LEFT OUTER JOIN dbo.Products AS r
ON s.ProductId = r.ID;