How do I get the aggr of two aggrs in QlikView? - reporting

If I want to find the maximum value of a column from two states aggregated by a member's ID, should this work?
=Aggr(
MaxString(
Aggr(NODISTINCT MinString({[State1]}DATE_STRING),MBR_ID)
+
Aggr(NODISTINCT MinString({[State2]}DATE_STRING),MBR_ID)
) , MBR_ID)
So if I had this data:
MBR ID DATE_STRING
1 20120101
1 20120102
1 20120103
And State1 had 20120101 selected and State2 has 20120103 selected, my expression would return 20120103 for member 1.
Thanks!
Edit: In SQL, this would look like:
WITH MinInfo (DATE_STRING, MBR_ID)
AS (SELECT MIN(DATE_STRING), MBR_ID FROM Table WHERE TYPE IN ('State1', 'State2') GROUP BY MBR_ID, TYPE)
SELECT MAX(DATE_STRING) DATE_STRING, MBR_ID FROM MinInfo GROUP BY MBR_ID

It would be easier to accomplish your goal if you convert your that to an actual date field
Assuming that you are using a chart where MBR_ID is the Dimension, if you want the maximum date (latest date) you can do the following:
=nummax(Max({[State1]}DATE_STRING),Max({[State2]}DATE_STRING))
To convert to a date, you can use this function:
date#(DATE_STRING,'[text format of the date]')
(The date format looks like YYYYMMDD to me, but if its day then month, you would use YYYYDDMM)
I'd suggest you format it in the script, so that you wont have to worry about it every time you need to use that date.

Related

How to restrict query result from multiple instances of overlapping date ranges in Django ORM

First off, I admit that I am not sure whether what I am trying to achieve is possible (or even logical). Still I am putting forth this query (and if nothing else, at least be told that I need to redesign my table structure / business logic).
In a table (myValueTable) I have the following records:
Item
article
from_date
to_date
myStock
1
Paper
01/04/2021
31/12/9999
100
2
Tray
12/04/2021
31/12/9999
12
3
Paper
28/04/2021
31/12/9999
150
4
Paper
06/05/2021
31/12/9999
130
As part of the underlying process, I am to find out the value (of field myStock) as on a particular date, say 30/04/2021 (assuming no inward / outward stock movement in the interim).
To that end, I have the following values:
varRefDate = 30/04/2021
varArticle = "Paper"
And my query goes something like this:
get_value = myValueTable.objects.filter(from_date__lte=varRefDate, to_date__gte=varRefDate).get(article=varArticle).myStock
which should translate to:
get_value = SELECT myStock FROM myValueTable WHERE varRefDate BETWEEN from_date AND to_date
But with this I am coming up with more than one result (actually THREE!).
How do I restrict the query result to get ONLY the 3rd instance i.e. the one with value "150" (for article = "paper")?
NOTE: The upper limit of date range (to_date) is being kept constant at 31/12/9999.
Edit
Solved it. In a round about manner. Instead of .get, resorted to generating values_list with fields from_date and myStock. Using the count of objects returned; appended a list with date difference between from_date and the ref date (which is 30/04/2021) and the value of field myStock, sorted (ascending) the generated list. The first tuple in the sorted list will have the least date difference and the corresponding myStock value and that will be the value I am searching for. Tested and works.

Split up date range into chunks to join back into one table KDB+/Q

