I have the following script that I use for part of my count in payment_status from Mysql. I have seen some examples of this online, but I can't seem to figure out how to make this work WITH a Null instance. Any insight would be appreciated.
Note: The way I have it now doesn't work when Null is in the database. I'm guessing because that isn't an inserted word?
Objective: To add $row['payment_status'] == "Null" to this with the rest of the script.
if($row['payment_status'] == "Completed" || $row['payment_status'] == "Free" || $row['payment_status'] == "Cancelled" || $row['payment_status'] == "Null")
if payment_status column has default value NULL, Then try below change
if($row['payment_status'] == "Completed" || $row['payment_status'] == "Free" || $row['payment_status'] == "Cancelled" || $row['payment_status'] == null)
Related
Trying to return the datetime if and of the three values are entered in the specified columns
Current function (works well)
function onEdit(e) {
var sheet = e.source.getActiveSheet()
if(sheet.getName() !== 'Feb 22' || e.range.columnStart !== 10 || e.range.rowStart < 0) return;
e.range.offset(0,1).setValue(e.value =='Pending' ? new Date() : null)
}
What I am trying to do is instead of one value, I want two more values so: 'Pending', 'Rejected', 'Non-Recvd'. If any of these are selected in the drop down list, return the current datetime.
Any help is appreciated
I have a super long value for a key in a json file. Is there any way to format this single line into multiple lines for easier reading? Thank you so much!
"transforms.route.topic.expression":"if (value.source.table == 'test_item') {'rec.test_item'} else if (value.after != null && value.after.item_channel == 0){'rec.test_item.order_item.item_channel0'} else if (value.after != null && value.after.item_channel == 1) {'rec.test_item.order_item.item_channel1'} else if (value.after != null && value.after.item_channel == 2) {'rec.test_item.order_item.item_channel2'} else if (value.after != null && value.after.item_channel == 3) {'rec.test_item.order_item.item_channel3'} else if (value.after != null && value.after.item_channel == 4) {'rec.test_item.order_item.item_channel4'} else null"
"transforms.route.topic.expression":"if (value.source.table == 'test_item') {'rec.test_item'} else if (value.after != null && value.after.item_channel == 0){'rec.test_item.order_item.item_channel0'} else if (value.after != null && value.after.item_channel == 1) {'rec.test_item.order_item.item_channel1'} else if (value.after != null && value.after.item_channel == 2) {'rec.test_item.order_item.item_channel2'} else if (value.after != null && value.after.item_channel == 3) {'rec.test_item.order_item.item_channel3'} else if (value.after != null && value.after.item_channel == 4) {'rec.test_item.order_item.item_channel4'} else null"
It could be split into lines in an array, which can be joined together again later, e.g.:
"transforms.route.topic.expression": [
"if (value.source.table == 'test_item') {'",
"rec.test_item'",
"} else if (value.after != null && value.after.item_channel == 0){",
"'rec.test_item.order_item.item_channel0'",
"} else if (value.after != null && value.after.item_channel == 1) {",
"'rec.test_item.order_item.item_channel1'",
"} else if (value.after != null && value.after.item_channel == 2) {",
"'rec.test_item.order_item.item_channel2'",
"} else if (value.after != null && value.after.item_channel == 3) {",
"'rec.test_item.order_item.item_channel3'",
"} else if (value.after != null && value.after.item_channel == 4) {",
"'rec.test_item.order_item.item_channel4'",
"} else null"
]
If using e.g. JavaScript, then join like this:
json["transforms.route.topic.expression"] = json["transforms.route.topic.expression"].join("");
Can someone please explain to me why this:
SELECT
A.id,
A.name,
B.id AS title_id
FROM title_information AS A
JOIN titles B ON B.title_id = A.id
WHERE
A.name LIKE '%testing%'
is considerably slower (6-7 times) than this:
SELECT
A.id,
A.name,
B.id AS title_id
FROM (SELECT id, name FROM title_information) AS A
JOIN titles B ON B.title_id = A.id
WHERE
A.name LIKE '%testing%'
I know it's probably hard to answer this question without knowing full details about the schema and MySQL configuration, but I'm looking for any generic reasons why the first example could be so significantly slower than the second?
Running EXPLAIN gives this:
|| *id* || *select_type* || *table* || *type* || *possible_keys* || *key* || *key_len* || *ref* || *rows* || *Extra* ||
|| 1 || SIMPLE || B || index || || id || 12 || || 80407 || Using index ||
|| 1 || SIMPLE || A || eq_ref || PRIMARY,id_UNIQUE,Index 4 || PRIMARY || 4 || newsql.B.title_id || 1 || Using where ||
and
|| *id* || *select_type* || *table* || *type* || *possible_keys* || *key* || *key_len* || *ref* || *rows* || *Extra* ||
|| 1 || PRIMARY || B || index || || id || 12 || || 80407 || Using index ||
|| 1 || PRIMARY || <derived2> || ALL || || || || || 71038 || Using where; Using join buffer ||
|| 2 || DERIVED || title_information || index || || Index 4 || 206 || || 71038 || Using index ||
UPDATE:
A.id and B.id are both PRIMARY KEYS, while A.name is an index. Both tables have around 50,000 rows (~15MB). MySQL configuration is pretty much a default one.
Not sure if that helps (or if it adds more to the confusion - as it does for me) but using more generic LIKE statement that is likely to have more matching fields (e.g. "LIKE '%x%'") makes the first query run considerably faster. On the other hand, using "LIKE '%there are no records matching this%'" will make the second query a lot faster (while the first one struggles).
Anyone can shed some light on what's going on here?
Thank you!
This is speculation (my powers of reading MySQL explain output are weaker than they should be, because I want to see data flow diagrams).
But here is what I think is happening. The first query is saying "Let's go through B and look up the appropriate value in A". It then looks up the appropriate value using the id index, then it needs to fetch the page and compare to name. These accesses are inefficient, because they are not sequential.
The second version appears to recognize the condition on name as being important. It is going through the name index on A and only fetching the matching rows as needed. This is faster, because the data is in the index and few pages are needed for the matching names. The match to B is then pretty simple, with only one row to match.
I am surprised at the performance difference. Usually, derived tables are bad performance-wise, but this is clearly an exception.
I have the following expression in a Derived Column transformation.
((DT_STR,1,1252)outGender == "M" || (DT_I1)outGender == 1) ? 1 : (((DT_STR,1,1252)outGender == "F" || (DT_I1)outGender == 2) ? 2 : 3
When I run the package, if the "outGender" column contains a "1" or "M", the GenderId column becomes 1. If "outGender" is "2", the GenderId column is "2". So far, this is correct. If, however, "outGender" contains "F" then GenderId will be NULL. This is wrong.
Why does this happen and how do I fix it? If I switch the condition strings, placing the "F" conditions in the first part of the expression and "M" conditions in the second part, then NULL appears in the GenderId column for rows when the "outGender" column contains "M" values.
So this transform works fine without casting your values as integers:
((DT_STR,1,1252)outgender == "M" || (DT_STR,1,1252)outgender == "1") ? 1 : (((DT_STR,1,1252)outgender == "F" || (DT_STR,1,1252)outgender == "2") ? 2 : 3)
And if outGender is already a DT_STR type you can simplify it and just use
(outgender == "M" || outgender == "1") ? 1 : ((outgender == "F" || outgender == "2") ? 2 : 3)
I'm looking for an extended answer to the question asked here:
Determine Whether Two Date Ranges Overlap
where any of the dates in either date range can be null. I've come up with the following solution, but I'm not sure if it can be simplified further.
(StartA == NULL || StartA <= EndB) &&
(EndA == NULL || EndA >= StartB) &&
(StartB == NULL || StartB <= EndA) &&
(EndB == NULL || EndB >= StartA)
Assuming:
DateTime ranges of StartA to EndA and StartB to EndB
EDIT: Sorry I quickly threw the above logic together, which seems to fail when either range's start and end dates are NULL. See David's solution below for a better & well-explained approach.
This case can be handled by a slight generalization of Charles Bretana's excellent answer to that question.
Let CondA Mean DateRange A Completely After DateRange B (True if StartA > EndB)
Let CondB Mean DateRange A Completely Before DateRange B (True if EndA < StartB)
In this case, assuming you want a null date to represent "no starting/ending bound," the conditions are modified. For CondA, for instance, in order for DateRange A to be completely after DateRange B, DateRange A must have a defined starting time, DateRange B must have a defined ending time, and the starting time of A must be after the ending time of B:
CondA := (StartA != null) && (EndB != null) && (StartA > EndB)
CondB is the same with A and B switched:
CondB := (StartB != null) && (EndA != null) && (StartB > EndA)
Continuing,
Then Overlap exists if Neither A Nor B is true
Overlap := !(CondA || CondB)
and
Now deMorgan's law, I think it is, says that
Not (A Or B) <=> Not A And Not B
Overlap == !CondA && !CondB
== ![(StartA != null) && (EndB != null) && (StartA > EndB)] &&
![(StartB != null) && (EndA != null) && (StartB > EndA)]
== [(StartA == null) || (EndB == null) || (StartA <= EndB)] &&
[(StartB == null) || (EndA == null) || (StartB <= EndA)]
I think this is actually a bit more robust than the solution you developed, because if EndB == NULL but StartA is not null, your first condition will wind up comparing StartA <= NULL. In most languages I'm familiar with, that's an error condition.
Without considering nulls, answer is
(StartA <= EndB) and (EndA >= StartB)
(see this for detailed explanation)
considering nulls for start and end dates,
Using C Ternary operator syntax:
(StartA != null? StartA: EndB <= EndB != null? EndB: StartA) &&
(EndA != null? EndA: StartB >= StartB != null? StartB: EndA)
Or C# 4.x style null operators:
(StartA??EndB <= EndB??StartA) && (EndA??StartB >= StartB??EndA)
or in SQL:
(Coalesce(StartA, EndB) <= Coalesce(EndB, StartA))
And (Coalesce(EndA, StartB ) <= Coalesce(StartB , EndA))
Explanation:
consider the non-null answer:
(StartA <= EndB) and (EndA >= StartB)
Now, consider that StartA is null, indicating that date range A has existed since beginning of time (BOT). In that case, DateRangeB can never be before DateRangeA. So first condition, (StartA(BOT) <= EndB) will ALWAYS be true, no matter what EndB is. So change this expression so that instead of comparing null with EndB, when StartA is null, compare EndB with itself
No matter what EndB is, the expression EndB <= EndB will be true.
(We could create variables to represent BOT and EOT, but this is easier).
Do the same for other three input variables.
That is probably as 'simple' as you can get it, although I haven't actually proven it.
It probably isn't worth it to simplify further, since that block ends up being about 8 operations in the worst case (4 on average thanks to short-circuit evaluation).
All answers are based if the condition is true. I'would like to add some note here.
1- The DateTime variable type is a struct and you can not set it to null unless that you are using nullable type like "DateTime?"
2- To find the overlap range follow the following steps
DateTime? StartOverLap = null,EndOverLap = null;
if (StartA != null && StartB != null)
{
StartOverLap = StartA > StartB ? StartA : StartB;
}
else if (StartA == null && StartB != null)
{
StartOverLap = StartB;
}
else if (StartA != null && StartB == null)
{
StartOverLap = StartA;
}
if (EndA != null && EndB != null)
{
EndOverLap = EndA < EndB ? EndA : EndB;
}
else if (EndA == null && EndB != null)
{
EndOverLap = EndB;
}
else if (EndA != null && EndB == null)
{
EndOverLap = EndA;
}
if (StartOverLap != null && EndOverLap == null)
{
if (EndOverLap < StartOverLap)
{
StartOverLap = null;
EndOverLap = null;
}
}