Count in xquery - count

I'm trying to solve this question but this code give me the result without counting it. What i mean it's that i need it to be a 13 but the result its divided by rows with a value of 1.
1
1
1
1
1
1
1
1
1
1
1
1
1
This is the question and this is what i have done.
List the number of countries that have a border (element ) in a country that has a Buddhist religion (element , value "Buddhist"). The correct query has to return the value 13.
for $var in /mondial/country
let $religion:=/mondial/country[religions="Buddhist"]/data(#id)
where $var /border/#country=$religion
return data (count($var/name)

This should get you there. I changed variable names to make things a bit more readable:
let $country := mondial/country
let $buddhist := $country[religions[./text()="Buddhist"]]/#id
return count($country[.//border[#country=$buddhist]])
Output:
13

Rather than returning the count for each item as you iterate, return each selected item and assign it to a variable, then you can get a count of the items of the sequence assigned to that variable:
let $names :=
for $var in /mondial/country
let $religion:= /mondial/country[religions="Buddhist"]/data(#id)
where $var/border/#country=$religion
return $var/name
return count($names)
You could also do this in a single XPath statement:
count(/mondial/country[border/#country=/mondial/country[religions="Buddhist"]/data(#id)])

Related

Kusto SCAN makes me crazy

could someone explain this!?
range i from 1 to 3 step 1
| scan with_match_id=matchId declare (s: long, n: long=0) with
(
step s1: true => s=1, n=s1.n + 1;
step s2: true => s=2, n=s2.n + 1;
)
Output
i
s
n
matchId
1
1
1
0
2
2
1
0
2
1
1
1
3
2
1
1
3
1
1
2
Why is the first row (i=1) not matched by s2?
Why is n always 1?
That SCAN Operator makes me totally crazy. With one step it's more or less understandable, but with more than one step, I don't get it...
Thanks a lot!
Steps are always executed in their definition order.
We always start with the 1st step (s1) and then move to the 2nd, the 3rs etc. (note that a step might repeat itself multiple times).
By this definition, the 1st input line might match the 1st step, however, it cannot match any other step.
2.
n is always 1 because each step is matched only once.
In step s1 you are refering to s1.n.
s1.n is the previous value of s1.
It has a value only if you execute this step multiple time.
Since you don't, the value take is the default value you have defined when you declared n, which is 0 (n: long=0).
Same thing for step s2.

How to count equal values of the same element name [xQuery]

Here is an example:
`
<bracketQualifier>
<bracketSequenceNumber>1</bracketSequenceNumber>
</bracketQualifier>
<bracketQualifier>
<bracketSequenceNumber>1</bracketSequenceNumber>
</bracketQualifier>
<bracketQualifier>
<bracketSequenceNumber>1</bracketSequenceNumber>
</bracketQualifier>
`
What i need to do is if bracketSequenceNumber holds the same value trow an exception.
Number of elements is N there can be more than 3. How can i achieve this using xquery.
I tried something like this without success and i cant say i understand xQuery completley:
`
let $count := ( for $bracketSequenceNumber in $bracketQualifier/bracketSequenceNumber return count(bracketQualifier[#bracketSequenceNumber = $bracketSequenceNumber ])) return
if($GDSN_PriceSyncPriceSegmentTM/value ='250' and $count >= 1) then something
`
You can use
if (count(//bracketSequenceNumber)
!= count(distinct-values(//bracketSequenceNumber) then ...
If you actually want to find the duplicates, use group by in XQuery 3.1 to process each group of equal values and test whether the group size is 2 or more.

Can I use tabular parameters in Kusto user-defined functions

Basically I'd like to pass in a set of field values to a function so I can use in/!in operators. I'd prefer to be able to use the result of a previous query rather than having to construct a set manually.
As in:
let today = exception | where EventInfo_Time > ago(1d) | project exceptionMessage;
MyAnalyzeFunction(today)
What is then the signature of MyAnalyzeFunction?
See: https://learn.microsoft.com/en-us/azure/kusto/query/functions/user-defined-functions
For instance, the following will return a table with a single column (y) with the values 2 and 3:
let someTable = range x from 2 to 10 step 1
;
let F = (T:(x:long))
{
range y from 1 to 3 step 1
| where y in (T)
}
;
F(someTable)

Teradata : using case statement in Where clause

My question is about using case statement in where clause to check for date and assign values to columns. My sample code include.
select * from table
where
column 1 > 10 and
case when column 2 = 1
then
column 3<= 10 and column 4 between (1st day of prev month) and (prev month end) or column 5 = '8888-01-01'
else
column 4 between (1st day of this month) and (yesterday)
end ;
when I am running this code. I am getting 3706 syntax error:expected something in between field and '='.
How to fix this ?
A CASE statement will always return a value or NULL (if none of the conditions matches), so you can use it in your WHERE clause. There are a couple ways to format your CASE statement:
Format 1
CASE
WHEN <condition> THEN <some_expression>
WHEN <another_condition> THEN <another_expression>
ELSE <final_expression>
END
-- Example
CASE
WHEN col1 = 10 THEN 'Y'
WHEN col1 = 20 THEN 'N'
ELSE 'N/A'
END
Format 2
CASE <expression>
WHEN <value> THEN <expression>
WHEN <another_value> THEN <another_expression>
ELSE <final_expression>
END
-- Example
CASE col1
WHEN 10 THEN 'Y'
WHEN 20 THEN 'N'
ELSE 'NA'
END
I'm not sure what you're trying to do with your sample code, but it looks more like pseudo-code and will not work as-is. Your CASE statement is not formatted properly and your column references like column 1 will not work that way. If your column is actually named column 1, then you need to put double-quotes around it:
select * from table where "column 1" > 10
Can you please describe a little more clearly what exactly you are trying to do?
A CASE expression can't be used to create some kind of dynamic conditions. Write it as a bunch of AND/OR conditons:
select * from table
where
column 1 > 10 and
(
( column 2 = 1 and
(column 3<= 10 and column 4 between (1st day of prev month) and (prev month end) or column 5 = '8888-01-01')
)
or
column 4 between (1st day of this month) and (yesterday)
);
Double check the logic, the precedence of logical operators is
parenthesis
NOT
AND
OR

How to filter duplicate list items

i have list of items IList with data that looks list this:
GenId TestMode
1 0
1 1
3 0
3 1
4 NULL
2 NULL
i want to remove the index of GenId from my list that have TestMode == 0 if the same GenId has a TestMode == 1.
does someone have a terse way of doing this?
LINQ is very good at running operations against collections of objects. The following query should give you what you are looking for:
var query = list.Where(i => i.TestMode == 1 ||
!list.Exists(i2 => i2.GenId == i.GenId && i2.TestMode == 1));
foreach (var item in query) {
// do something with items.
}
What this does is looks for an item where TestMode is equal to 1 (and includes if so), or otherwise checks to see if there is another element where TestMode is equal to 1, and excludes if that records exists.

Resources