I have a table that is being joined like so
result: select from table where date within (sd;ed)
where sd and ed span multiple months (like sd:2021.07.01 ed:2021.09.30). The table that I'm querying from has a break if you take more than a month, so to get the result I need, I have to do something like the following:
result: uj(uj(select from table where date within (2021.07.01;2021.07.30);select from table where date within (2021.08.01;2021.08.31));select from table where date within (2021.09.01;2021.09.30))
How can I make this dynamic for any sd and ed? That is, how can I break up time range into first days of months, last days of months, and join them all into one table cleanly? My initial idea was to divide the days in the range x amount of time, to be input by a user, then add the number of days that results to the sd to get frames, but that got messy.
Something like this should chunk it for you:
raze{select from t where date within x}each(first;last)#\:/:d group"m"$d:sd+til 1+ed-sd
Do not use where date.month=x as you had suggested - at least not for historical queries
One option for converting your start and end dates into an iterable list of dates might be:
f:{0N 2#(x,raze -1 0+/:`date$mx+1+til(`month$y)-mx:`month$x),y}
Where x is start date and y is end date.
f[2021.07.14;2022.02.09]
2021.07.14 2021.07.31
2021.08.01 2021.08.31
2021.09.01 2021.09.30
2021.10.01 2021.10.31
2021.11.01 2021.11.30
2021.12.01 2021.12.31
2022.01.01 2022.01.31
2022.02.01 2022.02.09
Then you could run:
{select from t where date within x} each f[sd;ed]
And join the results using raze or (uj/)

syntax help for comparison logic

I am trying to find out the loss of portfolios from the current month to the prior month.
What I want to see is what missing, basically when we compare the two months together I want to display what is not matching between the two
I have put this expression.
If([portfolio_code Current ]=[portfolio_code_Prior])
Then(Count([portfolio_code Current ])
Else(0)
I am not sure my if syntax is incorrect I just wanted to give an example
any insights?
[enter image description here]
Assuming there is one portfolio per row the count of portfolios that were in last month and not this month is...
total(
If ( [portfolio_code Current ] <> [portfolio_code_Prior] && [portfolio_code_Prior] is not null)
Then ( 1 )
Else ( 0 )
)
or
total(
case
when [portfolio_code Current ] <> [portfolio_code_Prior] and
[portfolio_code_Prior] is not null
then 1
else 0
end
)
If you want to see how many were "lost" from this month to last month (how many were added form last month to this month) you would need to reverse the logic.
If you want to see how many more or less, that is much simpler:
count(distinct [portfolio_code_Prior]) -
count(distinct [portfolio_code Current ])
If you want 0 when they don't match otherwise the portfolio code, then your logic looks okay.
If you want to ONLY see the rows that do not match then add a detail filter for the custom data item, let's call it compare, like this:
[Compare] <> 0
as far as counting, you can just select the column and use count (or count distinct) depending on what you want.

Retrieve database records between two weekdays

I have several records in my database, the table has a column named "weekday" where I store a weekday like "mon" or "fri". Now from the frontend when a user does search the parameters posted to the server are startday and endDay.
Now I would like to retrieve all records between startDay and endDay. We can assume startDay is "mon" and endDay is "sun". I do not currently know how to do this.
Create another table with the names of the days and their corresponding number. Then you'd just need to join up your current table with the days table by name, and then use the numbers in that table to do your queries.
Not exactly practical, but it is possible to convert sun,mon,tue to numbers using MySQL.
Setup a static year and week number like 201610 for the 10th week of this year, then use a combination of DATE_FORMAT with STR_TO_DATE:
DATE_FORMAT(STR_TO_DATE('201610 mon', '%X%V %a'), '%w')
DATE_FORMAT(STR_TO_DATE('201610 sun', '%X%V %a'), '%w')
DATE_FORMAT(STR_TO_DATE('201610 tue', '%X%V %a'), '%w')
These 3 statements will evaluate to 0,1,2 respectively.
The main thing this is doing is converting the %a format (Sun-Sat) to the %w format (0-6)
well i don't know the architecture of your application as i think storing and querying a week day string is not appropriate, but i can tell you a work around this.
make a helper function which return you an array of weekdays in the range i-e
function getWeekDaysArray($startWeekDay, $endWeekDay) {
returns $daysArray['mon','tue','wed'];
}
$daysRangeArray = getWeekDaysArray('mon', 'wed');
now with this array you can query in table
DB::table('TableName')->whereIn('week_day', $daysRangeArray)->get();
Hope this help

How to show date in normal format in jqgrid when formatter is used

Date column is created using colmodel below.
This column shows values like 0101.0101.7070 for every date column.
if formatter:date is removed, date is correct.
How to show normal date values with formatter:date ?
{ "name":"Date",
"formatter":"date",
"datefmt":"dd.mm.yyyy",
"formatoptions":{"newformat":"dd.mm.yyyy"},
"editable":true
}
Update.
Data is passed from server using json in same dd.mm.yyyy format like
{"total":2,"page":1,"records":57,"rows":
[{"id":"9279","cell":["9279","42","","10.08.2011","","","","False"]},
{"id":"9278","cell":["9278","41","","12.08.2011","","","","False"]},
...
Using d.m.y formats in column options as suggested shows proper dates but with 2 year digits only..
I'm looking for a 4-digit year numbers. I tried d.m.yyyy format but this shows 8 digit year numbers and 1 for month and day as 01.01.70707070
I also tried to add srcformat: 'dd.mm.yyyy' to formatoptions but this does not change the wrong result.
To display the day as the number you should use j or d. For the displaying month as the number you should use n or m. The d and m includes 0 padding at the beginning if needed. The 'y' means two digit year format and Y means four digit year.
So you probably need srcformat: 'd.m.Y' or srcformat: 'j.n.Y'.
Use d.m.y instead of dd.mm.yy ,

Resources