How do I fill empty values with 0? - kql

I've got the following code
MyDatabase
| project SessionId, NumberOfAlarms
I've got some values on "NumberOfAlarms" which are a blank cell, and I want to put fill those cells with 0 instead. Is there an easy way to do this?

coalesce()
let MyTable = datatable(SessionId:int, NumberOfAlarms:int)
[
1,14 ,2,62, 3,int(null) ,4,11 ,5,int(null) ,6,37 ,7,12
];
MyTable
| project SessionId, NumberOfAlarms = coalesce(NumberOfAlarms, 0)
SessionId
NumberOfAlarms
1
14
2
62
3
0
4
11
5
0
6
37
7
12
Fiddle

Related

How to create a new column with a label for every 5 rows in sql?

I want to create a new column in my table.
I want this new column to give the label "done" for every 5 rows.
My table looks like this:
no type
1 a
2 a
3 a
4 a
5 a
6 a
7 a
8 a
9 a
10 a
and what I mean with the new column is:
no type flag
1 a
2 a
3 a
4 a
5 a done
6 a
7 a
8 a
9 a
10 a done
so the "done" label will always appear for every 5 rows.
I use postgres for this. How can I do that with query?
Thanks in advance.
You can use the row_number() function and the modulo operator:
select no, type,
case
when row_number() over (order by no) % 5 = 0 then 'done'
end as flag
from the_table
order by no;

Two Condition Where-clause SQL

I need to filter some rows when 2 conditions are met, but not excluding the other rows.
Table:
idRow idMaster idList
1 10 45
2 10 46
3 10 47
4 11 10
5 11 98
6 14 56
7 16 28
8 20 55
Example:
When:
idMaster=10 and id List=45 (only show this combination for idMaster 10)
idMaster=11 and idList=98 (only show this combination for idMaster 11)
list all other rows as well.
Expected result:
idRow idMaster idList
1 10 45
5 11 98
6 14 56
7 16 28
8 20 55
Running SQL Server 2014
I tried combinations of CASE IF but all cases only filter the idMaster=10,11 and idList=45,98, excluding the other rows
Although you didn't mentioned the database name, this following query logic will be applicable for all databases-
SELECT *
FROM your_table
WHERE idMaster NOT IN (10,11)
OR (idMaster = 10 AND idList = 45)
OR (idMaster = 11 AND idList = 98)
You can indeed do this with a (nested) case. Hopefully this helps you understand better.
case idMaster
when 10 then case idList when 45 then 1 end
when 11 then case idList when 98 then 1 end
else 1
end = 1
This might be the best though:
not (idList = 10 and idList <> 45 or idList = 11 and idList <> 98)
Overall it's usually beneficial to avoid repeating that list of values in multiple places. Both of these avoid the need to keep things in sync when changes come.

Update current and next row in table

I have a table with few fields like below
ID | OpeningBal | A | B | C | D | ClosingBal
Here opening balance of current day is the closing balance of previous day and closing balance is calculated with this formula
OpeningBal + A - B - (C + D) - C
but current data is wrong in this table because of wrong formula applied previously for Closing Balance. I have tried like looping through all the records of this table and update the closing balance to the actual value. I want to update the opening balance of next row with the closing balance of current row in this cursor but I don't have the ID of next row. Any thoughts?
Actual Result:
ID OPBal A B C D CLBal
1 0 80 4 6 0 90
2 90 8 6 0 0 104
5 104 5 4 0 9 122
7 122 10 3 5 0 140
expected result:
ID OPBal A B C D CLBal
1 0 80 4 6 0 64
2 64 46 6 0 0 104
5 104 5 4 0 9 96
7 96 10 3 5 0 93
update tablename set openingbalance=a.clcurrentrow from
(select LAG(closingbalance) over (order by id) clcurrentrow from tablename ) a
Thanks all. I got the solution using LAG and I will modify this based on my requirements.
SELECT
ID,
LAG(ClosingBalance,1,0.00) OVER (ORDER BY ID) PrevClosingBalance,
ClosingBalance
FROM Table1
UPDATE Table1
SET OpeningBalnace = #PrevClosingBalance
ClosingBalance = #PrevClosingBalance + A - B -(C+D)-C
WHERE ID = #ID

