Kusto: How to transform a table to a table where columns and rows are defined by unique values of two columns? - kql

Say I have a table like this:
Computer
AppName
AppVersion
C1
App1
1.0
C1
App2
2.0
C2
App1
3.0
C2
App2
4.0
C3
App1
5.0
C3
App2
6.0
I'd like to transform it into a table like this:
Computer
App1
App2
C1
1.0
2.0
C2
3.0
4.0
C3
5.0
6.0
Is that possible in KQL?

pivot plugin
datatable(Computer:string, AppName:string, AppVersion:string)
[
,"C1" ,"App1" ,"1.0"
,"C1" ,"App2" ,"2.0"
,"C2" ,"App1" ,"3.0"
,"C2" ,"App2" ,"4.0"
,"C3" ,"App1" ,"5.0"
,"C3" ,"App2" ,"6.0"
]
| evaluate pivot(AppName, take_any(AppVersion))
Computer
App1
App2
C1
1.0
2.0
C2
3.0
4.0
C3
5.0
6.0
Fiddle

Related

Devide the time series data into groups using SQL

Now we have the battery usage data, mainly the voltage data.
The voltage decline as the day goes by. After a few days, the battery would be changed and the voltage would go back to the maximum value.
In this dataset, I hope to flag the 3 cycles the battery be used from the maximum voltage to the minimum voltage by some SQL clause if possible
voltage
date
4.2
2022-1-1
4.1
2022-1-10
4.0
2022-1-20
3.8
2022-1-23
3.6
2022-2-3
4.1
2022-2-5
4.0
2022-2-7
3.9
2022-2-25
3.8
2022-3-12
4.2
2022-3-15
4.1
2022-3-20
4.0
2022-3-23
3.5
2022-3-30
After the operation, the dataset should be like this:
voltage
date
type
4.2
2022-1-1
1
4.1
2022-1-10
1
4.0
2022-1-20
1
3.8
2022-1-23
1
3.6
2022-2-3
1
4.1
2022-2-5
2
4.0
2022-2-7
2
3.9
2022-2-25
2
3.8
2022-3-12
2
4.2
2022-3-15
3
4.1
2022-3-20
3
4.0
2022-3-23
3
3.5
2022-3-30
3

How to create a heatmap kind of visulalization for data

I have pandas dataframe that has the following structure
BID
EID
B1
1001,1002
B2
1001,1003,1006
B3
1004,1006,1008,1005
B4
1001,1002,1003,10004,1005,1008
I want to report the COUNT of how many common EIDs are there amongst the BIDs.
I want visualization in the following format
B1
B2
B3
B4
B1
n/a
1
0
2
B2
1
n/a
2
3
B3
0
2
n/a
4
B4
2
3
4
n/a
How can i achieve this ? Also higher the number in the cell, i want to highlight dark as it appears in heat map. Appreciate your help.
My logic is ..
Create a pandas dataframe with BID as index
Loop through each BID and compare it with other BIDs
Create a new column
Each of new column will be a list (This list will contain the count of EIDs)
How to Convert this dataframe to heat map ?
or any easy logic that I can implement ?
OK, I'm new here but here is my solution :)
first of all for each BID create a column with his data.
tmp = df["EID"].apply(pd.Series).set_index(df["BID"].values).T
next build the structure of the correlation with corr()
corr_df=tmp.corr()
Then change the values of the corr
import itertools
for a, b in itertools.combinations_with_replacement(tmp.columns, 2,):
corr_df.loc[[a],[b]]=len(set(tmp[a]) & set(tmp[b])) # len of equal items
corr_df.loc[[b],[a]]=len(set(tmp[a]) & set(tmp[b]))
corr_df.loc[[a],[a]]=np.NAN
print(corr_df)
output:
B1 B2 B3 B4
B1 NaN 1.0 0.0 2.0
B2 1.0 NaN 1.0 2.0
B3 0.0 1.0 NaN 3.0
B4 2.0 2.0 3.0 NaN
heatmap code:
import plotly.graph_objects as go # for visualization
fig = go.Figure(data=go.Heatmap(
z=corr_df,
x=corr_df.columns,
y=corr_df.columns,
hoverongaps = False,
colorscale="Greys",))
fig.update_layout(
title="HeatMap",
width=600
)
fig.show()
output:

Multi Level pivoting of dataframe

I have this dataframe:
Group
Feature 1
Feature 2
Class
First
5
4
1
Second
5
5
0
First
1
2
0
I want to do a multi level pivot in pandas to have something like this:
Group | Feature1 (class 1)| Feature (Class 2) | Feature2 (Class 1) | Feature 1(Class 2)
What if I want to select only one feature to work with?
Like this?
out = (df.assign(Class=df["Class"]+1)
.pivot(index="Group", columns="Class"))
print(out)
Feature 1 Feature 2
Class 1 2 1 2
Group
First 1.0 5.0 2.0 4.0
Second 5.0 NaN 5.0 NaN

