MySQL - conditional select in group - mysql

I have a simple table with several rows and I would like to group them by id_room and select value only when condition is true. Problem is that condition is always false even there is a row with right date column year_month.
Here is schema:
CREATE TABLE tbl_account_room (
`id` int,
`year_month` date,
`value` int,
`id_room` int
);
INSERT INTO tbl_account_room
(`id`, `year_month`, `value`, `id_room`)
VALUES
(1, '2016-08-01', 1, 300),
(2, '2016-09-01', 2, 300),
(3, '2016-10-01', 3, 300);
and here query:
SELECT
(case when '2016-10-01' = ar.year_month then ar.value else 0 end) as total
FROM tbl_account_room AS ar
WHERE ar.year_month >= "2016-08-01"
AND ar.year_month <= "2016-11-01"
and ar.id_room = '300'
GROUP BY ar.id_room
LIMIT 10
Here is SQL Fiddle
In column total I'm getting 0 and I would like to get value 3 because year_month is 2016-10-01. Why this happens?

You certainly don't need that CASE condition and include that condition in your WHERE clause rather like
SELECT
ar.value as total,
GROUP_CONCAT(ar.year_month)
FROM tbl_account_room AS ar
WHERE ar.year_month = '2016-10-01'
GROUP BY ar.id_room;

Not sure why you want the result like that, here you can use self join to do that:
SELECT
MAX(t1.value) as total,
GROUP_CONCAT(ar.year_month)
FROM tbl_account_room AS ar
LEFT JOIN tbl_account_room AS t1
ON ar.id_room = t1.id_room
AND ar.year_month = t1.year_month
AND t1.year_month = '2016-10-01'
WHERE ar.year_month >= "2016-08-01"
AND ar.year_month <= "2016-11-01"
and ar.id_room = '300'
GROUP BY ar.id_room
LIMIT 10;
and here is SQLFiddle Demo.

Related

Is there any way to optimize this MySQL query when merging two query?

I was trying to get the result with product_id, stock, new_cost
SELECT inventory_logs.product_id, ROUND(SUM(inventory_logs.qty + 0), 4) AS stock
FROM products
JOIN inventory_logs on products.id = inventory_logs.product_id
WHERE inventory_logs.`type` = 'as'
AND inventory_logs.`created_at` between '1970-01-01' and '2021-02-22 23:59:59'
AND inventory_logs.`branch_id` = 1
AND inventory_logs.`deleted_at` IS NULL AND products.deleted_at IS NULL
GROUP BY inventory_logs.product_id HAVING stock > 0
above that is the query for getting stock,
duration for query is about 0.031 sec
SELECT inventory_logs.product_id, inventory_logs.new_cost
FROM inventory_logs JOIN(
SELECT product_id, MAX(created_at) AS created_at
FROM inventory_logs
WHERE created_at <= '2021-02-22 23:59:59' AND deleted_at IS NULL
AND `type` = 'as'
AND inventory_logs.`branch_id` = 1
GROUP BY product_id
) i2 ON (inventory_logs.product_id = i2.product_id AND inventory_logs.created_at = i2.created_at)
WHERE `type` = 'as'
AND inventory_logs.`branch_id` = 1
above is the query for getting new_cost, duration for query is also about 0.031 sec
But when I try to merge the above two query into one, the duration of query is 26.016 sec
The query is
SELECT inventory_logs.product_id, ROUND(SUM(inventory_logs.qty = 0), 4) AS stock, sub.new_cost
FROM products
JOIN inventory_logs ON products.id = inventory_logs.product_id
JOIN (
SELECT inventory_logs.product_id, inventory_logs.new_cost
FROM inventory_logs JOIN (
SELECT product_id, MAX(created_at) AS created_at
FROM inventory_logs
WHERE created_at <= '2021-02-22 23:59:59' AND deleted_at IS NULL
AND `type` = 'as'
AND inventory_logs.`branch_id` = 1
GROUP BY product_id
) i2 ON (inventory_logs.product_id = i2.product_id AND inventory_logs.vreated_at
= i2.created_at)
WHERE `type` = 'as'
AND inventory_logs.`branch_id` = 1
) sub ON products.id = sub.product_id
WHERE inventory_logs.`type` = 'as'
AND inventory_logs.`created_at` BETWEEN '1970-01-01' AND '2021-02-22 23:59:59'
AND inventory_logs.`branch_id` = 1
AND inventory_logs.`deleted_at` IS NULL
AND products.deleted_at IS NULL
GROUP BY inventory_logs.product_id HAVING stock > 0
Is there any ways to optimize the above query to make it take lesser time?

