How can I get distinct values from two tables using union? - azure-data-explorer

Using KQL how can I get distinct values from two tables?
I tried the following
let brandstorelensscandevicedata = scandevicedata
| distinct Brand
| where Brand != "null";
let brandresellapp = usertrackerdevicedata
| distinct Brand
| where Brand != "null";
brandstorelensscandevicedata
| union kind=inner brandresellapp
But it gives me duplicates when a brand exists in both tables
How can I get unique brands from both tables and avoid duplicates?

// Data sample generation. Not part of the solution
let scandevicedata = materialize(range i from 1 to 10 step 1 | extend Brand = iff(rand()<0.2, "null", strcat("Brand_", toint(rand(9)))), x1 = rand(), x2 = rand());
let usertrackerdevicedata = materialize(range i from 1 to 10 step 1 | extend Brand = iff(rand()<0.2, "null", strcat("Brand_", toint(rand(9)))), x1 = rand(), x3 = rand());
// Solution starts here
union scandevicedata, usertrackerdevicedata
| distinct Brand
| where Brand != "null"
Brand
Brand_6
Brand_7
Brand_2
Brand_0
Brand_5
Brand_8
Fiddle

Related

How to retreive custom property corresponding to another property in azure

I am trying to write a kusto query to retrieve a custom property as below.
I want to retrieve count of pkgName and corresponding organization. I could retrieve the count of pkgName and the code is attached below.
let mainTable = union customEvents
| extend name =replace("\n", "", name)
| where iif('*' in ("*"), 1 == 1, name in ("*"))
| where true;
let queryTable = mainTable;
let cohortedTable = queryTable
| extend dimension = customDimensions["pkgName"]
| extend dimension = iif(isempty(dimension), "<undefined>", dimension)
| summarize hll = hll(itemId) by tostring(dimension)
| extend Events = dcount_hll(hll)
| order by Events desc
| serialize rank = row_number()
| extend dimension = iff(rank > 10, 'Other', dimension)
| summarize merged = hll_merge(hll) by tostring(dimension)
| project ['pkgName'] = dimension, Counts = dcount_hll(merged);
cohortedTable
Please help me to get the organization along with each pkgName projected.
Please try this simple query:
customEvents
| summarize counts=count(tostring(customDimensions.pkgName)) by pkgName=tostring(customDimensions.pkgName),organization=tostring(customDimensions.organization)
Please feel free to modify it to meet your requirement.
If the above does not meet your requirement, please try to create another table which contains pkgName and organization relationship. Then use join operator to join these tables. For example:
//create a table which contains the relationship
let temptable = customEvents
| summarize by pkgName=tostring(customDimensions.pkgName),organization=tostring(customDimensions.organization);
//then use the join operator to join these tables on the keyword pkgName.

How to rank rows in a table in sqlite?

How can I create a column that has ranked the information of the table based on two or three keys?
For example, in this table the rank variable is based on Department and Name:
Dep | Name | Rank
----+------+------
1 | Jeff | 1
1 | Jeff | 2
1 | Paul | 1
2 | Nick | 1
2 | Nick | 2
I have found this solution but it's in SQL and I don't think it applies to my case as all information is in one table and the responses seem to SELECT and JOIN combine information from different tables.
Thank you in advance
You can count how many rows come before the current row in the current group:
UPDATE MyTable
SET Rank = (SELECT COUNT(*)
FROM MyTable AS T2
WHERE T2.Dep = MyTable.Dep
AND T2.Name = MyTable.Name
AND T2.rowid <= MyTable.rowid);
(The rowid column is used to differentiate between otherwise identical rows. Use the primary key, if you have one.)

Select from table based on count

I have table like this:
id | name | type
-----------------
0 | firs | 2
1 | secs | 3
2 | this | 9
1 | thus | 3
I know id (it is not unique id) and type and I want to select records only if there is specified number of records with that id and type.
For one record I tried for example this:
select * from myTable
where
(select count(*) from myTable where myTable.id = 0 and myTable.type = 2) = 1;
This returns me all rows, not just the one row I want. Can anyone please tell me, what is the right way how to get the right result?
Your where statement looks like WHERE 1 = 1 and it's always true so SELECT gets every column from your table.
If you want select for example data where id = 1 and type = 3 and number of occurrences = 2 you can do something like that:
select * from myTable
where
(select count(*) from myTable where myTable.id = 1 and myTable.type = 3) = 2 and myTable.id = 1 and myTable.type = 3;

SQLite help for joining and count

I have 2 tables:
tblTransactions:
transID | type | userID
tblusers:
userID | name ;
Now I want my results to be like:
name |count( transactionType1(where type=1)) | transactionType1(where type=2)
mr.1 | 2 | 4
mr.2 | 3 | 5
Thanks for your help
Group by the user and then you can use aggregate functions like sum to count the type (with a condition)
select u.name,
sum(type = 1) as type1_count,
sum(type = 2) as type2_count
from tblusers u
left join tblTransactions t on u.userid = t.userid
group by u.name

selecting a row based on a number of column values in SQLite

I have a table with this structure:
id | IDs | Name | Type
1 | 10 | A | 1
2 | 11 | B | 1
3 | 12 | C | 2
4 | 13 | D | 3
except id nothing else is a FOREIGN or PRIMARY KEY. I want to select a row based on it's column values that are not PRIMARY KEY. I have tried the following syntax but it yields no results.
SELECT * FROM MyTable WHERE Name = 'A', Type = 1;
what am I doing wrong? What is exactly returned by a SELECT statement? I'm totally new to Data Base and I'm currently experimenting and trying to learn it. so far my search has not yield any results regarding this case.
Use and to add multiple conditions to your query
SELECT *
FROM MyTable
WHERE Name = 'A'
AND Type = 1;

Resources