How to write this subquery in Laravel - php

I have to group by the taken_date year to get the years and in the query I need to get the total work days and count of the order. Currently I am running this in MySQL procedure for executing this, but I sense there will be a better way to handle this, kindly let me know
select DATE_FORMAT(taken_date, '%Y') year, count(id) total, sum(total_days) total_days from
(
select id, taken_date, getNumOfWorkDays(order.order_num) total_days from order
where order.is_active = 1 and order.deleted_at is null
order by taken_date
) as order_years group by YEAR(taken_date) order by taken_date desc
Order Table columns looks like below
getNumOfWorkDays function will give you only work days based on start and end date of the order uses some other table to know the work days

Related

Sum monthly sales totals and provide output with month name and total for last 3 months

I am trying to get monthly sales totals from a MYSQL database and have them summed by month with month name for last X months. The two columns I am targeting are: total_customer_charged and local_time. I almost have my query working correctly, the problem I have is that the order the results come out sometimes changes with each query. Can anyone please give me some hints to get my query correct?
My existing query is:
select date_format(local_time,'%M'),sum(total_customer_charged) FROM ORDERS WHERE local_time BETWEEN curdate() - INTERVAL 3 MONTH AND curdate() group by month(local_time) order by year(local_time),month(local_time)
You should put year into the group-by statement like this:
select
date_format(local_time, '%Y') AS agg_year,
date_format(local_time, '%m') AS agg_month,
sum(total_customer_charged) AS monthly_total_customer_charged
FROM
ORDERS
WHERE
local_time BETWEEN curdate() - INTERVAL 3 MONTH AND curdate()
group by
date_format(local_time, '%Y'),
date_format(local_time, '%m')
order by
agg_year,
agg_month
SELECT MONTHNAME(local_time), SUM(total_customer_charged)
FROM ORDERS
GROUP BY YEAR(local_time), MONTH(local_time)

Order by Date in MySQL & PHP and giving importance to today date

I want to make an order by date to get the comments of user, but I want to give the importance to today date
if there's a comment today show it first and make the ordering with date for the rest.
I try to make this but always give me an error in syntax
SELECT *
FROM comment
ORDER BY IF(`DATE_TIME_COMMENT` = CURRENT_DATE())
is there any solution ?
First sort by a CASE returning "something lower", if date_time_comment is equal to current_date() and "something higher" else. Then, second, sort by date_time_comment.
SELECT *
FROM comment
ORDER BY CASE date_time_comment
WHEN current_date()
THEN 0
ELSE
1
END,
date_time_comment;
(Possibly add DESC after date_time_comment in the ORDER BY clause, if you want to have newest comments first.)
If you can't have comments with future date, just order by date
SELECT * FROM comment ORDER BY DATE_TIME_COMMENT DESC
If you can have future date, and want to order first the today date, then comments with other date than today, one way to do this is with a UNION
SELECT * FROM (
SELECT 1 as order, c.* FROM comment c WHERE DATE_TIME_COMMENT = CURRENT_DATE()
UNION ALL
SELECT 2 as order, c.* FROM comment c WHERE DATE_TIME_COMMENT <> CURRENT_DATE()
) order by order asc, DATE_TIME_COMMENT desc
That you get first the today comments, then other comments order by date

how to fetch exact data from mysql table in month date condition

hello everyone for my erp system i want some specific type of data fetch from database
for example if i want number of row added in current month purchase then i use
select count(receive_no) FROM tbl where contact_id=".$contactid." AND MONTH(created_on) = MONTH(CURDATE());
but it gets false results
and if i want sum of result in current month then i use
select sum(price) FROM tbl where contact_id=".$contactid." AND MONTH(created_on) = MONTH(CURDATE())
and if i want today date number of row or purchase then i use
select count(receive_no) FROM tbl where contact_id=".$contactid." AND created_on >= CURDATE()
so as if i want today sum of purchase then i use
select sum(price) FROM tbl where contact_id=".$contactid." AND created_on >= CURDATE()
my table created on field datetime like 2016-09-01 11:56:45
so please suggest me better query to fetch exact data
thanks
It's not really clear what - if any - the problem is with the last 3 queries, but the first one will get you the data from this month this year and this month last year, the year before that, etc. etc.
So what you need is something like (there are several ways to write that...):
select count(receive_no) FROM tbl where
contact_id=".$contactid."
AND MONTH(created_on) = MONTH(CURDATE())
AND YEAR(created_on) = YEAR(CURDATE())

SQL: Select last 15 records from mysql grouped rows

Here is my query:
SELECT temp_table.*
FROM
( SELECT COUNT(*) as hits_count
, date
FROM visits
GROUP
BY date
) as temp_table
ORDER
BY temp_table.date ASC
LIMIT 15
I insert a new record into this table each time an user access a page. I need to get those records stacked by their date. It worked untill it hit the limit of 15 days, so now it doesn't show other days, it stops on his limit(15).
To make it clearer, let say I have stored 20 days, it shows just the 1-15 day interval, but i need it to get from db the interval 5-20, and so on.
I think this is what you are looking for:
SELECT temp_table.* FROM (
SELECT COUNT(*) as hits_count, date FROM visits GROUP BY date
) as temp_table ORDER BY temp_table.date DESC LIMIT 15
Not sure about the limit part though.

order by dates that match criteria

I have the following query that outputs the users that received the highest number of favorites in the past week in descending order:
SELECT COUNT(faves.user_id) AS topFaves, faves.created_on, user_name
FROM users
INNER JOIN faves ON faves.user_id= users.id
WHERE DATE_SUB(CURDATE(),INTERVAL 7 DAY) <= created_on
GROUP BY id ORDER BY topFaves DESC
I would like to be able to extend this list to contain all users, not just from the past week, but still order them by the same criteria (the number of favorites they got in the past week and not by the number of faves they have in total).
I tried to include a subquery in the select but didnt have any luck with it.
Thanks in advance for any help
Maybe I'm missing something, but just delete this line:
WHERE DATE_SUB(CURDATE(),INTERVAL 7 DAY) <= created_on
And you'll have "all users" - still sorting by topFaves...
Also if you want to list everyone (not just 10 people), you'll need to delete:
LIMIT 10

Categories