I have a table with columns, a, b, c, d, e.
Each column is given a random numerical value.
I can then order by that numerical value.
query_results = UserProfile.objects.all().order_by('a')
What i want is the ability to order by the sum of 2 or 3 or even 5 columns. But i cant figure out how to do it lightly.
My answer so far has been some what convoluted. I add an extra column x.
cursor.execute("UPDATE mysite_userprofile SET x = a+b;")
I then order by x. I have to update the column x each time i want different columns added together.
This is not the way to do it i know. i know order_by('a+b') doesn't work but is their another solution just as simple. I looked on django's site but couldn't find anything
The best way is to use extra
UserProfile.objects.extra(
select={'sum_of_fields': 'a + b + c + d + e'},
order_by=('sum_of_fields',)
)
I think you can order the result in the SQL query. Before you run the SQL query, when you select the columns, you can build the query string in the background.
Related
I am trying Amazon Quicksight but I don't understand if this is possible.
I should display a number that is calculated:
[(a-b) / c]
a - is chosen from a list of data in the column A
b - is the mean of the column B
c - is the mean of the column C
it's possible?
Thanks
Where a differs depending on the row in column A? I don't think this is possible as you are writing a formula using both aggregated fields (mean of b or c) and a non-aggregated field (a).
I tried the formula with both and got the following error (using the avg function):
Mismatched aggregation. Custom aggregations can’t contain both
aggregate "AVG" and non-aggregated fields “AVG("ColumnId-2")”, in
any combination.
#Occamatic is right about inability to use both aggregated fields and a non-aggregated field in your formula.
However, you can circumvent this by using 'a' in an aggregated function in your calculated field. Example:
( sumIf({a},{a}={a}) - b ) / c
Please amend to the specifics to your dashboard, possibly with use of parameters in ifelse statements, but a version of this should work.
For instance, I myself can't use:
ifelse({metric_type}='Averages',avg({metric_value}),sum({metric_value}))
Instead I use:
ifelse(avgIf({metric_value},{metric_type}='Averages') > 0,avg({metric_value}),sum({metric_value}))
I have the following query in M:
= Table.Combine({
Table.Distinct(Table.SelectColumns(Tab1,{"item"})),
Table.Distinct(Table.SelectColumns(Tab2,{"Column1"}))
})
Is it possible to get it working without prior changing column names?
I want to get something similar to SQL syntax:
select item from Tab1 union all
select Column1 from Tab2
If you need just one column from each table then you may use this code:
= Table.FromList(List.Distinct(Tab1[item])
& List.Distinct(Tab2[Column1]))
If you use M (like in your example or the append query option) the columns names must be the same otherwise it wont work.
But it works in DAX with the command
=UNION(Table1; Table2)
https://learn.microsoft.com/en-us/dax/union-function-dax
It's not possible in Power Query M. Table.Combine make an union with columns that match. If you want to keep all in the same step you can add the change names step instead of tap2 like you did with Table.SelectColumns.
This comparison of matching names is to union in a correct way.
Hope you can manage in the same step if that's what you want.
I am trying to create a column that applies numerical differentiation and integration on another column but do not know how to reference a previous row.
My example data has columns: x | y | y'
A formula for differentiation is :
y' = y(x+h)-y(x-h) / 2h
In excel, this is really easy.
C3 = (B4-B2)/2*(A3-A2)
Here is an example of this code being executed in Excel:
Image The equation for y: y = 0.1*x^3 + x^2
However, in PowerBI i have no idea how to actually call a previous or next row's value.
I merely wish to calculate the y' column that formula.
you can't access previous row because PowerBi has no concept of a row :).
There is only concept called row context, which you might leverage to achieve what you want with earlier function.
But you have to have some kind of unique "row identifier" (index column might do).
Please, provide sample data, so we can help you further.
which of these is more efficient query to run:
one where the INCLUDE / DON'T INCLUDE filter condition in WHERE clause and tested for each row
SELECT distinct fullvisitorid
FROM `google.com:analytics-bigquery.LondonCycleHelmet.ga_sessions_20130910` t, unnest(hits) as ht
WHERE (select max(if(cd.index = 1,cd.value,null))from unnest(ht.customDimensions) cd)
= 'high_worth'
one returning all rows and then outer SELECT clause doing all filtering test to INCLUDE / DON'T INCLUDE
SELECT distinct fullvisitorid
FROM
(
SELECT
fullvisitorid
, (select max(if(cd.index = 1,cd.value,null)) FROM unnest(ht.customDimensions) cd) hit_cd_1
FROM `google.com:analytics-bigquery.LondonCycleHelmet.ga_sessions_20130910` t
, unnest(hits) as ht
)
WHERE
hit_cd_1 = 'high_worth'
Both produce exactly same results!
the goal is: list of fullvisitorId, who ever sent hit Level Custom Dimension (index =1) with value = 'high_worth' users ()
Thanks for your inputs!
Cheers!
/Vibhor
I tried the two queries and compared their explanations, they are identical. I am assuming some sort of optimization magic occurs prior to the query being ran.
As of your original two queries: obviously - they are identical even though you slightly rearranged appearance. so from those two you should choose whatever easier for you to read/maintain. I would pick first query - but it is really matter of personal preferences
Meantime, try below (BigQuery Standard SQL) - it looks slightly optimized to me - but I didn't have chance to test on real data
SELECT DISTINCT fullvisitorid
FROM `google.com:analytics-bigquery.LondonCycleHelmet.ga_sessions_20130910` t,
UNNEST(hits) AS ht, UNNEST(ht.customDimensions) cd
WHERE cd.index = 1 AND cd.value = 'high_worth'
Obviously - it should produce same result as your two queries
Execution plan looks better to me and it (query) is faster is much easier to read / manage
I have some table data in which I'd like to sum all the values in a specific column of all rows where column A contains string A and/or column B contains string B. How can I achieve this?
This works for one criterium:
=SUM(FILTER(G:G,REGEXMATCH(F:F,"stringA")))
I tried this, but it didn't work:
=SUM(FILTER(G:G,OR(ISTEXT(REGEXMATCH(F:F,"stringA")),ISTEXT(REGEXMATCH(C:C,"stringB")))))
Please try:
=SUM(FILTER(G:G,REGEXMATCH(F:F,"stringA")+REGEXMATCH(C:C,"stringB")))
+ works for or logic. ISTEXT is not needed because REGEXMATCH gives true or false.
OR does not work because filter is an arrayformula, use + in array formulas.
=SUM(FILTER(G:G,REGEXMATCH(F:F&C:C,"stringA|stringB")))
OR is denoted by |
EDIT Added &C:C to denote different Columns