Can someone please explain the ATTR Function in Tableau - tableau-api

I know that the ATTR function is used for aggregation, but can someone explain it in simple terms?

In the most simplest of terms, ATTR returns a value if it is unique, otherwise it returns "*". I think you'll find this link helpful with examples.
https://www.interworks.com/blog/tcostello/2014/05/15/attr-tableaus-attribute-function-explained

You cannot mix aggregate and non aggregate comparisons in tableau, you have to use ATTR, for e.g. if ATTR(segment) ='Corporate' then sum(sales)

ATTR is like using an already aggregated field for comparison with another aggregated field. Measures are taken as aggregated fields while dimension's aren't. If you have created a field which is already aggregated and still you want to use this field as measure it will be shown as ATTR as it cannot be further aggregated but is behaving like it has.

Related

how to get the max value of a field in MongoDB

like:
{id:4563214321,updateTime:long("124354354")}
there are always new collections enter the db, so I would like to always get latest updated documents aka the largest update time. how to design the shell script? thanks in advance.
You can use a combination of limit and sort to achieve this goal.
db.collectionName.find({}).sort({"updateTime" : -1}).limit(1)
This will sort all of your fields based on update time and then only return the one largest value.
I would recommend adding an index to this field to improve performance.
This is a repeated question, you can find an answer in this link
Using findOne in mongodb to get element with max id
use like this,
db.collection.find().sort({updateTime:-1}).limit(1).pretty()
as findOne you can do it with this syntax:
db.collection.findOne({$query:{},$orderby:{_updateTime:-1}})

Couchbase View : Sorting on Keys

I am using Couchbase 3.0 and I have a use case where I want to perform sorting, but as i cam through couchbase does not provide Sorting on Values but it provides Sorting on Keys, But as I use descending(true) it is returning me empty list. And on the other hand If I just simply use it without descending then it is giving me all the docs related.
My Map function is :
function (doc, meta) {
emit([meta.id,doc.latest.sortData],null);
}
}
Now my use case is that I want to perform a match query on meta.id and then for all the matched cases sort the data and then find out the top values.
The code that i am using to so is :
ViewQuery.from(DesignDocName, viewName).startKey(JsonArray.from(write(List("something","")))).descending(true).limit(5).stale(Stale.FALSE))
If I remove the descending parameter then I get the related rows but they are not sorted. So could you please provide me a way in which this can be done.
Any Help is appreciated
Thanks!
I was reading Couchbase's docs and stumbled upon this line:
Because selection is made after sorting the view results, if you
configure the results to be sorted in descending order and you are
selecting information using a key range, then you must also reverse
the startkey and endkey parameters.
see here: http://docs.couchbase.com/admin/admin/Views/views-querying.html#ordering

Can MongoDB do alphanumeric sort?

I need to do a alphanumeric sort on one of the fields in my collection. This field contains Chromosome info.
Chr1,
Chr2,
..,
..,
..,
Chr10,
Chr11,
..,
..,
ChrX,
ChrY
Instead of getting the values in this order, I get them in
Chr1,
Chr11
This is a common problem and was wondering whether MongoDB has an in-built solution for this sort or should we hack it as we normally do in Oracle.
If there is no in-built solution, what is the best way to get this sort? Natural sort is an acceptable solution, but I have already applied it to another field.
Any help would would be greatly appreciated.
MongoDB has no build-in way of sorting data alphanumerical. But when all of your data has a field with a string with the same convention of "Chr" + number, you could put the number into a different field, keep it as an integer instead of a string, and sort by that.
Please refer to "https://docs.mongodb.com/manual/reference/collation".
Mongo provides collation for alphanumeric sorting.
Example:-
db.names.find({}).sort({username:1}).collation({locale:'en_US', numericOrdering:true})
You can also use if you are using aggregation in mongodb:-
db.names.aggregate([...query, {$sort:{username:1}]).collation({locale:'en_US', numericOrdering:true});
Don't forget to add sorting of key in query array otherwise you will not get sorting order.
This thing is not available in documentation.

Mongoid Query Syntax Question

I need to retrieve a set of answers according to 2 attributes.
This is what i want to do:
# where liker_ids is an array and user_id is a bson in the answer document
feed_answers=Answer.any_in(:liker_ids=>to_use,:user_id.in=>to_use).desc()map{|a|a}
What i ended up doing:
# to use
to_use=[some array of ids]
friend_answers=Answer.any_in(:liker_ids=>to_use).map{|a|a}
liked_answers=Answer.where(:user_id.in=>to_use).map{|a|a}
feed_answers=(friend_answers+ liked_answers).sort{|x,y| y.created_at<=>x.created_at}
The problem is that I do not not know how to combine the 2 queries into one. I have been trying out various combinations, but nothing seems to work. and my hacked together method is highly inefficient of course.
You should do(Missing parameter to desc):
Answer.any_in(:liker_ids=>to_use, :user_id.in=>to_use).desc(:created_at)
But the any_in here is not correctly used, it behaves similar to where in this situation. You probably want or:
Answer.or(:liker_ids=>to_use).or(:user_id.in=>to_use).desc(:created_at)
# or
Answer.any_of({:liker_ids=>to_use}, {:user_id.in=>to_use}).desc(:created_at)
# or
Answer.or({:liker_ids=>to_use}, {:user_id.in=>to_use}).desc(:created_at)
You don't need that map at the end of criteria chain, mongoid criteria are lazy loaded, when they encounter a method which criteria do not respond to. They can also leverage mongodb cursors, so it is advised not to use map if it is not necessary. You should use Criteia#only or Criteria#without if you want to retrieve subset of fields from mongodb.

Is there a way to fetch max and min values in Sphinx?

Im using sphinx for document search. Each document has list of integer parameters, like "length", "publication date (unix)", popularity, ... .
The search process itself works fine. But is there a way to get a maximum and minimum fields values for a specified search query?
The main purpose is to generate a search form which will contain filter fields so user can select document`s length.
Or maybe there is another way to solve this problem?
It is possible if length, date etc are defined as attributes.
http://www.sphinxsearch.com/docs/current.html#attributes
Attributes are additional values
associated with each document that can
be used to perform additional
filtering and sorting during search.
Try GroupBy function by 'length' and select mix(length), max(lenght).
In SphinxQl it is like:
select mix(length), max(lenght) from index_123 group by length
The same for other attributes.