KQL query to take a table with a field with array data and break out each value in the array - kql

I have a table like this:
Table1
Column1
Column2
1
["1","2","8"]
2
["2","3","7"]
What I would like to do is to break that table out into a list like:
Column1
column2
1
1
1
2
1
8
2
2
2
3
2
7
I haven't been able to get the right query to split the column 2 and pair this with the original column1 values.
How would I do that? Ta Steve

datatable(Column1:int, Column2:dynamic)
[
,1, dynamic(["1","2","8"])
,2, dynamic(["2","3","7"])
]
| mv-expand Column2
Column1
Column2
1
1
1
2
1
8
2
2
2
3
2
7
Fiddle

Related

Get combination over two columns

Help me to get SQL
column 1
column 2
Id
DEP-1
1
1
DEP-1
1
2
DEP-1
2
3
DEP-2
3
4
DEP-3
1
5
DEP-3
2
6
DEP-3
3
7
DEP-3
2
8
DEP-3
3
9
I have above table I need to write SQL to display all DISTINCT combination of column 2 over column 1. for example DEP-1 has 1 and 2 in column 2. my final table has to look below.
column 1
column 2
Id
column 2 map
DEP-1
1
1
1~2
DEP-1
1
2
1~2
DEP-1
2
3
1~2
DEP-2
3
4
3
DEP-3
1
5
1~2~3
DEP-3
2
6
1~2~3
DEP-3
3
7
1~2~3
DEP-3
2
8
1~2~3
DEP-3
3
9
1~2~3
select origin.*, c2_map
from origin
join (
select c1, group_concat("~", c2) as c2_map
from (
select distinct c1, c2 from origin
) t1
group by c1
) t2
on origin.c1 = t2.c1
Note:
group_concat(sep, value) is an aggregate function, it depends on the database you use, it means join the values together using sep

Select rows where column is two different values

I have a table like this:
id
item
value
1
Apple
1
2
Apple
7
3
Banana
5
4
Banana
6
5
Pear
1
6
Pineapple
7
How do I only select rows that have both [1,7] as values?
Output:
id
item
value
1
Apple
1
2
Apple
7
Edit: You can try using a sub query like this
SELECT * FROM TABLE
WHERE ITEM IN
(SELECT ITEM FROM TABLE WHERE VALUE IN (1,2)
GROUP BY ITEM HAVING COUNT(ITEM) = 2)

distinct value row from the table in SQL

There is a table with values as below,
Id Value
1 1
2 1
3 2
4 2
5 3
6 4
7 4
now need to write a query to retrieve value from the table and output should look as
ID Value
1 1
3 2
5 3
6 4
any suggestion ?
The query you want is nothing to do with being distinct, it's a simple aggregation of value with the minimum ID for each:
select Min(id) Id, value
from table
group by value

Select field 1 Where Field 2 ='value1' And field 2 ='value2' when Field 1 is Same

I am currently having troubles with filtering my SQL records. I need something like what it results in the following concept: Table is
A B
1 1
1 3
2 1
2 2
2 3
2 4
3 1
3 2
I want to select value of A , where B=1 and B=2 And B=3 when same A .... result is
A
2
Please help
You can use aggregation:
select a
from mytable
where b in (1, 2, 3)
group by a
having count(*) = 3
This assumes no duplicates in the table - else, you need to change the having clause to:
having count(distinct b) = 3

Increase row count in sql server after some count (say 25,000)

I have table like this
col1 col2 col3
3 5 8
4 5 5
5 5 5
3 3 3
4 5 6
I need to get table like below in SQL Server
col1 col2 col3 group
3 5 8 1
4 5 5 1
5 5 5 2
3 3 3 2
4 5 6 3
After some row count (say 25000 ) group column row count has to increase
(ex- if row count crosses 25,000 the group column value has to change to next number ie 25,001 - 2, 50001 - 3)
How to write a query in SQL Server?
You can use row_number to generate numbers and the do some calculations.
This will make on group of 5 rows.
select Column1,
Column2,
1 + ((row_number() over(order by Column3) - 1) / 5)
from YourTable