I have two tables
1) outreach
id profile_id url
-------------------------
1 2 www.test.com
2 3 www.google.com
3 4 www.example.com
4 2 www.test2.com
5 2 www.test3.com
6 2 www.test4.com
2). outreach_links
id outreach_id start_date created_at cost status
-----------------------------------------------------------------------
1 1 2016-12-01 00:00:00 2016-12-07 00:00:00 100.00 Approved
2 1 2016-12-02 00:00:00 2016-12-09 00:00:00 120.00 Approved
3 1 NUll 2016-12-28 00:00:00 20.00 Pending
4 1 2016-12-05 00:00:00 2016-12-10 00:00:00 35.00 Approved
5 1 2016-12-07 00:00:00 2016-12-13 00:00:00 10.00 Approved
6 2 2016-12-10 00:00:00 2016-12-15 00:00:00 10.00 Pending
7 2 2016-12-13 00:00:00 2016-12-18 00:00:00 10.00 Approved
8 2 2016-12-01 00:00:00 2016-12-28 00:00:00 10.00 Pending
9 2 2016-12-04 00:00:00 2016-12-21 00:00:00 10.00 Approved
10 2 2016-12-09 00:00:00 2016-12-22 00:00:00 15.00 Pending
I am trying to do a count by Month/Year and I thought its working but I think its not working because of "profile_id" issue here is my query:
select monthname(date) as Month, year(date) as Year, month(date) as Mn, UNIX_TIMESTAMP(CONCAT(year(date),"-",month(date),"-","01")) as tt,
(select count(*) from outreach_links where year(outreach_links.created_at) = year and month(outreach_links.created_at) = month and status = "Pending" and created_at>="2016-12-01 00:00:00" and created_at<="2016-12-31 00:00:00") as pp,
(select count(*) from outreach_links where year(outreach_links.start_date) = year and month(outreach_links.start_date) = month and status = "Approved" and start_date>="2016-12-01 00:00:00" and start_date<="2016-12-31 00:00:00") as aa,
(select sum(cost) from outreach_links where year(outreach_links.start_date) = year and month(outreach_links.start_date) = month and status = "Approved" and start_date>="2016-12-01 00:00:00" and start_date<="2016-12-31 00:00:00") as cc
from
(select year(outreach_links.created_at) as year, month(outreach_links.created_at) as month, outreach_links.created_at as date
from outreach_links
inner join outreach on outreach.id = outreach_links.outreach_id
where outreach_links.created_at>="2016-12-01 00:00:00" and outreach_links.created_at<="2016-12-31 00:00:00" and outreach.profile_id=2
union
select year(outreach_links.start_date) as year, month(outreach_links.start_date) as month, outreach_links.start_date as date
from outreach_links
inner join outreach on outreach.id = outreach_id
where start_date>="2016-12-01 00:00:00" and start_date<="2016-12-31 00:00:00" and outreach.profile_id=2 ) t1
group by year, month
order by date
So I am doing a date range from "2016-12-01 00:00:00" to "2016-12-31 00:00:00" these could be any date range inputed by the user , and try to do a count based on outreach.profile_id = 2 , my output is wrong its counting everything for all profile_ids, I am not sure why
Note: this is just a sample of the tables , there might be more records and the user inputed Date range from to could be different , I want to group them by Month / Year
here is my output: ( its counting ALL records)
array:1 [▼
0 => {#394 ▼
+"Month": "December"
+"Year": "2016"
+"Mn": "12"
+"tt": "1480568400.000000"
+"pp": "4"
+"aa": "6"
+"cc": "285.00"
}
]
Which is wrong it should count only for profile_id=2 , here is the DESIRED output I want:
array:1 [▼
0 => {#394 ▼
+"Month": "December"
+"Year": "2016"
+"Mn": "12"
+"tt": "1480568400.000000"
+"pp": "1"
+"aa": "4"
+"cc": "265.00"
}
]
As you can see the 3 counts are wrong they suppose to be:
"pp": "1"
"aa": "4"
"cc": "265.00"
here is what I am looking for:
1). **"pp" is Total Pending** Count when status="Pending" based on created_at
2). **"aa" is Total Approved** Count when status="Approved" based on start_date
3). **"cc" is Total Cost** Sum of All cost when Status="Approved" and based on start_date
4). Group by Month & Year of the user imputed Date Range
here is a SQLFIDDLE >> http://sqlfiddle.com/#!9/87dfa8/1
can you please help me fix it?
Thanks
I think you want something like this:
SELECT MONTHNAME(d.date) AS Month
, YEAR(d.date) AS Year
, MONTH(d.date) AS Mn
, SUM(IF(l.status = 'Pending' AND l.created_at >= d.date AND l.created_at < d.date + INTERVAL 1 MONTH ,1 ,0)) AS pp
, SUM(IF(l.status = 'Approved' AND l.start_date >= d.date AND l.start_date < d.date + INTERVAL 1 MONTH ,1 ,0)) AS aa
, SUM(IF(l.status = 'Approved' AND l.start_date >= d.date AND l.start_date < d.date + INTERVAL 1 MONTH ,l.cost,0)) AS cc
FROM ( SELECT '2016-12-01' + INTERVAL 0 MONTH AS date ) d
JOIN outreach o
ON o.profile_id = 2
LEFT
JOIN outreach_links l
ON l.outreach_id = o.id
AND ( ( l.start_date >= d.date + INTERVAL 0 MONTH
AND l.start_date < d.date + INTERVAL 1 MONTH
)
OR ( l.created_at >= d.date + INTERVAL 0 MONTH
AND l.created_at < d.date + INTERVAL 1 MONTH
)
)
GROUP BY d.date
EDIT
To specify the end date of the range along with the start date, the inline view d can return two date values. And the outer query can reference the second date value, in this example d.end_date, as well as the start of the range d.date.
SELECT MONTHNAME(d.date) AS Month
, YEAR(d.date) AS Year
, MONTH(d.date) AS Mn
, SUM(IF(l.status = 'Pending' AND l.created_at >= d.date AND l.created_at < d.end_date,1 ,0)) AS pp
, SUM(IF(l.status = 'Approved' AND l.start_date >= d.date AND l.start_date < d.end_date,1 ,0)) AS aa
, SUM(IF(l.status = 'Approved' AND l.start_date >= d.date AND l.start_date < d.end_date,l.cost,0)) AS cc
FROM ( SELECT '2016-12-01' + INTERVAL 0 MONTH AS date
, '2016-12-16' + INTERVAL 0 MONTH AS end_date
) d
JOIN outreach o
ON o.profile_id = 2
LEFT
JOIN outreach_links l
ON l.outreach_id = o.id
AND ( ( l.start_date >= d.date
AND l.start_date < d.end_date
)
OR ( l.created_at >= d.date
AND l.created_at < d.end_date
)
)
GROUP BY d.date, d.end_date
Related
i have a query like this :
select DATE_FORMAT(o.created_at, '%Y-%m') as date,
JSON_EXTRACT(oi.inventory, '$.id') as inv_id,
count(JSON_EXTRACT(oi.inventory, '$.id')) as inv_count
from orders as o inner join order_items as oi on oi.order_id = o.id
where o.created_at >= (CURDATE() + INTERVAL (1 - DAY(CURDATE())) DAY) - INTERVAL 12 MONTH AND
o.created_at < (CURDATE() + INTERVAL (1 - DAY(CURDATE())) DAY) + INTERVAL 1 MONTH
group by date, inv_id
order by date desc limit 10
and the result is :
date
inv_id
inv_count
2023-01
1
22
2023-01
2
29
2022-12
1
1
2022-12
2
2
2022-11
1
2
2022-11
2
1
2022-10
1
2
2022-10
2
1
but all i want is result like this :
date
inv_id
inv_count
2023-01
2
29
2022-12
2
2
2022-11
1
2
2022-10
1
2
how to grouping data like that? because i can't use max(inv_count) inside group by
I have a dataset like the below dataset. I want to find the number of nights each id was occupied per month. For some rows, the check-in and checkout dates are in different months. I want to know how to write a query to have the occupancy per month. For example, for id=1, check-in: 2020-01-26 and checkout date: 2020-03-02. How can I have a table that shows January occupancy: 6, Feb occupancy: 29, and March occupancy: 1
id
check-in
checkout
1
2020-01-26
2020-03-02
2
2020-04-01
2020-04-20
3
2020-06-29
2020-07-03
The outcome should be like this:
id
Month
Occupancy
1
Jan
06
1
Feb
29
1
Mar
01
2
Apr
19
3
Jun
02
3
Jul
02
first, you need a numbers table or tally table , after you can easily to it using this query :
select c.id,
case when m.id <> 0
then adddate(last_day(adddate(checkin_date, interval m.id -1 month)),interval 1 day)
else checkin_date
end as Checkin_date,
case when last_day(adddate(checkin_date, interval m.id month)) > checkout_date
then checkout_date
else last_day(adddate(checkin_date, interval m.id month))
end checout_date,
datediff(case when last_day(adddate(checkin_date, interval m.id month)) > checkout_date
then checkout_date
else last_day(adddate(checkin_date, interval m.id month)) end,
case when m.id <> 0
then last_day(adddate(checkin_date, interval m.id -1 month))
else adddate(checkin_date, interval -1 day) end
) daysdiff
from checkins c
join numbers m on m.id <= period_diff(date_format(checkout_date, "%Y%m"),date_format(checkin_date, "%Y%m"))
order by c.id, checkin_date
this is works for any gap (for more than 1 year)
you can usedate_format to show month :
select
date_format(case when m.id <> 0
then adddate(last_day(adddate(checkin_date, interval m.id -1 month)),interval 1 day)
else checkin_date
end, '%Y %M') as month_year
,sum(datediff(case when last_day(adddate(checkin_date, interval m.id month)) > checkout_date
then checkout_date
else last_day(adddate(checkin_date, interval m.id month)) end,
case when m.id <> 0
then last_day(adddate(checkin_date, interval m.id -1 month))
else adddate(checkin_date, interval -1 day) end
)) Occupancy
from checkins c
join numbers m on m.id <= period_diff(date_format(checkout_date, "%Y%m"),date_format(checkin_date, "%Y%m"))
group by date_format(case when m.id <> 0
then adddate(last_day(adddate(checkin_date, interval m.id -1 month)),interval 1 day)
else checkin_date
end, '%Y %M')
order by month_year
month_year | Occupancy
:------------ | --------:
2020 April | 20
2020 February | 29
2020 January | 6
2020 July | 3
2020 June | 2
2020 March | 2
db<>fiddle here
If I understand correctly, you want month-wise aggregated result of occupied inventory.
You can try below simple aggregate query as based on 'Group by' clause then add more criteria logic based on your need if required
select monthname(check_in) as 'Month', sum(dayofyear(check_out) - dayofyear(check_in)) as 'Occupied_days'
from inventory
where year(check_in)=year(check_out)
group by 1;
Note: Above query will work only for dataset where check_in & check_out happened within same year.
Check sample query output here in Fiddle
I tried many way but didn't get expected result. How can I get expected result.
You can download the dbtable structure from here :-
SELECT
IF(o.source_id = 1, 'online',0) TYPE,
MONTH(date_created) AS monthvalue
,SUM(total_price) AS totalSale
FROM orders o
WHERE o.source_id = 1 AND date_created BETWEEN(CURDATE() - INTERVAL 1 MONTH)
AND CURDATE()
GROUP BY monthvalue
UNION ALL
SELECT
IF(o.source_id = 2, 'visit',0) TYPE,
MONTH(date_created) AS monthvalue
,SUM(total_price) AS totalSale
FROM orders o
WHERE o.source_id =2 AND date_created BETWEEN(CURDATE() - INTERVAL 1 MONTH)
AND CURDATE()
GROUP BY monthvalue
UNION ALL
SELECT
IF(o.source_id = 3, 'phone',0) TYPE,
MONTH(date_created) AS monthvalue
,SUM(total_price) AS totalSale
FROM orders o
WHERE o.source_id = 3 AND date_created BETWEEN(CURDATE() - INTERVAL 1 MONTH)
AND CURDATE()
GROUP BY monthvalue
Got Result :-
TYPE monthvalue totalSale
online 6 38300
visit 6 16800
phone 5 500
phone 6 7150
Expected Result :
Type May June
online 0 38300
Visit 0 16800
Phone 500 7150
According to your database schema, if you just want your expected result, please try following sql;)
select
case source_id when 1 then 'online' when 2 then 'visit' when 3 then 'phone' end as `Type`,
sum(if(month(date_created) = 5, total_price, 0)) as `May`,
sum(if(month(date_created) = 6, total_price, 0)) as `June`
from `orders`
where date_created between (curdate() - interval 1 month) and curdate()
group by source_id
SQLFiddle DEMO HERE.
I don't have any idea how i can get this information.
This is my query
SELECT
CAST(reports.report_creation_datetime AS DATE) AS report_date,
MIN(CAST(reports.report_creation_datetime AS TIME)) AS work_start, <--- I want to get id of this row
MAX(CAST(reports.report_creation_datetime AS TIME)) AS work_end <--- and this
FROM reports
WHERE reports.report_creation_datetime >= '2016-02-01'
AND reports.report_creation_datetime < LAST_DAY('2016-02-01') + INTERVAL 1 DAY
GROUP BY CAST(reports.report_creation_datetime AS DATE)
SAMPLE DATA
---reports table
report_id report_creation_datetime
------------------------------------
82 2016-02-01 07:20:00
80 2016-02-01 10:30:00
85 2016-02-01 17:00:00
88 2016-02-02 08:00:00
87 2016-02-02 16:00:00
81 2016-02-03 10:50:00
I want to get
---expected result
report_date work_start wstart_id ...
-------------------------------------------------------
2016-02-01 07:20:00 82 ...
2016-02-02 08:00:00 88 ...
2016-02-03 10:50:00 81 ...
...
2016-02-29 07:30:00 199 ...
I think about subquery like this
WHERE date = MIN(date)
but it is not correct
There are a couple different ways to do this. You can join the table back to itself using aggregation in both subqueries. You could use user-defined variables.
Perhaps the easiest solution is 2 correlated subqueries:
SELECT
CAST(r.report_creation_datetime AS DATE) AS report_date,
MIN(CAST(r.report_creation_datetime AS TIME)) AS work_start,
(SELECT id FROM reports r2
WHERE CAST(r.report_creation_datetime AS DATE) = CAST(r2.report_creation_datetime AS DATE)
ORDER BY r2.report_creation_datetime
LIMIT 1) work_start_id,
MAX(CAST(reports.report_creation_datetime AS TIME)) AS work_end,
(SELECT id FROM reports r3
WHERE CAST(r.report_creation_datetime AS DATE) = CAST(r3.report_creation_datetime AS DATE)
ORDER BY r3.report_creation_datetime DESC
LIMIT 1) work_end_id
FROM reports r
WHERE r.report_creation_datetime >= '2016-02-01'
AND r.report_creation_datetime < LAST_DAY('2016-02-01') + INTERVAL 1 DAY
GROUP BY CAST(r.report_creation_datetime AS DATE)
Here's an alternative which should return ties as well:
SELECT t.report_date, t.work_start, t.work_end, r1.id minid, r2.id maxid
FROM (
SELECT
CAST(reports.report_creation_datetime AS DATE) AS report_date,
MIN(CAST(reports.report_creation_datetime AS TIME)) AS work_start,
MAX(CAST(reports.report_creation_datetime AS TIME)) AS work_end
FROM reports
WHERE reports.report_creation_datetime >= '2016-02-01'
AND reports.report_creation_datetime < LAST_DAY('2016-02-01') + INTERVAL 1 DAY
GROUP BY CAST(reports.report_creation_datetime AS DATE)
) t
JOIN reports r1 ON t.report_date = CAST(r1.report_creation_datetime AS DATE) AND t.work_start = CAST(r1.report_creation_datetime AS TIME)
JOIN reports r2 ON t.report_date = CAST(r2.report_creation_datetime AS DATE) AND t.work_end = CAST(r2.report_creation_datetime AS TIME)
Did you want to achieve something like this? (not tested)
SELECT distinct CAST(mainReports.report_creation_datetime AS DATE) AS
report_date, workStartReports.id as StartId, workEndReports.id as EndId FROM
(SELECT MIN(CAST(report_creation_datetime AS TIME)) as minDate,
MAX(CAST(report_creation_datetime AS TIME)) as maxDate From reports WHERE
report_creation_datetime >= '2016-02-01' AND report_creation_datetime <
LAST_DAY('2016-02-01') + INTERVAL 1 DAY) as MinMaxDates, reports as
mainReports, reports as workStartReports, reports as workEndReports WHERE
mainReports.report_creation_datetime >= '2016-02-01' AND
mainReports.report_creation_datetime < LAST_DAY('2016-02-01') + INTERVAL 1 DAY
AND workStartReports.report_creation_datetime = MinMaxDates.minDate AND
workEndReports.report_creation_datetime = MinMaxDates.maxDate
I have two similar mysql tables and want to get the data from both of them in one output (merge). How can I do it?
For this reason I have created two separate queries to check that I get what I'm looking for.
First: Table -> web_session
SELECT date_format(booking_time, '%m-%Y') AS m, count(booking_time) AS b
FROM web_session
WHERE
date(booking_time) >= date_sub(curdate(), interval 12 month)
AND
date(booking_time) <= date_add(curdate(), interval 6 month)
GROUP BY month(booking_time)
ORDER BY booking_time ASC;
Output:
m, b
10-2013, 15
11-2013, 6
12-2013, 13
01-2014, 10
02-2014, 10
03-2014, 25
04-2014, 1
Second: Table -> web_log
SELECT date_format(request_time, '%m-%Y') AS m, count(request_time) AS r
FROM web_log
WHERE
date(request_time) >= date_sub(curdate(), interval 12 month)
AND
date(request_time) <= date_add(curdate(), interval 6 month)
GROUP BY month(request_time)
ORDER BY request_time ASC;
Output:
m, r
03-2014, 45
04-2014, 35
desired output:
m, b, r
10-2013, 15, null
11-2013, 6, null
12-2013, 13, null
01-2014, 10, null
02-2014, 10, null
03-2014, 25, 45
04-2014, 1, 35
You need to join them. Try this:
SELECT T1.m,T1.b,T2.r FROM
(
SELECT date_format(booking_time, '%m-%Y') AS m, count(booking_time) AS b
FROM web_session
WHERE
date(booking_time) >= date_sub(curdate(), interval 12 month)
AND
date(booking_time) <= date_add(curdate(), interval 6 month)
GROUP BY month(booking_time)
ORDER BY booking_time ASC;) T1
LEFT OUTER JOIN
(SELECT date_format(request_time, '%m-%Y') AS m, count(request_time) AS r
FROM web_log
WHERE
date(request_time) >= date_sub(curdate(), interval 12 month)
AND
date(request_time) <= date_add(curdate(), interval 6 month)
GROUP BY month(request_time)
ORDER BY request_time ASC;) T2
ON T1.m=T2.m
ORDER BY T1.m
Output will be:
m b r
10-2013 15 null
11-2013 6 null
12-2013 13 null
01-2014 10 null
02-2014 10 null
03-2014 25 45
04-2014 1 35