Invoice table hold invoices based on their line items

I have a table that records the invoices within our company. I need to determine if there is multiple entries for a particular invoice number, and if there is then I need to look at the bin location. I need the results to look like a single invoice entry with all the qty, taxvalue, costvalue and discvalue summed together. The final output should only display a single line for each invoice with all values summed together.
Invoice | QtyInvoiced | TaxValue | CostValue | DiscValue | Warehouse | Bin
___________________________________________________________________________
1 1000 5.0 5.0 1.0 KT 23
1 500 5.0 5.0 1.0 KT Stage
2 1000 3.0 9.0 0.0 KT Stage
3 1000 5.0 5.0 1.0 KT 19
3 500 5.0 5.0 1.0 KT Stage
Results need to be:
Invoice | QtyInvoiced | TaxValue | CostValue | DiscValue | Warehouse | Bin
___________________________________________________________________________
1 1500 10.0 10.0 2.0 KT
2 1000 3.0 9.0 0.0 KT
3 1500 10.0 10.0 2.0 KT
This SHOULD get you what you want, you'll probably have to modify it slightly to fit as you didn't really provide a table structure.
SELECT invoice_nbr, SUM(qty) As TotalQty,
SUM(taxvalue) As TotalTaxValue,
SUM(costvalue) As TotalCostValue,
SUM(discvalue) As TotalDiscValue
FROM invoices
GROUP BY invoice_nbr
ORDER BY invoice_nbr;

SQL : Copying records within same table

SQL Server 2008 Table: table1
ID DESC TYP SUBSET VAL1 VAL2 VAL3 VAL4 PReview Country
1 DESC1 1 1 1.0 1.1 1.2 1.2 0 1
2 DESC1 1 1 2.0 1.1 1.2 1.2 0 1
3 DESC1 1 1 1.0 1.1 1.2 1.2 0 1
4 DESC2 2 1 3.0 2.1 1.7 1.8 0 1
5 DESC2 2 1 4.0 3.1 1.7 1.9 0 1
6 DESC2 2 1 5.0 6.1 1.5 1.6 0 1
13 DESC1 1 1 1.0 1.1 1.2 1.2 1 1
14 DESC1 1 1 2.0 1.1 1.2 1.2 1 1
15 DESC1 1 1 1.0 1.1 1.2 1.2 1 1
16 DESC2 2 1 1.0 6.1 1.7 1.2 1 1
17 DESC2 2 1 2.0 4.1 6.2 8.2 1 1
18 DESC2 2 1 1.0 8.1 7.2 1.9 1 1
I need to copy records which have preview = 1 into records which have preview 0. There is no way to uniquely define each record..just that they shoul dbe copied in an orderly manner.
Record 13 should be copied to Record 1
Record 14 should be copied to Record 2
Record 15 should be copied to Record 3
Thanks.
The basic idea is to "enumerate" (i.e. attach indexes to) both source and destination rows and then assign the source row with index 1 to the destination row with index 1, source row with index 2 to the destination row with index 2 etc:
UPDATE TABLE1
SET
[DESC] = SOURCE.[DESC],
TYP = SOURCE.TYP,
SUBSET = SOURCE.SUBSET,
VAL1 = SOURCE.VAL1,
VAL2 = SOURCE.VAL2,
VAL3 = SOURCE.VAL3,
VAL4 = SOURCE.VAL4,
PREVIEW = SOURCE.PREVIEW,
COUNTRY = SOURCE.COUNTRY
FROM (
SELECT DEST_ID, SRC.*
FROM
(SELECT ID DEST_ID, RANK() OVER (ORDER BY ID) R FROM TABLE1 WHERE PREVIEW = 0) DEST
JOIN (SELECT *, RANK() OVER (ORDER BY ID) R FROM TABLE1 WHERE PREVIEW = 1) SRC
ON SRC.R = DEST.R
) SOURCE
WHERE TABLE1.ID = SOURCE.DEST_ID
In plain English:
Attach indexes to rows where PREVIEW = 0, in order of ID (RANK() OVER (ORDER BY ID)).
Do the same where PREVIEW = 1.
Match source to destination indexes (JOIN ... ON SRC.R = DEST.R).
Update the table based on that matching.
Please be careful when number of destination rows is smaller than the number of source rows - the initial query execution will not update all the destination rows and the second execution may lead to the same source row being copied to a different destination row.
In effect, you'd be copying the same source row to multiple destination rows.
If you need the records to "copy over" the preview = 0 records, couldn't you just:
Delete the preview = 0 records
Replicate each preview = 1 record so you have two copies of each.
This sounds like what you're asking for.