Using Kusto, I want to write a query to see the average duration of events and total count of those events as well. I am able to do it in two queries like this but is it possible to do this in 1 query?
dependencies
| where type == 'SQL' and operation_Name == 'something'
| summarize avg(duration) by data
| order by data asc
dependencies
| where type == 'SQL' and operation_Name == 'something'
| summarize count() by data
| order by data asc
This is giving me what I want in two separate results. For example I get this:
A 1.2
B 1.4
C 4.5
and
A 5
B 90
C 78
but what I want is this
A 1.2 5
B 1.4 90
C 4.5 78
See summarize operator for syntax
T | summarize [SummarizeParameters] [[Column =] Aggregation [, ...]] [by [Column =] GroupExpression [, ...]]
dependencies
| where type == 'SQL' and operation_Name == 'something'
| summarize avg(duration), count() by data
| order by data asc
Related
As the title says, I'd like to find an efficient way to calculate aggregates over groups of rows without collapsing those rows together. For an example I want to create the mean column in the table below.
|------------|---------|-------------|
| category | value | mean(value) |
|------------|---------|-------------|
| A | 1 | 3 |
|------------|---------|-------------|
| A | 3 | 3 |
|------------|---------|-------------|
| A | 5 | 3 |
|------------|---------|-------------|
| B | 1 | 1.5 |
|------------|---------|-------------|
| B | 2 | 1.5 |
|------------|---------|-------------|
So far, the best way I've found to do this is:
T
| join kind=leftouter (T | summarize avg() by category) on category
This seems to be causing performance problems. I'm also aware of a way of doing it using partition by, but need to support having more than 64 categories.
Am I missing a good way of doing this task?
Here you go:
let MyTable = datatable(Category:string, value:long) [
"A", 1,
"A", 3,
"A", 5,
"B", 1,
"B", 2
];
let Avgs = MyTable | summarize avg(value) by Category;
MyTable | lookup (Avgs) on Category
This will output exactly what you want.
Explanation:
First you create a temporary table (using a let statement) named Avgs, where you'll have the average per Category.
Your main statement is to output MyTable, but for every category you want to also display the relevant value from Avgs, which you achieve by using the lookup operator.
I need to count the number of unique values and to group them by day.
For example, I have :
user id | Date
1 | 01.-11-2018
1 | 01-11-2018
2 | 02-11-2018
3 | 03-11-2018
1 | 03-11-2018
In result i need
01-11-2018 | 1
02-11-2018 | 1
03-11-2018 | 2
I need do it in Klipfolio
You can use the GROUPBY() function with the aggregation in the method parameter set to Count Distinct. For example, if your dates are in column A of a spreadsheet and your values are in column B, your formula would look like this:
GROUPBY(#A:A,#B:B,Count Distinct)
I have a dataset like shown below (except the Ser_NO, this is the field i want to create).
+--------+------------+--------+
| CaseID | Order_Date | Ser_No |
+--------+------------+--------+
| 44 | 22-01-2018 | 1 |
+--------+------------+--------+
| 44 | 24-02-2018 | 3 |
+--------+------------+--------+
| 44 | 12-02-2018 | 2 |
+--------+------------+--------+
| 100 | 24-01-2018 | 1 |
+--------+------------+--------+
| 100 | 26-01-2018 | 2 |
+--------+------------+--------+
| 100 | 27-01-2018 | 3 |
+--------+------------+--------+
How can i achieve a serial number for each CaseId based on my dates. So the first date in a specific CaseID gets number 1, the second date in this CaseID gets number 2 and so on.
I'm working with T-SQL btw,
I've tried a few things:
CASE
WHEN COUNT(CaseID) > 1
THEN ORDER BY (Order_Date)
AND Ser_no +1
END
Thanks in advance.
First of all, although I don't understand what you did, it gives you what you wanted. The serial number is assigned by date order. The problem I can see is that the result shows you the rows in the wrong order (1, 3, 2 instead of 1, 2, 3).
To sort that order you can try this:
SELECT *, ROW_NUMBER() OVER (PARTITION BY caseid ORDER BY caseid, order_date) AS ser_no
FROM [Table]
Thanks for your reply,
Sorry for the misunderstanding, because the ser_no is not yet in my table. That is the field a want to calculate.
I finished it myself this morning, but it looks almost the same like your measure:
RANK() OVER(PARTITION BY CaseID ORDER BY CaseID, Order_Date ASC
User wants a count of unique sessions per week in application insights. I have the query working, including a pivot, but the Week columns are out of order. I would prefer if they were in order.
pageViews
| where timestamp < now()
| summarize Sessions= dcount(session_Id)
by Week=bin(datepart("weekOfYear", timestamp), 1), user_AuthenticatedId
| order by Week
| evaluate pivot(Week, sum(Sessions))
| join kind=innerunique (pageViews
| summarize MostRecentRequest = max(timestamp) by user_AuthenticatedId)
on $right.user_AuthenticatedId == $left.user_AuthenticatedId
| project-away user_AuthenticatedId1
I've tried ordering by timestamp before the summarize, and ordering by week after the summarize (still in there) and no luck.
There's currently a "trick" that will work: serialize right after your order by
pageViews
| where timestamp < now()
| where isnotempty(user_AuthenticatedId)
| summarize Sessions= dcount(session_Id)
by Week=bin(datepart("weekOfYear", timestamp), 1), user_AuthenticatedId
| order by Week
| serialize // <--------------------------------- RIGHT HERE
| evaluate pivot(Week, sum(Sessions))
| join kind=innerunique (pageViews
| summarize TotalSessions=dcount(session_Id), MostRecentRequest = max(timestamp) by user_AuthenticatedId)
on $right.user_AuthenticatedId == $left.user_AuthenticatedId
| project-away user_AuthenticatedId1
| top 100 by TotalSessions desc
gets me this in workbooks, with the weeks in descending order (I also added total session count to sort/top by with some custom column settings set):
the custom settings I have for the column settings in workbooks:
delete all the #'d columns that are there by default and add one for ^[0-9]+$ set to heatmap:
I refactored query a bit for my own comprehension. I took the the left and right into "views". Thought I'd share.
let users_MostRecent_Session =
pageViews
| summarize
TotalSessions=dcount(session_Id)
, MostRecentRequest = max(timestamp)
by
user_AuthenticatedId
;
//
let users_sessions_ByWeek =
pageViews
| where timestamp < now()
| where isnotempty(user_AuthenticatedId)
| summarize
Sessions= dcount(session_Id)
by
Week=bin(datepart("weekOfYear", timestamp), 1)
, user_AuthenticatedId
| order by Week
| serialize
| evaluate pivot(Week, sum(Sessions))
;
//
//
users_sessions_ByWeek
| join kind=innerunique
users_MostRecent_Session
on user_AuthenticatedId
| project-away user_AuthenticatedId1
| top 100 by TotalSessions desc
I'm trying to get a result from two sqlite tables that contain the same columns but have no other relation. Both have the date and amount columns and all I want is a new table as a result to show dates and amounts from both tables.
Table A
+----------+-------+
| date | amount|
+----------+-------+
|10-01-2013| 3.8 |
|12-23-2104| 4.2 |
+----------+-------+
and
Table B
+----------+-------+
| date | amount|
+----------+-------+
|10-03-2013| 2.4 |
|12-28-2014| 3.5 |
+----------+-------+
And the desired table would be
+----------+----------+---------+
| date | A.amount | B.amount|
+----------+----------+---------+
|10-01-2013| 3.8 | NULL |
|10-03-2013| NULL | 2.4 |
|12-23-2104| 4.2 | NULL |
|12-28-2014| NULL | 3.5 |
+----------+----------+---------+
I tried many posts in the forum but I couldn't find any that match my need.
Could you help?
What you need is not a JOIN is a UNION.
see:
UNIONS in SQLITE
Something like:
SELECT Date, Amount as Amount1, NULL AS Amount2
FROM TableA
UNION ALL
SELECT Date, NULL AS Amount1, Amount as Amount2
FROM TableB