I have a use case in druid where real-time data comes in the format like,
task{
taskno;
category;
}
Here category can be "assigned" or "unassigned".Suppose tasks with the following came,
taskno:1,category:"assigned"
taskno:2,category:"unassigned"
taskno:3,category:"assigned"
taskno:4,category:"assigned"
Here if I perform a query with filter as "category" with count(*) on it ,I will get result as;
assigned:3
unassigned:1
Now a new event comes with
taskno:2,category:"assigned"
I want in such a way that the query results
assigned:4
unassigned:0
Is there any way I can do like this using Javascript UDF or something in druid?
Thanks.
You can first filter your query on task and than filter it on category with order as desc and limit 1. It should give you the desired result.
And in your function where you getting the results you can addup the counts, or you can write a post aggregation function to do the same in druid.
Related
I want to get data get based on orderBy('fee', DESC) from pivot table in laravel.
The condition is
Doctor::with('doctor_hospital_settings')
->when(request('sort-by') == 'highest-fee', function ($query) {
$query->whereHas('doctor_hospital_settings', function (Builder $query1){
$query1->orderBy('doctor_hospital_settings.fee', 'DESC');
})
})
->orderBy('id', 'ASC')
this(whereHas) runs the sub-query which does not sort the data according to Fee DESC.
It only sort data based on doctor id mentioned at the end.
orderBy('id', 'ASC').
The orderby is done in the subquery but i need this sort in main query not in subquery
Doctor Model
public function doctor_hospital_settings()
{
return $this->belongsToMany(Hospital::class, 'doctor_hospitals')->withPivot('fee');
}
How I can achieve this
You can't sort by a relationship using the query builder directly. This is because Laravel performs two queries. One to fetch the doctor_hospital_settings and one to fetch the fees. The fees itself are sorted, but after that they are connected to the doctor_hospital_settings which doesn't result in the same order anymore.
You have two options to make this work. The first one is using a join the second one is sorting the data based on the results you get back using the collection methods. It depends on the amount of data you get back. If you only get a view records back, the collections approach is the simplest. If you have a lot of data, the join is a better approach
Joins: https://laravel.com/docs/8.x/queries#joins
Collections: https://laravel.com/docs/8.x/collections
I’m using NestJS with TypeORM, database is MySQL, and I’d like to filter multiple parameters that can be passed in.
The frontend has a list of products and filters are applied as query params sent to NestJS, filtering works for a single param eg api.example.com?manufacturer=Acer but how would I filter an Array eg api.example.com?manufacturer=Acer,Toshiba,Asus.
I tried quite a few things in TypeORM, currently using the QueryBuilder to build the array with an if statement if the filter exists if so I’m doing something like a where statement.
.andWhere(manufacturer = filterOne, {filterOne: *manufacturers from the query param*})
But yeah just can’t hack something together, tried a couple of things, above is a rough example, did try methods that TypeORM had as an example on filtering arrays but it seemed like it was more for an array of integers only? Regardless, I’m open to any methods that allow for the end result of filtering the example I provided, cheers and thanks again!
You have to use IN to get all data where manufacturer equal the data came from the query, first, you have to convert the query to an array:
var manufacturerParam = filterOne.split(",");
then add it to your query:
.andWhere(manufacturer IN (:filter)", { filter: manufacturerParam })
How to query Model say Post to get latest post first. Descending order.
My question is that is there any in built method exists something like latestFirst() in Laravel 5.4 to query model?
You can get all the latest post by ordering the posts by created_at column like this:
$latestPost = Post::orderBy('created_at', 'desc')->first();
You can also use latest(). For e.g:
$post = Post::latest()->first();
From the docs: The latest and oldest methods allow you to easily order results by date. By default, result will be ordered by the created_at column. Or, you may pass the column name that you wish to sort by
I try to sum mutliple count fields with JOOQ and a MySQL database.
At the moment my code looks like this:
int userId = 1;
Field<Object> newField = DSL.select(DSL.count()).from(
DSL.select(DSL.count())
.from(REQUIREMENT)
.where(REQUIREMENT.CREATOR_ID.equal(userId))
.unionAll(DSL.select(DSL.count())
.from(REQUIREMENT)
.where(REQUIREMENT.LEAD_DEVELOPER_ID.equal(userId)))
which always returns 2 as newField. But I want know how many times an user is the creator of a requirement PLUS the lead developer of a requirement.
You say "sum over multiple count", but that's not what you're doing. You do "count the number of counts". The solution is, of course, something like this:
// Assuming this to avoid referencing DSL all the time:
import static org.jooq.impl.DSL.*;
select(sum(field(name("c"), Integer.class)))
.from(
select(count().as("c"))
.from(REQUIREMENT)
.where(REQUIREMENT.CREATOR_ID.equal(userId))
.unionAll(
select(count().as("c"))
.from(REQUIREMENT)
.where(REQUIREMENT.LEAD_DEVELOPER_ID.equal(userId)))
);
Alternatively, if you plan to add many more of these counts to the sum, this might be a faster option:
select(sum(choose()
.when(REQUIREMENT.CREATOR_ID.eq(userId)
.and(REQUIREMENT.LEAD_DEVELOPER_ID.eq(userId)), inline(2))
.when(REQUIREMENT.CREATOR_ID.eq(userId), inline(1))
.when(REQUIREMENT.LEAD_DEVELOPER_ID.eq(userId), inline(1))
.otherwise(inline(0))
))
.from(REQUIREMENT);
More details about the second technique in this blog post
I have a design problem with SQL request:
I need to return data looking like:
listChannels:
-idChannel
name
listItems:
-data
-data
-idChannel
name
listItems:
-data
-data
The solution I have now is to send a first request:
*"SELECT * FROM Channel WHERE idUser = ..."*
and then in the loop fetching the result, I send for each raw another request to feel the nested list:
"SELECT data FROM Item WHERE idChannel = ..."
It's going to kill the app and obviously not the way to go.
I know how to use the join keyword, but it's not exactly what I want as it would return a row for each data of each listChannels with all the information of the channels.
How to solve this common problem in a clean and efficient way ?
The "SQL" way of doing this produces of table with columns idchannel, channelname, and the columns for item.
select c.idchannel, c.channelname, i.data
from channel c join
item i
on c.idchannel = i.idchannel
order by c.idchannel, i.item;
Remember that a SQL query returns a result set in the form of a table. That means that all the rows have the same columns. If you want a list of columns, then you can do an aggregation and put the items in a list:
select c.idchannel, c.channelname, group_concat(i.data) as items
from channel c join
item i
on c.idchannel = i.idchannel
group by c.idchannel, c.channelname;
The above uses MySQL syntax, but most databases support similar functionality.
SQL is made for accessing two-dimensional data tables. (There are more possibilities, but they are very complex and maybe not standardized)
So the best way to solve your problem is to use multiple requests. Please also consider using transactions, if possible.