Yii Create Command Sort - yii

I am looking to sort by ASC or DESC and am not quite sure how to do this using Yii, my code below is as follows but I am not sure where to order by as I need to order by DESC for timestamp and ASC for homeTeamName.
$query= Yii::app()->db->createCommand()
->select('*')
->from('livematch')
->where('DATE(timestamp) BETWEEN DATE(NOW()) AND DATE(NOW()) + INTERVAL 7 DAY')
->order(array('timestamp', 'homeTeamName'))
->queryAll();
Any help on this would be great

You can add it directly like below:
$query= Yii::app()->db->createCommand()
->select('*')
->from('livematch')
->where('DATE(timestamp) BETWEEN DATE(NOW()) AND DATE(NOW()) + INTERVAL 7 DAY')
->order(array('timestamp', 'homeTeamName desc'))
->queryAll();
which means: ORDER BY timestamp,homeTeamName DESC

try like this ,
$query = Yii::app()->db->createCommand("SELECT * FROM livematch where DATE(timestamp) BETWEEN DATE(NOW()) AND DATE(NOW()) + INTERVAL 7 DAY order by timestamp desc" );
$std_list = $query->queryAll();

Related

Get columns of data with two different date range

I would like to get the average rating for last 7 days and last 14 days.
I tried using WITH AS to get the data but it's taking way too long to load. Any other way that is better and could reduce the run time?
syntax:
WITH last_7_days AS (
SELECT item, rating
FROM sales
WHERE (
rating IS NOT NULL
AND (entry_date >= CAST((CAST(now() AS timestamp) + (INTERVAL '-7 day')) AS date) AND entry_date < CAST((CAST(now() AS timestamp) + (INTERVAL '1 day')) AS date))
)
),
last_14_days AS (
SELECT item, rating
FROM sales
WHERE (
rating IS NOT NULL
AND (entry_date >= CAST((CAST(now() AS timestamp) + (INTERVAL '-14 day')) AS date) AND entry_date < CAST((CAST(now() AS timestamp) + (INTERVAL '1 day')) AS date))
)
)
SELECT last_7_days.item, avg(last_7_days.score) as "avg_last_7_days", avg(last_14_days.rating) as "avg_last_14_days", count(*) AS "count"
FROM last_7_days, last_14_days
WHERE last_7_days.item = last_14_days.item
GROUP BY last_7_days.item
ORDER BY "avg_last_7_days" DESC, last_7_days.item ASC
Result should be something like this:
item|avg_last_7_days|avg_last_14_days|count|
thank you
Use conditional aggregation:
SELECT item,
AVG(rating) FILTER (WHERE entry_date >= NOW() + interval '-7 day' AND entry_date < NOW() + interval '1 day') AS avg_rating_last_seven_days,
AVG(rating) FILTER (WHERE entry_date >= NOW() + interval '-14 day' AND entry_date < NOW() + interval '1 day') AS avg_rating_last_fourteen_days
FROM sales
WHERE rating IS NOT NULL AND
(entry_date >= NOW() + interval '-14 day' AND entry_date < NOW() + interval '1 day')
GROUP BY item;
Note: If you only care about the date, then perhaps you should use CURRENT_DATE or even NOW()::date.
Getting rid of all the casts and aggregating directly on the CTEs should help, try with the following:
WITH last_7_days AS (
SELECT
item,
AVG(rating) AS avg_rating_last_seven_days
FROM
sales
WHERE
rating IS NOT NULL AND
(entry_date >= NOW() + interval '-7 day' AND entry_date < NOW() + interval '1 day')
GROUP BY
1
),
last_14_days AS (
SELECT
item,
AVG(rating) AS avg_rating_last_fourteen_days
FROM
sales
WHERE
rating IS NOT NULL AND
(entry_date >= NOW() + interval '-14 day' AND entry_date < NOW() + interval '1 day')
GROUP BY
1
)
SELECT
lsd.item,
avg_rating_last_seven_days,
avg_rating_last_fourteen_days
FROM
last_7_days AS lsd
INNER JOIN
last_14_days AS lfd ON lsd.item = lfd.item
Let me know in case it helped on improving your current performance!

Postgres Subtract Date Interval in Having

