I need to join on a table to return the MAX value from that right-hand table. I have tried to mock it up using 'datatable' but have failed miserably :(. I'll try and describe with words.
T1 = datatable(ID:int, Properties:string, ConfigTime:datetime) [1,'a,b,c','2021-03-04 00:00:00']
T2 = datatable(ID:int, Properties:string, ConfigTime:datetime) [2,'a,b,c','2021-03-02 00:00:00', 3,'a,b','2021-03-01 00:00:00', 4,'c','2021-03-20 00:00:00']
I'm using this as an update policy on T2, which has a source of T1. So I want to select the rows from T1 and then join the rows from T2 that have the highest timestamp. My first attempt was below:
T1 | join kind=inner T2 on Id
| summarize arg_max(ConfigTime1, Id, Properties, Properties1, ConfigTime) by Id
| project Id, Properties, ConfigTime
In my actual update policy, I merge the properties from T1 and T2 then write to T2, but for simplicity, I've left that for now.
Currently, I'm not getting any output in my T2 from the update policy. Any guidance on another way I should be doing this would be appreciated. Thanks
It seems that you want to push the arg_max calculation into the T2 side of the join, something like this:
T1
| join kind=inner (
T2
| summarize arg_max(ConfigTime1, Id, Properties, Properties1, ConfigTime) by Id
| project Id, Properties, ConfigTime
) on Id
Note that to ensure acceptable performance you want to limit the timeframe for the arg_max search, so you should consider a time based filter before the arg_max.
I think what you're looking for is a union
let T1 = datatable(ID:int, Properties:string, ConfigTime:datetime) [
1,'a,b,c','2021-03-04 00:00:00'
];
let T2 = datatable(ID:int, Properties:string, ConfigTime:datetime) [
2,'a,b,c','2021-03-02 00:00:00',
3,'a,b','2021-03-01 00:00:00',
4,'c','2021-03-20 00:00:00'
];
Here is an example using a variable with summarize max:
let Latest = toscalar(T2 | summarize max(ConfigTime));
T1
| union (T2 | where ConfigTime == Latest)
The result will keep the entries from T1 and only the latest entries from T2.
If this doesn't reflect your expected results please show your expected output.
i have 3 tables
table 1( one id to one name)
ID|name
1| john
2|mike
3|olga
4|juliet
table 2 ()
ID|adress
1|xxx
1|yyy
2|xx
2|z
3|xxx
3|yy
table 3 ()
address|buildings
xxx|flat
xxx|building
z|flat
z|building
z|park
z|lot
yy|building
yy|park
i wish to find the name of those wanting to stay at both building and flat, all values are random so i can only search with buildings, i have already managed to query for only one type of buildings but i am missing something as to validate the needed AND
You have to join all 3 tables and group by name:
SELECT t1.name FROM table1 t1
INNER JOIN table2 t2 ON t2.id = t1.id
INNER JOIN table3 t3 ON t3.address = t2.address
WHERE t3.buildings IN ('building', 'flat')
GROUP BY t1.name
HAVING COUNT(DISTINCT t3.buildings) = 2
I'm trying to get the result into a variable (is it possible to do it as a %rowtype of an existing table? probably not because of conflicting columns) where it would display me all the values where the two refs overlap and the i_ref(which is inputted ) also overlaps with the ref from t1.
select *
into aRow
from table1 t1
where t1.ref = i_ref
and (select * from table2 t2 where t1.ref = t2.ref);
What am I doing wrong with my select?
You can join tables instead of nested subquery:
select t1.*
into aRow
from table1 t1 join table2 t2 on t1.ref = t2.ref
where t1.ref = i_ref
I have a below working query:
`select distinct t1.ExNo,
t3.ExDesc from table1 t1
left join table2 t2 on t1.ExNo = t2.ExNo
join table3 t3 on t3.ExTyp = case
when t1.ExTyp = 'CONT'
and t2.ExTyp is not null
then t2.ExTyp
else t1.ExTyp
end
order by t1.ExNo;`
But if the same query modified as below and use, I am getting this error. Could you please rectify:
`select distinct t1.ExNo,
t3.ExDesc
from table1 t1 t1, table2 t2,table3 t3 where
t1.ExNo *= t2.ExNo and
t3.ExTyp = case
when t1.ExTyp = 'CONTNR'
and t2.ExTyp is not null
then t2.ExTyp
else t1.ExTyp
end
order by t1.ExNo;`
Error:
The table is an inner member of an outer-join clause. This is not allowed if the table also participates in a regular join clause.
I need to compare data of the same table who do this.
At example, compare A,10 with B,10 when 10 is a repeat value.
You can do this using the cross-product operator, in SQL this would be done as
SELECT T1.colA, T2.colA, (T1.colA < T2.colA) as colA_comp
FROM TableName T1, TableName T2
WHERE T1.colB = T2.colB
What this does is take the cross-product of the table TableName with itself (renamed as T1 and T2), and the WHERE clause filters out those records that agree on colB (the repeated value 10, in your example).
If you compare with the same table then you may use left Join
SELECT t1.cola,t1.colb,t2.cola,...
from tableA t1
LEFT JOIN tableA t2 on t2.cola = t1.cola
WHERE t1.cola = 10
I hope it might be work!