Room update based on selection - sql-update

I am using Room in my application, and I wish to update the results based on a selection with order by.
The goal is to update the five top scoring results.
Inside my Dao class I have this query:
// Setting new five words
#Query("with data as( SELECT score FROM words_table ORDER BY score DESC LIMIT 5 )" +
" UPDATE data SET isInTodayWords= 1")
LiveData<List<Word>> setNewWords();
I get an error from the update statement - "can't resolve symbol data".
What am I missing in here?

I believe the issue is that you need to UPDATE the actual table not the CTE. Try :-
#Query("with data as( SELECT score FROM words_table ORDER BY score DESC LIMIT 5 )" +
" UPDATE words_table SET isInTodayWords= 1")
LiveData<List<Word>> setNewWords();
As per (qualified-table-name):-
Saying that I believe that you need to the utilise the CTA (data) in the WHERE clause otherwise all rows will be updated.
As such you likely want to add WHERE score IN data (see note below as this could apply, if a row has a unique column then you may prefer to extract that rather than the score and use it in the WHERE clause):-
#Query("WITH data AS (SELECT score FROM words_table ORDER BY score DESC LIMIT 5) UPDATE words_table SET isInTodaysWords = 1 WHERE score IN data")
An alternative approach could be to use :-
#Query("UPDATE words_table SET isInTodaysWords = 1 WHERE score >= (SELECT min(score) FROM (SELECT * FROM words_table ORDER BY score DESC LIMIT 5))")
However, if there were say 10 rows all with the same topscore then all 10 rows would be flagged.
If there was a unique column named _id then the following would restrict the flagging to the 5 selected rows :-
#Query("WITH data AS (SELECT _id FROM words_table ORDER BY score DESC LIMIT 5) UPDATE words_table SET isInTodaysWords = 1 WHERE _id IN data")

Related

Get the unique customers with the highest Item value in Table Visualization in Power BI

I am struglling to getting top 25 unique customers when filtering the table using Alert_Id. Basically, I have these columns in table which you can find below. The goal is to show top 25 unique customers based on highest value. The Item can be repeated but name has to be unique. I have tried so many different things but nothing seems to be working as expected because of Multiple customers have used multiple items and hence I am getting duplicate rows. The table has to be dymic because whenever user filters the table using Alert_id it should return those top unique customers that associated with Alert_id(Alert_id is a single selection). So whenever user select their Alert_id that table should display their data. I have tried below measure,
First I created calculated column to break the tie for price because many Item shares the same price:
max price = Table[PRICE] + RAND()
Then I created another column to get max price for the customer name:
MAX column for table = CALCULATE(MAX('Table'[max price]), ALLEXCEPT(Table, Table[CUSTOMER_NAME]))
Then I created calculated table using these columns:
SELECTCOLUMNS(
FILTER(Table, Table[max price]=Table[MAX column for table]), "Name" ,Table[CUSTOMER_NAME],"Item",Table[ITEM], "PRICE",Table[MAX column for table], "Alert_ID", Table[ID], "DATE", Table[REQ_DATE], "ITEM_COUNT", Table[PK])
But, this is giving me all unique customers with the MAX value and I am getting blank table when I filter with Alert_ID even thought it has data but the customers are not with the MAX value. Basically, It's not dynamically capturing max values for each customer_name when filter is applied. And, I if there are multiple rows with same customer name which can have same exact value then I would choose any random row without considering which ITEM it is. I just want top 25 unique customers for one Alert_ID.
Here is the sample data,
Here is expected output if I select Alert_ID = 123 from filter and it can be different when I select different Alert_ID.
FYI: I have tried topn with max price and even with RANKX but no luck. I always endedup having multiple customers.
Any help or lead will be highly appreciated!
I was able to figure out how to get unique values. Here is the solution that worked for me.
First, I created calculated column with my price column and RAND function to break the ties:
sum value = Table[PRICE] + RAND()
Then, I have created one measure that calculates the rank:
rank with table = RANKX(CALCULATETABLE(VALUES('Table'[ITEM]), ALLSELECTED('Table'[ITEM])),CALCULATE(SUM(Table[sum value])), ,DESC, Dense )
Then I applied the filter on NAME column to get top 25 based on sum value calculated column. Also, dragged my measure on filters pane and applied the filter where Rank with table = 1.
That's how I got unique names with highest valued ITEM.

Why bigquery can't handle a query processing 4TB data?

I'm trying to run this query
SELECT
id AS id,
ARRAY_AGG(DISTINCT users_ids) AS users_ids,
MAX(date) AS date
FROM
users,
UNNEST(users_ids) AS users_ids
WHERE
users_ids != " 1111"
AND users_ids != " 2222"
GROUP BY
id;
Where users table is splitted table with id column and user_ids (comma separated) column and date column
on a +4TB and it give me resources
Resources exceeded during query execution: Your project or organization exceeded the maximum disk and memory limit available for shuffle operations.
.. any idea why?
id userids date
1 2,3,4 1-10-20
2 4,5,6 1-10-20
1 7,8,4 2-10-20
so the final result I'm trying to reach
id userids date
1 2,3,4,7,8 2-10-20
2 4,5,6 1-10-20
Execution details:
It's constantly repartitioning - I would guess that you're trying to cramp too much stuff into the aggregation part. Just remove the aggregation part - I don't even think you have to cross join here.
Use a subquery instead of this cross join + aggregation combo.
Edit: just realized that you want to aggregate the arrays but with distinct values
WITH t AS (
SELECT
id AS id,
ARRAY_CONCAT_AGG(ARRAY(SELECT DISTINCT uids FROM UNNEST(user_ids) as uids WHERE
uids != " 1111" AND uids != " 2222")) AS users_ids,
MAX(date) OVER (partition by id) AS date
FROM
users
GROUP BY id
)
SELECT
id,
ARRAY(SELECT DISTINCT * FROM UNNEST(user_ids)) as user_ids
,date
FROM t
Just the draft I assume id is unique but it should be something along those lines? Grouping by arrays is not possible ...
array_concat_agg() has no distinct so it comes in a second step.