Double Join to get data from a table based on other two tables

I have three tables with the following key fields:
CONTRACTS
reference
package
EVENTS
reference
condition1
condition2
TRADES
reference
event_reference
Basically, what I would like to do is the following:
Get all the reference of the table EVENTS where the two conditions (condition1 and condition2) are met;
Hence, getting all the reference of the table TRADES where TRADES.event_reference = EVENTS.reference
Finally, getting the CONTRACTS.package where the CONTRACTS.reference = TRADES.reference (after having filtered the data at the point 2).
In order to do this, I have tried a JOIN statement:
SELECT CONTRACTS.package
FROM CONTRACTS
JOIN TRADES ON CONTRACTS.reference = TRADES.reference
JOIN EVENTS ON TRADES.event_reference = EVENTS.reference
WHERE EVENTS.condition1 = '1.511' AND EVENTS.condition2 IN (1,2)
However, the above (which is executed without errors) does not issue any result, and I would actually expect to see some.
I hence understand that I'm being wrong in the logic that I follow: could anyone please help?
EDIT: this is an example of how the data look like (in yellow, I have highlighted the data that would be touched in the query if it was working as I had it in mind:
...here is the expected result:
1 (package of 4, related to 11 which satisfies condition 1 and 2)
2 (package of 6, related to 13 which satisfies condition 1 and 2)
4 (package of 10, related to 16 which satisfies condition 1 and 2)
and here are the data to copy-paste them:
CONTRACTS
reference package
1 1
2 1
3 1
4 1
5 2
6 2
7 3
8 3
9 4
10 4
EVENTS
reference condition1 condition2
10 1.511 0
11 1.511 1
12 1.202 0
13 1.511 2
14 1.511 0
15 1.202 0
16 1.511 1
TRADES
reference event_reference
2 10
4 11
5 12
6 13
7 14
9 15
10 16
Your query looks OK
SQL Fiddle Demo
SELECT CONTRACTS.package
FROM CONTRACTS
JOIN TRADES ON CONTRACTS.reference = TRADES.reference
JOIN EVENTS ON TRADES.event_reference = EVENTS.reference
WHERE EVENTS.condition1 = 'true' AND EVENTS.condition2 = 'true'
OUTPUT
| package |
|---------|
| 1 |
| 2 |
| 4 |

Simple Math iOS?

So in my app I am trying to do a simple math in one of my methods without using a ton of if/else statements.
So I have an integer named 'StartInt' which is at max 13. Now what I need to get is FinishInt an integer that will be the result of this pattern:
StartInt: 13 FinishInt: 1
StartInt: 12 FinishInt: 2
StartInt: 11 FinishInt: 3
etc... all the way down until StartInt is 1 and FinishInt is 13. Anyway how would I accomplish this? I know this must be simple but I am just not that great in Math! :)
All the way down until StartInt is 0 and FinishInt is 13. Anyway how
would I accomplish this?
That won't quite work if startInt = 13 gives finishInt = 1 and you want a finishInt to increment 1 for each decrement of startInt. Check out the following table:
13 1
12 2
11 3
10 4
9 5
8 6
7 7
6 8
5 9
4 10
3 11
2 12
1 13
So you're off by 1 at either the beginning or end of your sequence. Nevertheless, it looks like you want something like this:
(int) calculateFinish(int startInt)
{
int finishInt = -1;
if (startInt >= 0 && startInt <= 13) {
finishInt = 14 - startInt;
}
return finishInt;
}
That'd give a value of 14 for finishInt when startInt = 0.