I wonder how to generate a top 20 events list with sqlalchemy based on ticket sales?
SQL expression:
SELECT event_id, SUM(count) AS sum FROM ticketDb WHERE datetime < '$dayAfter' and datetime > '$dayBefore' GROUP BY event_id ORDER BY sum DESC LIMIT 20");
Thanks in advance!
Just a little update, as I found a solution a while ago:
db.session.query(Ticket.event_id, count_).\
filter(Ticket.creation_date > day_before).\
filter(Ticket.creation_date <= day_after).\
group_by(Ticket.event_id).order_by(count_.desc()).\
limit(20).\
all()
Maybe it helps somebody in the future ;)
Related
I need to perform the following query in mysql.
SELECT
evaluationpart.id,
evaluationpart.creation,
evaluationpart.evaluationid,
evaluationpart.partid,
evaluation.horimeter,
personcompressorpart.hourcapacity,
evaluation.evaluationdate AS changedate,
evaluation.averageworkload,
#ed := DATEDIFF(curdate(), evaluation.evaluationdate) AS elapseddays,
#uh:= #ed * evaluation.averageworkload AS usedhours,
#htu:= personcompressorpart.hourcapacity - #uh AS hourstouse,
#nc:= curdate() + INTERVAL (#htu/evaluation.averageworkload) DAY AS nextchange
FROM evaluationpart
LEFT JOIN evaluation ON evaluation.id = evaluationpart.evaluationid
LEFT JOIN personcompressorpart ON personcompressorpart.id = evaluationpart.partid
ORDER BY #nc ASC
But the Order By is not working and I'm getting this result
Could anyone tell me why?
It seems that you are not using the column name in the ORDER BY clause.
If you want to order the query result by the column named 'nextchange', the ORDER BY clause should be ORDER BY nextchange ASC.
Here's the MySQL documentation on Sorting Rows: https://dev.mysql.com/doc/refman/8.0/en/sorting-rows.html
I hope this helps.
I have two fields, "cont_time_published" and "cont_date_published" that I want to use to filter results in a listing of records.
select *
from news
WHERE cont_date_published < CURDATE() AND cont_time_published < CURTIME()
I am trying to achieve listing that only shows records that the publish time and publish date is in the past. So that it would filter a record that has today's date but the time is still in the future.
Results are wrong when the date is today and the time is future.
cont_date_published is "DATE" only field and cont_time_published is "TIME" Field.
If you need to get the records published in the past (today in the past hours or before) try:
SELECT *
FROM news
WHERE cont_date_published < CURDATE()
OR (cont_date_published = CURDATE() AND cont_time_published < CURTIME());
sorry my mistake, forgot the OR
Try this
SELECT *
FROM news
WHERE (cont_date_published < CURDATE() ) AND ( cont_time_published < CURTIME() )
ORDER BY id DESC;
You can CAST to an actual date:
WHERE CAST(CONCAT(cont_date_published, ' ', cont_time_published) AS DATETIME)<NOW()
... but this will possibly prevent query optimiser from using indexes (if any). An alternative would be:
WHERE cont_date_published<CURDATE() OR
(cont_date_published=CURDATE() AND cont_time_published<CURTIME())
Of course, all this extra work could be easily avoided with a proper database design that makes use of a single DATETIME column:
WHERE cont_published<NOW()
select * from news WHERE cont_date_published < CURDATE()
This will be enough to get the records that are Published in the Past.
Hey guys I have a quick question regarding sql performance. I have a really really large table and it takes forever to run the query below, note that there is a column with timestamp
select name,emails,
count(*) as cnt
from table
where date(timestamp) between '2016-01-20' and '2016-02-3'
and name is not null
group by 1,2;
So my friend suggested to use this query below:
select name,emails,
count(*) as cnt
from table
where timestamp between date_sub(curdate(), interval 14 day)
and date_add(curdate(), interval 1 day)
and name is not null
group by 1,2;
And this takes much less time to run. Why? What's the difference between those two time function?
And is there another way to run this even faster? Like index?Can someone explain to me how mysql runs? Thanks a lot!
just add index on timestamp field and use query as per below-
select name,emails,
count(*) as cnt
from table
where `timestamp` between '2016-01-20 00:00:00' and '2016-02-03 23:59:59'
and name is not null
group by 1,2;
Why? What's the difference between those two time function
In first query you are getting dates from your own column but with date() function due to this reason mysql is not using index and doing table scan while 2nd suggested table you have removed date(timestamp) function so now mysql will check values from index instead of table scan so it is fast.
Same mysql will use index in my table also.
In order to calculate projected sales for a given day, I need to query the last six weeks of data for a given day. For example, if I want projected sales for Friday, I need to query data from the last six Fridays only.
I'm assuming there is a way to do this within a query, just not sure exactly how. Any help or insight would be greatly appreciated, as always.
Thanks in advance.
The easiest way is to use a limit.
SELECT date, sales FROM yourtable WHERE DAYOFWEEK(date)=6
ORDER BY date DESC LIMIT 6;
EDIT: To get this relative to today, just add CURDATE()
SELECT date, sales FROM yourtable WHERE DAYOFWEEK(date)=DAYOFWEEK(CURDATE())
ORDER BY date DESC LIMIT 6;
You can use a combination of different MySQL date and time functions to achieve this. Your query could look something like this:
SELECT fields FROM table WHERE DAYOFWEEK(table.date) = DAYOFWEEK(CURDATE()) ORDER BY table.date DESC LIMIT 6
Of course you can replace CURDATE() with the date that you are trying to predict.
Select * From table Where date_field > DATE_ADD(now(),INTERVAL -42 DAY)
That's about what you will need to do.
I just seen you wanted to query only a particular day of each week. Give me a moment and I'll update this.
Nevermind, I'm not going to edit this. Bobby has your answer for you. You just need to place variables in there through your script as needed. +1 Bobby.
I have an events table and need to pull the 4 closest dates to today's date and they can be in the past, present or future.
What would the SQL (using MySQL) be for this if it is possible?
Thanks
Brett
I don't know which DB you are using, but this works with mysql:
select *
from event
order by abs(datediff(event_date, now()))
limit 4
Try using the TIMEDIFF function like this:
select *
from events
order by abs(timediff(now(), yourdatecolumn))
limit 4;