SQL Query Issue - Picking the minimum time when there is a maximum number

SQL God...I need some help!
I have a data table that has a route_complete_percentage column and a created_at column.
I need two pieces of data:
the time stamp (within created_at column) when the route_complete_percentage is at its minimum but not zero
the time stamp (within created_at column) when the route_complete_percentage is at its maximum, it might be 100% or not, but when its at its highest.
Here is the kicker, there might be multiple time stamps for the highest route completion column. For example,
Example Table
I have multiple values when the route_completion_percentage is at its maximum, but I need the minimum time stamp value.
Here is the query so far...but the two time stamps are the same.
SELECT
A.fc,
A.plan_id,
A.route_id,
mintime.first_scan AS First_Batch_Scan,
min(route_complete_percentage),
maxtime.last_scan AS Last_Batch_Scan,
max(route_complete_percentage)
FROM
(SELECT
fc,
plan_id,
route_id,
route_complete_percentage,
CONCAT(plan_id, '-', route_id) AS JOINKEY
FROM
houdini_ops.BATCHINATOR_SCAN_LOGS_V2
WHERE
fc <> ''
AND order_id <> 'Can\'t find order'
AND source = 'scan'
AND created_at > DATE_ADD(CURDATE(), INTERVAL - 3 DAY)) A
LEFT JOIN
(SELECT
l.fc,
l.route_id,
l.plan_id,
CONCAT(l.plan_id, '-', l.route_id) AS JOINKEY,
CASE
WHEN MIN(route_complete_percentage) THEN CONVERT_TZ(l.created_at, 'UTC', s.time_zone)
END AS first_scan
FROM
houdini_ops.BATCHINATOR_SCAN_LOGS_V2 l
JOIN houdini_ops.O_SERVICE_AREA_ATTRIBUTES s ON l.fc = s.default_station_code
WHERE
l.fc <> ''
AND l.order_id <> 'Can\'t find order'
AND l.source = 'scan'
AND l.created_at > DATE_ADD(CURDATE(), INTERVAL - 3 DAY)
GROUP BY fc , plan_id , route_id) mintime ON A.JOINKEY = mintime.JOINKEY
LEFT JOIN
(SELECT
l.fc,
l.route_id,
l.plan_id,
CONCAT(l.plan_id, '-', l.route_id) AS JOINKEY,
CASE
WHEN MAX(route_complete_percentage) THEN CONVERT_TZ(l.created_at, 'UTC', s.time_zone)
END AS last_scan
FROM
houdini_ops.BATCHINATOR_SCAN_LOGS_V2 l
JOIN houdini_ops.O_SERVICE_AREA_ATTRIBUTES s ON l.fc = s.default_station_code
WHERE
l.fc <> ''
AND l.order_id <> 'Can\'t find order'
AND l.source = 'scan'
AND l.created_at > DATE_ADD(CURDATE(), INTERVAL - 3 DAY)
GROUP BY fc , plan_id , route_id) maxtime ON mintime.JOINKEY = maxtime.JOINKEY
GROUP BY fc , plan_id , route_id
I don't want to meddle with the rest of your query. Here is something that will do what it sounds like you need. There's sample data included. -- I interpreted your blank values as nulls from your sample data.
Basically, what you are looking for is the Minimum created_at value, inside each of the route_complete_percentage groups. So I treated route_complete_percentage as a group identifier. But you only care about two of the groups, so I identify those groups first in the cte, and use them to filter the aggregate query.
if object_id('tempdb.dbo.#Data') is not null drop table #Data
go
create table #Data (
route_complete_percentage int,
created_at datetime
)
insert into #Data (route_complete_percentage, created_at)
values
(0, '20170531 19:58'),
(1, null),
(2, null),
(3, null),
(4, null),
(5, null),
(6, null),
(7, null),
(80, null),
(90, null),
(100, '20170531 20:10'),
(100, '20170531 20:12'),
(100, '20170531 20:15')
;with cteMinMax(min_route_complete_percentage, max_route_complete_percentage) as (
select
min(route_complete_percentage),
max(route_complete_percentage)
from #Data D
-- This ensures the condition that you don't get the timestamp for 0
where D.route_complete_percentage > 0
)
select
route_complete_percentage,
min_created_at = min(created_at)
from #Data D
join cteMinMax MM on D.route_complete_percentage in (MM.min_route_complete_percentage, MM.max_route_complete_percentage)
group by route_complete_percentage