I am new to SQL but am having a (probably obvious) issue. My goal is to show all data in the month before the last dated entry. When executing the code block below to find the date a month ago,
SELECT MAX(created_at) - INTERVAL '1 MONTH' AS date
FROM shopify_view
it returns, '2019-11-27 11:40:06'. Makes sense!
But when I try to get all the date with a date above that value:
SELECT created_at AS date
FROM shopify_view
GROUP BY created_at
HAVING created_at >= MAX(created_at) - INTERVAL '1 MONTH'
ORDER BY created_at
it returns the first date as '2018-04-23 10:57:28'. Does not make sense!
what am I missing? Thank you!!
You should use a subquery
SELECT *
FROM shopify_view
Where created_at >= (SELECT MAX(created_at) - INTERVAL '1 MONTH' AS date
FROM shopify_view)
ORDER BY created_at

Select date and hour in query conditions

I am using PGADMIN 4 on postgres database. I would like to extract using the following query but adding hour as well. current date at 6am for example.
Thank you
SELECT object_key, audited_changes, created_at
FROM pg_audits
WHERE
source_action = 'funding' AND
created_at >= CURRENT_DATE -1 AND
created_at < CURRENT_DATE
ORDER BY
created_at DESC
You can write any number of hours in place of N
SELECT object_key, audited_changes, (created_at + INTERVAL 'N HOURS') as created_at
FROM pg_audits
WHERE
source_action = 'funding' AND
created_at >= CURRENT_DATE -1 AND
created_at < CURRENT_DATE
ORDER BY
created_at DESC

Postgresql get max row group by column

I am trying to get the max row from the sum of daily counts in a table. I have looked at several posts that look similar, however it doesn't seem to work. I have tried to follow
Get MAX row for GROUP in MySQL
but it doesn't work in Postgres. Here's what I have
select source, SUM(steps) as daily_steps, to_char("endTime"::date, 'MM/DD/YYYY') as step_date
from activities
where user_id = 1
and "endTime" <= CURRENT_TIMESTAMP + INTERVAL '1 day'
and "endTime" >= CURRENT_TIMESTAMP - INTERVAL '7 days'
group by source, to_char("endTime"::date, 'MM/DD/YYYY')
This returns the following
source, daily_steps, step_date
"walking";750;"11/17/2015"
"walking";821;"11/22/2015"
"walking";106;"11/20/2015"
"running";234;"11/21/2015"
"running";600;"11/24/2015"
I would like the result to return only the rows that have the max value for daily_steps by source. The result should look like
source, daily_steps, step_date
"walking";821;"11/22/2015"
"running";600;"11/24/2015"
Postgres offers the convenient distinct on syntax:
select distinct on (a.source) a.*
from (select source, SUM(steps) as daily_steps, to_char("endTime"::date, 'MM/DD/YYYY') as step_date
from activities a
where user_id = 1 and
"endTime" <= CURRENT_TIMESTAMP + INTERVAL '1 day' and
"endTime" >= CURRENT_TIMESTAMP - INTERVAL '7 days'
group by source, to_char("endTime"::date, 'MM/DD/YYYY')
) a
order by a.source, daily_steps desc;

Sql query to select from 1 hour ago?

I have this query but I want to change the date to delete everything that is from more than 1 hour ago based on the server time (or if not possible by server time by post date). How do I do that?
DELETE FROM wp_posts
WHERE post_date < '2008-06-06 19:18:00'
AND post_status = 'publish'
Use:
DELETE FROM wp_posts
WHERE post_date < DATE_SUB(NOW(), INTERVAL '1' HOUR)
AND post_status = 'publish'
Reference:
DATE_ADD
DATE_SUB
Or even simpler:
SELECT NOW() - INTERVAL 1 HOUR;
So the query becomes:
DELETE FROM wp_posts
WHERE post_date < NOW() - INTERVAL 1 HOUR
AND post_status = 'publish'
Subdate function works too!
SELECT subdate(current_timestamp, interval 1 hour)
DELETE from table where date_add < NOW() - INTERVAL 1 HOUR;
to see the time run this sql :
select NOW() - INTERVAL 1 HOUR as s1 , NOW() as s2;