Select all posts where its title contains 14 digits - ruby

I'd like to find all posts where its title meets the critera below in one database call.
title.scan(/\d/).length == 14

Post.where('title ~ ?', '^\D*(\d\D*){14}$')

Related

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.

How can I optimize the following query in laravel eloquent model?

I am trying to fetch some result from a table in a particular order.
I have a table, with few thousands questions. Each question have a category_id.
My task is, collect 100 questions from this table, in random order. Where first 30 questions will be from category_id =1, next 30 questions from category_id =2, and last 40 question from category_id=3.
My current solution is:
Question::inRandomOrder()->where('category_id',1)->limit(30)->get()
Question::inRandomOrder()->where('category_id',2)->limit(30)->get()
Question::inRandomOrder()->where('category_id',3)->limit(40)->get()
My question is, can I get same result with only one query?
With unions you could do the following.
$questions = Question::inRandomOrder()->where('category_id',1)->limit(30)
->union(Question::inRandomOrder()->where('category_id',2)->limit(30))
->union(Question::inRandomOrder()->where('category_id',3)->limit(40))
->get();
Technically a single query, but I'd rather have 3 queries.
you can use union
$q1 = Question::inRandomOrder()->where('category_id', 1)->limit(30);
$q2 = Question::inRandomOrder()->where('category_id', 2)->limit(30);
$value = Question::inRandomOrder()->where('category_id', 3)->limit(40)->union($q1)->union($q2)->get();

filter datatables with same prefix but different suffix in numbers

I am using dataTables for a class list of students. One of my tables looks like this
Student Name || Class Name
Victoria Say || Grade 1
Phillip Hey || Grade 1
Stephen Chew || Grade 3
Marian Boot || Grade 2
Mary Brave || Grade 3
Betty Nancy || Form 1
Bright Hanson || Form 3
If I type 'Grade 3' in the search box, instead of returning, [Stephen Chew and Mary Brave], the table
returns all the five members whose Class Name contains the word [Grade]. How would I able to return
only the [Grade 3] students using the search box. Thank you in advance.
First you need to disable the default search handler and attach your own. Then inside your search function you need to use search() and set regex to true and smart search to false so that it only returns exact matches.
$('.dataTables_filter input', dt.table().container())
.off('.DT')
.on('keyup.DT cut.DT paste.DT input.DT search.DT', function(e) {
table.search(`^${this.value}$`, true, false).draw();
}
Where table is a JQuery reference to your DataTable. E.g.
const table = $('#tableId').DataTable();

how to get result count group by day in laravel

Hello Everyone can you please help me to resolve this
I want to get the user count day-wise, for example, I want to know how many users registers in last week
like date of
22-07-19
user count 20
23-07-19
user count 30
24-07-19
user count 10
25-07-19
user count 15
I want this result for last 7 day from today
basically, I want to show this in my chart please check the image here
By selecting DATE(created_at) and grouping by that, we can get the count of users that have registered each day. We can then add a simple where clause, using Carbon to help us get the lower bounds.
Example (where x = date and y = count):
User::selectRaw('DATE(created_at) as x, COUNT(*) as y')
->groupBy('x')
->where('created_at', '>', Carbon::now()->subWeek())
->get();

Need some help ordering a Linq result

I have some Linq code, that works fine. It retrieves a list of board posts ordered by the most recent.
The catch here is the order by ... it orders by the most recent COMMENTS. So if a board post just got a comment, then it will be up the top of the list (ie. most recent).
kewl ... but what about new board posts that just got created? They are listed down the bottom, because they have NO comments :( It's like i want to say "order by most recent comments .. but if u have no comment, then its by your board post create date)".
here's my linq...
boardPostList = (from bp in db.tblBoardPosts.Where(whereClause)
orderby (from bc in bp.tblBoardComments
orderby bc.DateModified descending
select bc.DateModified).First() descending
select bp).ToPagedList(pageNumber, numberOfResults);
Does anyone have any suggestions?
Is there any reason you can't update a field in tblBoardPosts whenever a comment is posted to it? Keep the "posted or last comment" date, then you've got a much simpler query, and one which doesn't need to scan every comment in the system to work out what to do.
Having said that, this query might work, if your DateModified field is nullable:
boardPostList = (from bp in db.tblBoardPosts.Where(whereClause)
orderby ((from bc in bp.tblBoardComments
orderby bc.DateModified descending
select bc.DateModified).FirstOrDefault() ?? bp.DateModified) descending
select bp).ToPagedList(pageNumber, numberOfResults);
If it's just a straight DateTime column, the result of FirstOrDefault is still a DateTime which would be non-nullable... You could try:
boardPostList = (from bp in db.tblBoardPosts.Where(whereClause)
let lastComment = bp.tblBoardComments
.OrderByDescending(bc => bc.DateModified)
.FirstOrDefault()
let lastModified = (lastComment == null
? bp.DateModified
: lastComment.DateModified)
orderby lastModified descending
select bp).ToPagedList(pageNumber, numberOfResults);
It's pretty hideous though, and may not translate into SQL properly. I would certainly try to change to a scheme where the post itself keeps track of it.

Resources