Sql update where left join count equal to 3

Hi I'm trying to build a sql query where I update the value of a table where the left join of another table is equal to 3.
Example when a vehicle has 3 photos.
The query I've written thus far, it seems to fail with group by though.
UPDATE domain.vehicle_listing AS t0 LEFT OUTER JOIN photo AS t1 ON t0.id = t1.vehicle_listing_id
SET t0.active = 0
WHERE `create_date` >= '2015-5-2'
AND user_profile_id is not null
AND t0.active = 1
GROUP BY t1.vehicle_listing_id
HAVING COUNT(DISTINCT t1.id) = 3
ORDER BY create_date desc;
Vehicle_Listing
id
Photo
id, vehicle_listing_id, photo_url
OneToMany relationship with photo.
You can also use exists
UPDATE vehicle_listing AS t0
SET t0.active = 0
WHERE t0.`create_date` >= '2015-05-02'
AND t0.user_profile_id is not null
AND t0.active = 1
AND EXISTS (
SELECT 1
FROM photo
WHERE vehicle_listing_id=t0.id
GROUP BY vehicle_listing_id
HAVING COUNT(DISTINCT id) = 3
)
Sample data for vehicle_listing
INSERT INTO vehicle_listing
(`id`, `title`, `create_date`, `active`,user_profile_id)
VALUES
(1, 'test', '2015-05-02 00:00:00', 1,1),
(2, 'test1', '2015-05-02 00:00:00', 1,1)
;
Sample data for photo
INSERT INTO photo
(`id`, `vehicle_listing_id`, `photo_url`)
VALUES
(1, 1, 'image.jpg'),
(2, 1, 'image.jpg'),
(3, 1, 'image.jpg'),
(4, 2, 'image.jpg'),
(5, 2, 'image.jpg')
;
Sample Output
id title create_date active user_profile_id
1 test May, 02 2015 00:00:00 0 1
2 test1 May, 02 2015 00:00:00 1 1
DEMO
It is silly to use a left join for this. You want inner join:
UPDATE cardaddy.vehicle_listing vl INNER JOIN
(SELECT p.vehicle_listing_id, count(*) as cnt
FROM photo p
GROUP BY p.vehicle_listing_id
) p
ON vl.id = p.vehicle_listing_id AND
p.cnt = 3
SET vl.active = 0
WHERE vl.create_date >= '2015-05-02' AND
vl.user_profile_id IS NOT NULL AND
vl.active = 1;
(Assuming that user_profile_id is in vehicle_listing.)
UPDATE cardaddy.vehicle_listing AS t0
LEFT OUTER JOIN (
SELECT vehicle_listing_id, count(1) AS counter
FROM photo
GROUP BY vehicle_listing_id
) AS t1
ON t0.id = t1.vehicle_listing_id
AND t1.counter = 3
SET t0.active = 0
WHERE `create_date` >= '2015-5-2'
AND user_profile_id IS NOT NULL
AND t0.active = 1
AND t1.vehicle_list_is IS NOT NULL

