I have a requirement to join 2 tables in Kusto but they are in different Database under same cluster.
I can see the below Query works if the tables are in Same DB.
Table1 | join (Table2) on CommonColumn, $left.Col1 == $right.Col2
But could you please tell me how do I join 2 tables in diff DB.
for example
DB1 - Table 1 - This is where I run the Query
DB2 - Table 2 - this is where I would like to take a join and I have the common column name - RunID
If you're running the query in the context of db1, then you can run a cross-db query like this:
Table1 // assuming Table1 is in db1
| join (database("db2").Table2) on CommonColumn, $left.Col1 == $right.Col2
If you don't know (or don't want to rely) on the database in the context of which the query runs, you can prefix all the tables with the relevant database name, like this:
database("db1").Table1
| join (database("db2").Table2) on CommonColumn, $left.Col1 == $right.Col2
I was able to take the join with the below query
| join kind = innerunique (
database("DB2").Table2) on $left.RunID == $right.RunID
Thanks
Related
Edit:
Below are more details about what I am trying to achieve.
I have 1 cluster with two databases. Database1 contains Table1 with the below info
DB2 contains a table with the below info:
UniqueID contains the same data as userID on table1.
In DB2, I have a function that performs some filtering on Unique ID.
I know that I can join both tables as the below screenshot, but my production example is way more complex than that and requires me to use the function.
My goal is to run the Table1 query, and pass the the userID to the function getuserproperties, to get a merged output.
Something Like this which does not work :)
Finally found it :) It was very simple :)
let UID = 7;
database('db1').Table1
| join kind=leftouter database('db2').Table2 on $left.userID==$right.UniqueID
| where userID == toscalar(getUserProperties(UID))
I have a Kusto Query like:
(Events
| take 1)
| join kind=leftouter Sensor_Data on $left.start_timestmp ==
$right.timestmp, someotherfield
and it will never return. The right side of the join has several billion entries.
If I do a
Events
| take 1
and use the result in the where clause of Sensor_Data it returns in no time.
The MS Support Team explains that this query requires a full table scan of the Sensors_Data table. The join parameters are not taken into consideration by the query optimizer.
Question:
Is the Kusto Query Optimizer really no able to optimize queries based on the join condition? To me it sounds a little bit like 1999 to have to first do the left side of the query manualy and then do the right side manualy as well? Is there some hint or so, to make this run fast?
consider rewriting your query as follows (for example) and see if that helps it perform better:
let x = toscalar(Events | take 1 | project pack("ts", start_timestmp, "sof", someotherfield));
Sensor_Data
| where timestmp == todatetime(x["ts"])
| where someotherfield == tostring(x["sof"])
I have multiples databases with the same table name. Is there any SQL command or API to merge multiple databases.
lets assume to perform a join between a table that is in database A, to a table that is in database B
SELECT *
FROM A.table1 t1 JOIN B.table2 t2 ON t2.column2 = t1.column1;
cheers
I created a database and having two tables like x and y. I cross joined them both and got an output.Now my question is can i save that output to a new table in the same database or different?
First you can try running the query SELECT * FROM table1 CROSS JOIN table2. Observe what columns are returned from the query, then create a new table new_table with columns of the appropriate type. Then you can try using INSERT INTO ... SELECT:
INSERT INTO new_table
SELECT * FROM table1 CROSS JOIN table2;
The CREATE TABLE statement directly accepts a query:
CREATE TABLE new_table AS
SELECT * FROM table1 CROSS JOIN table2;
(This will not keep the column types.)
The output can be saved in a different database if that database has been ATTACHed.
I'm porting my app from Django to ASP.NET Webforms (against my will, but what can we do with the corporate world..), and I'm used to Django generating all my SQL queries so now I need help.
I have 3 tables: proceso,marcador,marcador_progreso
Every proceso has many marcador_progreso, which in turn is the foreign key table to marcador.
So basically the tables look like:
proceso
id
marcador
id
text
marcador_progreso
id
marcador_id
proceso_id
state
For all the marcador_progreso where its proceso_id is the current proceso (from a QueryField in the URL), I need to list its state and it's respective marcador.text.
I've been working with EntityFramework but this is like a double query so I'm not sure how to do it.
I guess it is something that combines the following two statements, but I'm not sure how to do it.
SELECT [state] FROM [marcador_progreso]
SELECT [text] FROM [marcador] WHERE ([id] = marcador_id)
You want to do a JOIN:
SELECT mp.state, m.text
FROM marcador_progreso as mp
INNER JOIN marcador as m
ON mp.marcador_id = m.id
This is an excellent post that goes over the various join types.
You'll want to know about JOINs to call more than one table in your FROM clause. JOIN combines records from two or more tables in a database by using values common to each. There are different types - the SQL example below is an INNER join, which gets only records where both of the tables have a match on the common value. You may want to consider a LEFT join which would get any records that exist for the LEFT table (in this case marcador), even if there are not any matching record in the RIGHT(marcador_progreso ) table.
Pop the below in Management Studio, Play with different joins. Replace the INNER with LEFT, run it without the WHERE.
Read about `JOIN's.
In general, for your new venture of writing your own queries, they all start with the same basic structure:
SELECT (UPDATE,WHATEVER DML statement, etc) (COLUMNS) what you want to display (update,etc)
FROM (TABLE) where those records live
WHERE (FILTER/LIMIT) conditions that must be met by the data
Happy fetching!
SQL:
DECLARE #ProcessoId int
SET #ProcessoId = --1
SELECT m.[STATE],mp.[TEXT]
FROM marcador M
INNER JOIN marcador_progreso MP ON MP.marcador_id = m.id
WHERE proceso_id = #ProcessoId
EF INNER example
var marc = from m in yourcontext.marcador
join mp in yourcontext.marcador_progreso on m.id equals mp.marcador_id
where proceso_id == processoIdvariable
EF LEFT example
var marc = from m in yourcontext.marcador
join mp in yourcontext.marcador_progreso on m.id equals mp.marcador_id into details
from d in details.DefaultIfEmpty()
where proceso_id == processoIdvariable