Less than or equal to date in set analysis - qlikview

I am trying to sum values where the week number is less than or equal to the current week. It works if i just want equal to current week but not if i want less than or equal.
This is what I've tried but nothing works.
=Sum({$<Week={"$(<=Week(Today()))"}>}Sales
=Sum({$<Week<={"$(=Week(Today()))"}>}Sales
Hopefully someone can help me.
thanks in advance

Hi you need operator (less or equal) in the beginning inside quotes.
In dollar expansion $(=expr) you always need equal to evaluate it. Try this:
=Sum({$<Week={"<=$(=Week(Today()))"}>}Sales)

Related

MONTHS_BETWEEN('2018-12-31', '2019-02-28') outputs 1?

Simply shown above, this has been causing inconsistencies with our data.
I'm currently writing an AMDP Logic that makes use of the difference between the months of each records, but surprisingly, the function MONTHS_BETWEEN() is not consistent for all cases and were seemingly skipping months for very specific cases. I don't know what is causing this. Is there a way to fix this inconsistency or some sort of alternate solution if none? Thanks.
Thanks to the comments, I figured out that the result from the function MONTHS_BETWEEN() is being calculated according to the number of days between. I initially assumed that it only uses the Month's value for the calculation, hence the expectation that Feb 2019 was 2 months away from Dec 2018.
Given that, I was able to come up with quite a fix that converts the dates into the first day of the month to get a guaranteed difference in months for any two given date values.
Here's a snippet of the SQL Code and the corresponding result:
This workaround is followed by an assumption that the dates being compared are all the last days of any given month. If that's not the case then a pre-processing by using the LAST_DAY() function will be needed to be inserted inside the NEXT_DAY() function in order for it to work similarly.
***EDIT:
Manually editing the string date value using LEFT() and CONCATENATING with '01' seems to be a more efficient and straightforward approach. I can't upload the screenshot but here's the working code:
SELECT MONTHS_BETWEEN(
LEFT('2018-12-31', 8) || '01',
LEFT('2019-02-28', 8) || '01'
) AS dmonths
FROM dummy

calculate into new column as percentage

Very new and learning SQL. Trying to calculate a percentage from two columns as such:
Select (total_deaths/total_cases)*100 AS death_percentage
From covid_deaths
I’m getting the column but it’s showing as an Integer and all values are zero.
I’ve tried using CAST to make it a decimal but i don’t have the syntax right. Very noob question but seems simple enough. Do I have to declare the numeric type of all calculated columns?
In addition to the answer linked by Stefan Zivkovik in a comment above, it may be good to handle division by zero. Even if you don't ever anticipate total_cases will be zero, someone may reuse this part of the code (for instance, if total_cases is later broken into subcategories).
You probably also want to ROUND to a certain number of decimal places
SELECT
CASE WHEN total_cases > 0 THEN
ROUND((total_deaths::NUMERIC/total_cases)*100,1)
END AS death_percentage
FROM covid_deaths
By not specifying an ELSE clause, the column will be null when total_cases is zero. If this doesn't work for your purposes, you could specify another default value (like zero) with ELSE.

What is the purpose of the string "- 4/24" from a record that holds a date?

I have an already generated script that uses the following code: ORDER BY TO_CHAR((A.VERIFIED_DTTM - 4/24),'YYYY-MM-DD'). The output of A.VERIFIED_DTTM is a simple date eg: 09-SEP-19.
I was my understanding that 4/24 is the same as saying 4 hours. I have tried removing this string from my query and received radically different results (not just sorting or ordering).
In another area of the script, I have a code that makes excellent sense (A.VERIFIED_DTTM >= sysdate - 30). This I understand.
Can anyone explain what is actually happening with this query line?
Thank you!
It is subtracting 4 hours and converting the date value to a string in the format of YYYY-MM-DD. This is probably a timezone adjustment.
To be honest, I'm not sure why it doesn't just use:
ORDER BY A.VERIFIED_DTTM
Removing it should not radically change the results.

MS Access If Function

I'm not using SQL at this point, I'm just trying to define a function using query design function in MS Access 2010.
What i'm trying to do:
So turns out that I have a 5 month spread (Jan,Feb..May) where each month is a column. Turns out that at times May has a value and January does not, but it should. All the values are either one or null.
What I'm trying to do is write an if function of this sort:
Jan15new: Iff([May-15]=1,[Jan-15]=1,[Jan-15])
However, when I run the query with this iff function I got a column full of negative ones that doesn't abide by the rules of this if function.
If you can shed somelight that would be great!
thanks,
This formula returns a 1 if May-15 = 1, otherwise returns whatever value is in Jan-15:
Jan15new: IIf([May-15]=1,1,[Jan-15])
If the values in your formula are Boolean values, you do not need to compare them to anything, and you should not be comparing them to numbers:
Jan15new: IIf([May-15]=True,[Jan-15]=True,[Jan-15])
That's actually meaningless, because it is equivalent to this:
Jan15new: IIf([May-15],[Jan-15],[Jan-15])

How to increase the chance that an old quote is selected

I have a database which contains quotes. I like to display random quotes. But with two conditions.
Quotes that are displayed the last week should not be selected.
How farther in the past a quote is displayed, the more chance it has to be to be selected.
The first is not so difficult. I can use the WHERE clause for this.
The second I do not know how to implement. Is there an easy way to do this, or need I to define a complex function for this?
One extension that could be nice also. Initially most quotes will not have been displayed. If it would be possible to give a quote that has not been displayed yet a much bigger chance to be displayed, then it would be quite nice.
A possible approach: order by the length of time in the past the quote was used, times a random number. This has the effect of giving a greater weight to records further in the past.
Here is pseudo code:
select quote from quotes
where current date - display date > 1 week
order by ((current date - display date - 1 week) * random number) desc
fetch first row only
You will have to adapt it to your system's date/time functions, since these are highly variable.
You probably need to use an intermediate step to apply a random number to each row, because directly ordering by rand() doesn't work on many systems.
Update: Here is a working MySQL example.