SOLVED

Get scalar value from table

Copper Contributor

Using the new OMS Log Analytics query language, how do I get a scalar value out of a table?

That is, what is the equivalent of this TSQL query, presuming Table1 has a single row:

Select @ScalarVar = Column1 from Table1

 

5 Replies
Hi I am not very familiar with SQL but I think you are probably are looking for let statement: https://docs.loganalytics.io/docs/Language-Reference/Query-statements/Let-statement

Stanislav,

I have some understanding of the let statement.

In all of the examples, you can use a let statement with a query to store a table result, or with a scalar expression to store a scalar result.

How do I get, store and use scalar result that comes from a table query?

For example, I need to run one query that gets and stores the datetime from Column1 of Table1 of a particular single record, so that I can use that scalar value in subsequent queries.

best response confirmed by Tim Curwick (Copper Contributor)
Solution

Tim,

 

You're looking for toscalar(). For a slightly more advanced example of how to use it (together with makeset() and in()), check out the last section of our aggregations tutorial.

 

To pseudocode your example, you'd be looking at something like:

let myDateTime = toscalar(Table1 | summarize max(TimeGenerated)); 

Table 2

| where TimeGenerated between ((myDateTime - 5m) .. (myDateTime + 5m)) 

| ...

Evgeny,

Perfect. Thanks. I knew there was something like that in there somewhere, but I was having trouble finding it in the language reference. I see it now under Scalar Functions.

Great example by Evgeny For some small things you do not even need scalar like: let WinComps = Heartbeat | where OSType == 'Windows' | distinct Computer; Perf | where Computer in (WinComps) | limit 10
1 best response

Accepted Solutions
best response confirmed by Tim Curwick (Copper Contributor)
Solution

Tim,

 

You're looking for toscalar(). For a slightly more advanced example of how to use it (together with makeset() and in()), check out the last section of our aggregations tutorial.

 

To pseudocode your example, you'd be looking at something like:

let myDateTime = toscalar(Table1 | summarize max(TimeGenerated)); 

Table 2

| where TimeGenerated between ((myDateTime - 5m) .. (myDateTime + 5m)) 

| ...

View solution in original post