SQL statement for GROUP BY

I am really stucked with one sql select statement.
This is output/result which I get from sql statement below:
WHAT I need: I need to have columns assignedVouchersNumber and usedVouchersNumber together in one row by msisdn. So for example if you can see "msisdn" 723709656 there are two rows now.. one with assignedVouchersNumber = 1 and second with assignedVouchersNumber = 1 too.
But I need to have it in one row with assignedVouchersNumber = 2. Do you now where is the problem?
SELECT eu.msisdn,
eu.id as userId,
sum(case ev.voucherstate when '1' then 1 else 0 end) as assignedVouchersNumber,
sum(case ev.voucherstate when '2' then 1 else 0 end) as usedVouchersNumber,
ev.extra_offer_id,
ev.create_time,
ev.use_time,
ev.id as voucherId,
ev.voucherstate
FROM extra_users eu
JOIN (SELECT sn.msisdn AS telcislo,
stn.numberid
FROM stats_number sn
JOIN stats_target_number AS stn
ON ( sn.numberid = stn.numberid )
WHERE stn.targetid = 1) xy
ON eu.msisdn = xy.telcislo
JOIN extra_vouchers AS ev
ON ( eu.id = ev.extra_user_id )
WHERE ev.create_time BETWEEN '2012-07-23 00:00:00' AND '2013-08-23 23:59:59'
AND ev.use_time <= '2013-08-23 23:59:59'
AND ev.use_time >= '2012-07-23 00:00:00'
AND ev.voucherstate IN ( 1, 2 )
AND Ifnull(ev.extra_offer_id IN( 2335, 3195, 30538 ), 1)
GROUP BY eu.msisdn, ev.extra_offer_id, ev.voucherState
ORDER BY eu.msisdn ASC
You have two different extra_offer_id for same msisdn and VouchersNumber. Thats why you get two rows.
I got it... there should not be groupping by ev.voucherState in
GROUP BY eu.msisdn, ev.extra_offer_id, ev.voucherState
After then I have removed ev.voucherState it is working now.

mysql get difference of two sum on the same table

how can i get the result in just one query instead of this one:
SELECT SUM(`quantity`) as type0 FROM `fruits_delivery`
WHERE `fid`='1001' AND `type`=0;
result_1 = type0 ;
SELECT SUM(`quantity`) as type1 FROM `fruits_delivery`
WHERE `fid`='1001' AND `type`=1;
result_2 = type1 ;
final_result = result_1 - result_2;
You should use this
SELECT sum(IF(`type`=0, `quantity`, 0))-sum(IF(`type`=1, `quantity`, 0))
AS `final_result`
FROM `fruits_delivery`
WHERE `fid` = '1001'
sqlfiddle
Old Answer
SELECT T1.result - T2.result AS `final_result`
FROM (SELECT Sum(`quantity`) AS result,
`fid`
FROM `fruits_delivery`
WHERE `fid` = '1001'
AND `type` = 0
LIMIT 1) AS T1
JOIN (SELECT Sum(`quantity`) AS result,
`fid`
FROM `fruits_delivery`
WHERE `fid` = '1001'
AND `type` = 1
LIMIT 1) AS T2
ON ( T1.fid = T2.fid )
SQLFiddle
Alternatively, you can also do it using CASE
SELECT SUM(CASE WHEN type = 0 THEN quantity ELSE 0 END) -
SUM(CASE WHEN type = 1 THEN quantity ELSE 0 END)
AS final_result
FROM fruits_delivery
WHERE fid = '1001'
SQLFiddle Demo