How to only show TOP N in table

I have a simple table with a name field and a count(distinct) field and I am trying to figure out how to show only the TOP N records based on the count(distinct) column, the sorting and the count works great I just cannot find the option to limit it to top N records
The solutions I know are:
Use RANK calculation, then inserting a filter
Create a new table in dax using the TOPN function
https://dax.guide/topn/
NewTable = TOPN(100, YourTable
, YourTable[ColumnToOrder]
, DESC)

Create a monthly graph containing dynamical values

I discovered PowerBI few hours ago and I am trying to render a new graph, pretty complex from my data.
My subject:
I have a list of employees from my society with some information (name, location, seniority, ....) inside a table named Suivis de mission.
I'm focusing on seniority column. I have to display, monthly, the number of employees which has less than 3 seniority years, more than 3, more than 5 and more than 8.
This field is calculed through another field (date_de_changement_de_mission) by the next formula:
(today - date_de_changement_de_mission) / 365
It gives me a number according to the seniority years (above in french)
My experimental work:
I tried to make some things and this is what I have up to now.
I created a new table with months (string column1 and int column2) and the third column according to the number of employee with less than 3 seniority years for each month.
This is the formula that I'm trying to implement in order to get the number but I have lot of mistakes :
Nbr_inf_3_ans = SUMX('Suivis de mission';
IF(
'Suivis de mission'[Activité] = "En poste";1 &&
DIVIDE(
DATEDIFF(01/01/2019;'Suivis de mission'[Date de changement de mission].[Date];DAY);365)
;"null"))
The formula must contains several conditions :
Field 'Suivis de mission'[Activité] has to be : "En poste"
Loop over each month in my table (if the month is not targed, display 0)
I'm a bit lost.
Assuming that:
You want to do it in Power Query Editor
You already created the "seniority" and "month" ("mois*") columns
You are not worried about the year (you are really grouping just by month)
Then these steps may help you to begin:
Create a custom column name "IsLesserThan3" with the formula:
if [Seniority] < 3 and [Activité] = "En poste" than 1 else 0
Change the formula to include the requirement about "if the month is not targed" (i didn't get what it means, sorry).
Choose "Transform" (menu), then "Group By", then basic, set group by = month, give it a name ("Nbr_inf_3_ans"), set operation = sum, set column = IsLesserThan3
EDITED
New extra assumption:
There is a second table with month description and month number and you want to create the new column there ("Nbr_inf_3_ans") and fill it, probably to grant all the months are there (including those with no occurrences).
With this new assumption in mind, the approach will be to join the two tables and do the same thing:
Create a custom column named "IsLesserThan3" with the formula:
if [Seniority] < 3 and [Activité] = "En poste" than 1 else 0
Change the formula to include the requirement about "if the month is not targed" (i didn't get what it means, sorry).
Change the type of the new column to integer (click the icon to the left of the column name).
Create a custom column named "Mois_int" with the formula:
Date.Month([date_de_changement_de_mission])
Then join the tables by month. To do that, select the other table and choose "Transform" (menu), then "Merge", then let the second table selected and select the first table as the "bottom" table, select columns "Mois_int" in both tables, set "join kind" = "left outer".
PowerBI will create a new column wich name is the name of the first table. Click the arrows icon in that column header and choose "aggregate" and mark only "sum of LesserThan3".

Create calculated measure from other table with filters

I have four tables which I have tried related and unrelated:
Store (column "Store Number")
Calendar (column "Sales Date")
SKU (column "SKU Code")
Sales (columns "Store Number", "Sales Date",
"SKU Code" and "Sales Quantity")
I have slicers on the Calendar and SKU tables
I need to list all stores with total "Sales Quantity" for each store and at the same time to limit the sales quantity to the two slicers mentioned above. Basically, I need to list these columns:
Store Number - from the Store table (no filtering from Slicers)
Store Name - from the Store table (no filtering from Slicers)
Total Quantity of Sales for the Store - calculated measure filtered by Calendar and SKU slicers
So my question is, what DAX required to create the calculated measure?
Please note I must list ALL stores regardless of whether they have sales in the stipulated period.
I've tried various DAX functions such as TREATAS, SUMMARIZE, ETC.
I've tried with and without active relationships and with no relationships.
The closest I've got is the code below, but it excludes stores with zero sales. I need all stores regardless of their sales.
Qty by Store = CALCULATE(
sum(Sales[Sales Qty])
,USERELATIONSHIP(
Sales[Store Number]
,Store[Store Number]
)
)
The problem with the output I've managed is that stores without sales are excluded from the list. I need to have them included.
Keep the relationship active, and change the DAX formula to
Qty by Store =
VAR res = sum(Sales[Sales Quantity])
RETURN IF (ISBLANK(res), 0, res)
There is no need for USERELATIONSHIP(). Relationship Store - Sales is already active. The reason why the number of stores changes in the table visual is because when there is no sale for a particular store Qty by store measure returns BLANK and those BLANKs get filtered out by the table.
Result:
An easy way to make a blank return zero instead is to simply append +0 to your measure formula.
Qty by Store = SUM ( Sales[Sates Quantity] ) + 0
This works because DAX calculates BLANK() + 0 = 0.