I have 3 table t1, t2 and t3.
t1 has 2 column-> id1, val1
t2 -> id2, val2
t3 -> id3, val3
If id1=id2 and id2 = id3
then I need to update val1 ad val3.
But I have repeating id1 and each should have same val3
I am using
update t1
inner join t2 on t1.id1 = t2.id2
inner join t3 on t2.id2 = t3.id3
set t1.val1 = t3.val3
;
But not able to do this.
The correct syntax is:
UPDATE table_name SET column = { expression | DEFAULT } [,...]
[ FROM fromlist ]
[ WHERE condition ]
So your UPDATE statement should look as follows:
update t1 set val1 = val3
from t2 inner join t3 on t2.id2 = t3.id3
where t1.id1 = t2.id2
;
See the Redshift documentation and their comprehensive UPDATE examples.
I needed to get values from the other table, t2.val3, on Redshift. I used the following
update t1
set val1 = t2.val3
from t2 join t1 t on t.id = t2.id;
I have to re-name t1. Otherwise Redshift complains.
Per #jie solution, but expanded a bit and w/o the distraction of a table alias as a table name (heh heh):
update
my_counts
set
my_count_delta = t1.my_count - t2.my_count
from
my_counts t1 join
my_counts t2 on
t1.group_id = t2.group_id and
t2.count_dt = ( t1.count_dt - interval '1 day' )
where
t1.count_error is null
A few notes:
my_count is a running total
my_count_delta is the change from the previous day entry (-/+)
this solves the issue where running total exists, but summarial columnar data for the delta needs to be added
:heart: how pg style sql makes date add/subtraction so simple: ( t1.count_dt - interval '1 day' )
as an avid lover of LEFT JOIN i was confounded that this would not run with a left join...the error message was very clear requiring "balanced join" or was it "even join"...so i went back to the #jie version of JOIN and found that the query was incredibly fast.
Related
I am looking to do some querying on two partitions (different partition date) on BQ.
Table format is the following
crm_id, attribute#1, attribute#2,attribute#3
For the IDs that are common from week 1 and week 2, I want to see how many changed attribute#1, attribute#2 and attribute# 3
I started with the inner join using:
WITH t1 AS
(SELECT crm_id AS w1_crm
FROM `table`
WHERE DATE(_PARTITIONTIME) = "date1"
)
SELECT crm_id
FROM `table`
WHERE DATE(_PARTITIONTIME) = "date2"
INNER JOIN
t1
ON
w1_crm = crm_id
I am getting an error on the INNER JOIN
Try below
WITH t1 AS (
SELECT crm_id AS w1_crm
FROM `table`
WHERE DATE(_PARTITIONTIME) = "date1"
), t2 AS (
SELECT crm_id
FROM `table`
WHERE DATE(_PARTITIONTIME) = "date2"
)
SELECT crm_id
FROM t2
INNER JOIN t1
ON w1_crm = crm_id
Is there a way to use joins in update statements for DB2?
Google has really let me down.
This is roughly what I'm trying to achieve (... except obviously working ....)
Update gk.WR_VEHICLE_WARRANTY w
join gk.VGARANT_FRIST_ZUWEIS z
on z.PK_GARANT_FRIST_ZUWEIS = w.FK_GARANT_FRIST_ZUWEIS
set CURRENT = '1'
where z.GW = '1'
and z.FK_GBE is null
and z.INTERN = '0';
I solved the problem , just took out the join and did a inner select
Update gk.WR_VEHICLE_WARRANTY
set CURRENT = '1'
Where FK_GARANT_FRIST_ZUWEIS in
(select PK_GARANT_FRIST_ZUWEIS from gk.VGARANT_FRIST_ZUWEIS z
where z.GW = '1'
and z.FK_GBE is null
and z.INTERN = '0' )
I have two tables table1 and table2. Each table contains a column with itemPrice. I need to add the two columns together.
The SQL query below returns the correct SUM.
SELECT SUM(item1+ item2) FROM
(select SUM(t1.itemPrice) item1 from table1 t1 WHERE t1.userid=='jonh') tableA
CROSS JOIN
(select SUM(t2.itemPrice) item2 from table2 t2 WHERE t1.userid=='jonh') tableB
I am not been lazy but the above query has so many SUM functions that I don't know where to start to write LINQ queries.
Can anyone help?
Ceci,
Hopefully this will give you what you want...
from f in (
from x in ( from t1 in Table1
where t1.Userid.Equals("John")
select new { Userid = t1.Userid }
).Distinct()
select new { item1 = ( from z in Table1
where z.Userid.Equals("John")
select z.ItemPrice ).Sum() ??0 ,
item2 = ( from z in Table2
where z.Userid.Equals("John")
select z.ItemPrice ).Sum() ??0 }
) select new { total = f.item1 + f.item2 }
In the case where there are no records for "john" in one table, it will bring back a 0 and sum up the other tables.
hope this helps.
I have multiple tables in a join and every table has a column ID. So in the resultig join there are a lot of ID columns. How I can access a specific ID column with the criteria API?
ParameterExpression<A> idParam = criteriaBuilder.parameter(A.class, PARAM_NAME);
Subquery<B> sq = query.subquery(B.class);
Root<B> root = sq.from(B.class);
Join<C, D> joinTogether = root.join("memberX").join("memberY");
sq.select(root);
sq.where(criteriaBuilder.and(criteriaBuilder.equal(joinTogether.get("id"), idParam), criteriaBuilder.equal(parentQuery.get("id"), root.get("id"))));
The problem is, that in the resulting SQL contains
SELECT 1 FROM E t6, B t5, C t4, D t3 WHERE ((( = paramName) AND (t0.ID = t5.ID)) AND (((t6.memberZ = t5.ID) AND (t4.ID = t6.memberX)) AND (t3.ID = t4.memberY))))
The table E (t6) is an additional join table between table B and C, t0 is the reference to the parent query. Instead t3.id = :paramName EclipseLink creates nothing just before the first equal-sign (paramName is the content of the constant PARAM_NAME). My idea is, that the "id" column could reference all tables and EclipseLink can not decide, which table I mean.
How I can change that?
Thank you
André
I have three tables. I have to retrieve the data using Linq statement. My three table names are A,B,C. I connected join for connecting two tables A and B based on the id's like:
select ol, fN, LN, ci, co
from member
join details
on member_id = details_id
where details_id in
(select contacts_id from contacts where
contacts_id1 = 1 and contacts_usr_id = 1)
I am able to write the query up to the where condition, how can I write the query for the inner while condition?
you can try this:
var idlist = (from tbl in table3
where tbl.usr_id == 1 && tbl.contacts_id == 1
select tbl.contacts_id ).ToList();
var x = from A in table1
from B in table2 where A.user_id == B.user_id
&& idlist.Contains(A.user_id)
select new { a = A.a, b = A.b, c = A.c, d = B.d, e = B.e };
check and let me know if that work.
Try flipping the query upside down. How about the following:
var query =
from t3 in table3
where t3.user_id = 1 && t3.contacts_id = 1
join t2 in table2 on t3.contacts_id equals t2.usr_id
join t1 in table1 on t2.usr_id equals t1.userid
select new {t2.a, t2.b, t2.c, t1.d, t1.e};