MySQL: How to select data from a column in another table - mysql

I have the following select statement:
SELECT
items.id,
items.name AS item_name,
items.tobuy,
items.list_id,
items.note,
items.unit,
MIN(NULLIF(packages.ppu, 0)) AS mppu,
packages.price AS mprice,
items._deleted
FROM
items
INNER JOIN
lists ON lists.id = items.list_id
LEFT JOIN
packages ON items.id = packages.item_id
WHERE
lists.user_id = 1 AND
items._deleted = '0'
GROUP BY
items.id
ORDER BY
tobuy DESC,
item_name
But what I really want is the price to come from the package that has the minimum ppu (which is not necessarily the package with the minimum price).
Any ideas?
Sample Records:
Table: items:
id, name, tobuy, list_id, note, unit, _deleted
95, test1, 1, 1, null, null, 0
69, test2, 1, 1, null, null, 0
194, test3, 1, 1, null, null, 0
162, test4, 1, 1, null, null, 0
Table: lists:
id, name, user_id
1, list1, 1
Table: packages:
id, item_id, price, ppu
392, 95, 0, 0
117, 95, 13.49, 0.078
391, 95, 0, 0
386, 69, 0, 0
387, 69, 0, 0
388, 69, 0, 0
368, 194, 4.58, 0.138
18, 194, 3.38, 0.177
17, 194, 3.88, 0.144
The results should be four items with the following information:
id, item_name, tobuy, list_id, note, unit, mppu, mprice, _deleted
95, test1, 1, 1, null, null, 0.078, 13.49, 0
69, test2, 1, 1, null, null, 0, 0, 0
194, test3, 1, 1, null, null, 0.138, 4.58, 0
162, test4, 1, 1, null, null, 0, 0, 0
Notice that item 162 doesn't have any corresponding packages, but it still shows up in the list. This is the reason for the "LEFT JOIN"
BTW, "mppu" stands for "minimum price per unit"

can you try for the 2nd left:
packages ON ( packages.item_id = items.id and select min(packages.ppu) from packages where item_id=item.id)
also make sure to change group by to items.id
not tested

With mysql this is surprisingly simplex.
Order by ppn and move the group by to an outer query
SELECT * FROM (
SELECT
items.id,
items.name AS item_name,
items.tobuy,
items.list_id,
items.note,
items.unit,
MIN(NULLIF(packages.ppu, 0)) AS mppu,
packages.price AS mprice,
items._deleted
FROM items
INNER JOIN lists
ON lists.id = items.list_id
AND lists.user_id = 1
LEFT JOIN packages
ON items.id = packages.item_id
WHERE items._deleted = '0'
ORDER BY
ppm,
tobuy DESC,
item_name) x
GROUP BY
id

The following works to get the results that I was looking for.
SELECT * FROM (
SELECT
items.id,
items.name AS item_name,
items.tobuy,
items.list_id,
items.note,
items.unit,
NULLIF(packages.ppu, 0) AS mppu,
packages.price AS mprice,
items._deleted
FROM
items
INNER JOIN
lists ON lists.id = items.list_id
AND lists.user_id = 1
LEFT JOIN
packages ON packages.item_id = items.id
WHERE
items._deleted = '0'
ORDER BY
tobuy DESC,
item_name,
IFNULL(mppu, 999999)) x
GROUP BY
x.id
ORDER BY
tobuy DESC,
item_name
Thanks to everyone for the input to get me through this.

Related

how to handle If a subquery (inner query) returns a null value to the outer query

I have a vertical table like this:
id
Profile_id
feature_id
value
1
1
1
Rick
2
1
2
Novak
3
5
3
5428
4
5
1
Joe
...
...
...
...
(above table is short part of profile_features table)
I have a query:
SELECT * FROM(SELECT `value` TelNum FROM `profile_features` WHERE `feature_id` IN (10, 64, 103) AND `profile_id` = 16752 LIMIT 1) as TelNum,
(SELECT `value` NCode From `profile_features` WHERE `feature_id` IN (5, 61, 100) AND `profile_id` = 16752 LIMIT 1) AS NCode,
(SELECT `value` Fname From `profile_features` WHERE `feature_id` IN (1, 55, 86) AND `profile_id` = 16752 LIMIT 1) AS Fname,
(SELECT `value` Lname From `profile_features` WHERE `feature_id` IN (2, 56, 95) AND `profile_id` = 16752 LIMIT 1) AS Lname
but if one of the subqueries returns null, query have not any output.
I want a row result with TelNum, NCode, Fname, Lname either they are Null or not null.
You could use scalar subqueries in the select-list instead of your current Cartesian product:
SELECT (SELECT `value` FROM `profile_features` WHERE `feature_id` IN (10, 64, 103) AND `profile_id` = 16752 LIMIT 1) as TelNum,
(SELECT `value` From `profile_features` WHERE `feature_id` IN (5, 61, 100) AND `profile_id` = 16752 LIMIT 1) AS NCode,
(SELECT `value` From `profile_features` WHERE `feature_id` IN (1, 55, 86) AND `profile_id` = 16752 LIMIT 1) AS Fname,
(SELECT `value` From `profile_features` WHERE `feature_id` IN (2, 56, 95) AND `profile_id` = 16752 LIMIT 1) AS Lname;
Alternatively, you could learn to use LEFT OUTER JOIN:
SELECT TelNum.TelNum, NCode.NCode, Fname.Fname, Lname.Lname
FROM (SELECT NULL AS dummy) AS dummy
LEFT OUTER JOIN (SELECT `value` TelNum FROM `profile_features` WHERE `feature_id` IN (10, 64, 103) AND `profile_id` = 16752 LIMIT 1) AS TelNum ON true
LEFT OUTER JOIN (SELECT `value` NCode From `profile_features` WHERE `feature_id` IN (5, 61, 100) AND `profile_id` = 16752 LIMIT 1) AS NCode ON true
LEFT OUTER JOIN (SELECT `value` Fname From `profile_features` WHERE `feature_id` IN (1, 55, 86) AND `profile_id` = 16752 LIMIT 1) AS Fname ON true
LEFT OUTER JOIN (SELECT `value` Lname From `profile_features` WHERE `feature_id` IN (2, 56, 95) AND `profile_id` = 16752 LIMIT 1) AS Lname ON true;

Get previous X days of revenue for each group

Here is my table
CREATE TABLE financials (
id INT(6) UNSIGNED AUTO_INCREMENT PRIMARY KEY,
CountryID VARCHAR(30) NOT NULL,
ProductID VARCHAR(30) NOT NULL,
Revenue INT NOT NULL,
cost INT NOT NULL,
reg_date TIMESTAMP
);
INSERT INTO `financials` (`id`, `CountryID`, `ProductID`, `Revenue`, `cost`, `reg_date`) VALUES
( 1, 'Canada', 'Doe' , 20, 5, '2010-01-31 12:01:01'),
( 2, 'USA' , 'Tyson' , 40, 15, '2010-02-14 12:01:01'),
( 3, 'France', 'Keaton', 80, 25, '2010-03-25 12:01:01'),
( 4, 'France', 'Keaton',180, 45, '2010-04-24 12:01:01'),
( 5, 'France', 'Keaton', 30, 6, '2010-04-25 12:01:01'),
( 6, 'France', 'Emma' , 15, 2, '2010-01-24 12:01:01'),
( 7, 'France', 'Emma' , 60, 36, '2010-01-25 12:01:01'),
( 8, 'France', 'Lammy' ,130, 26, '2010-04-25 12:01:01'),
( 9, 'France', 'Louis' ,350, 12, '2010-04-25 12:01:01'),
(10, 'France', 'Dennis',100,200, '2010-04-25 12:01:01'),
(11, 'USA' , 'Zooey' , 70, 16, '2010-04-25 12:01:01'),
(12, 'France', 'Alex' , 2, 16, '2010-04-25 12:01:01');
For each product and date combination, I need to get the revenue for previous 5 days. For instance, for Product ‘Keaton’, the last purchase was on 2010-04-25, it will only sum up revenue between 2010-04-20 to 2010-04-25 and therefore it will be 210. While for "Emma", it would return 75, since it would sum everything between 2010-01-20 to 2010-01-25.
SELECT ProductID, sum(revenue), reg_date
FROM financials f
Where reg_date in (
SELECT reg_date
FROM financials as t2
WHERE t2.ProductID = f.productID
ORDER BY reg_date
LIMIT 5)
Unfortunately, when i use either https://sqltest.net/ or http://sqlfiddle.com/ it says that 'LIMIT & IN/ALL/ANY/SOME subquery' is not supported. Would my query work or not?
Your query is on the right track, but probably won't work in MySQL. MySQL has limitations on the use of in and limit with subqueries.
Instead:
SELECT f.ProductID, SUM(f.revenue)
FROM financials f JOIN
(SELECT ProductId, MAX(reg_date) as max_reg_date
FROM financials
GROUP BY ProductId
) ff
ON f.ProductId = ff.ProductId and
f.reg_date >= ff.max_reg_date - interval 5 day
GROUP BY f.ProductId;
EDIT:
If you want this for each product and date combination, then you can use a self join or correlated subquery:
SELECT f.*,
(SELECT SUM(f2.revenue)
FROM financials f2
WHERE f2.ProductId = f.ProductId AND
f2.reg_date <= f.reg_date AND
f2.reg_date >= f.reg_date - interval 5 day
) as sum_five_preceding_days
FROM financials f;
After some trials I ended up with some complex query, that I think it solves your problem
SELECT
financials.ProductID, sum(financials.Revenue) as Revenues
FROM
financials
INNER JOIN (
SELECT ProductId, GROUP_CONCAT(id ORDER BY reg_date DESC) groupedIds
FROM financials
group by ProductId
) group_max
ON financials.ProductId = group_max.ProductId
AND FIND_IN_SET(financials.id, groupedIds) BETWEEN 1 AND 5
group by financials.ProductID
First I used group by financials.ProductID to count revenues by products. The real problem you are facing is eliminating all rows that are not in the top 5, for each group. For that I used the solution from this question, GROUP_CONCAT and FIND_IN_SET, to get the top 5 result without LIMIT. Instead of WHERE IN I used JOIN but with this, WHERE IN might also work.
Heres the FIDDLE

SQL: count and sum based on conditions from the related table

I have two tables;
countries(id, name, region);
1, 'UK', '1';
2, 'USA', '1';
3, 'AUSTRALIA', '1';
4, 'CHINA', '0';
5, 'INDIA', '0';
6, 'SRI LANKA', '0' ;
and
tickets(id, country_id, issued_date, holder, gender, fee, canceled);
100, 2, 2017-08-15, 'Person 1', 'M', 200, '1';
101, 2, 2017-08-15, 'Person 2', 'M', 200, '0';
103, 3, 2017-08-15, 'Person 3', 'M', 200, '0';
104, 5, 2017-08-16, 'Person 1', 'M', 200, '0';
105, 6, 2017-08-16, 'Person 1', 'M', 200, '0';
106, 1, 2017-08-17, 'Person 1', 'M', 200, '0';
107, 3, 2017-08-18, 'Person 1', 'M', 200, '1';
108, 4, 2017-08-18, 'Person 1', 'M', 200, '0';
I want to group all the tickets based on issued_date with some aggregates fields to generate the summary. Here is my query:-
SELECT
issued_date,
COUNT(*) as total_tickets,
COUNT(CASE WHEN canceled = '0' THEN 1 ELSE NULL END) as issued_tickets,
SUM(CASE WHEN canceled = '0' THEN fee ELSE NULL END) as total_amount
FROM tickets
GROUP BY issued_date;
But, how to use COUNT and SUM for related table countries? For example, I want to show how many tickets were sold on a date (2017-08-15) from a country having region = '1'.
I tried the following, but the results are not correct for region_1 field
SELECT
issued_date,
COUNT(*) as total_tickets,
COUNT(CASE WHEN canceled = '0' THEN 1 ELSE NULL END) as issued_tickets,SUM(CASE WHEN canceled = '0' THEN fee ELSE NULL END) as total_amount,
(SELECT COUNT(countries.id) FROM countries WHERE countries.id = tickets.country_id && countries.region = '1') as region_1
FROM tickets
GROUP BY issued_date;
I would use a derived table that is grouped on issue_date, country_id and region and use that derived table in an inner join.
SELECT issued_date
,COUNT(*) AS total_tickets
,COUNT(CASE WHEN canceled = '0' THEN 1 ELSE NULL END) AS issued_tickets
,SUM(CASE WHEN canceled = '0' THEN fee ELSE NULL END) AS total_amount
,tickets_by_region.total_region_1_tickets
FROM tickets
INNER JOIN (
SELECT issued_date
,country_id
,countries.region
,COUNT(*) AS total_region_1_tickets
FROM tickets
INNER JOIN countries ON (countries.id = tickets.country_id)
GROUP BY issued_date
,countries.country_id
,countries.region
) tickets_by_region ON (
tickets_by_region.issued_date = tickets.issued_date
AND tickets_by_region.country_id = tickets.country_id
AND tickets_by_region.region = '1'
) AS region_1
GROUP BY issued_date;
HTH.
You probably need to use INNER JOIN and GROUP BY with HAVING which allow append next table on related keys and add to additional summary or counts you need to use them like sub-query because they need to work with no filtered data.
Approach is prepare data in sub-query and then JOIN the filtered data
to your main table which can do final filtering for final result.
SQL can looks like bellow (tested on local)
SELECT t1.issued_date, COUNT(t1.id) as sum_tickets, t2.region, t3.total_tickets
FROM tickets t1
LEFT JOIN (SELECT id, COUNT(id) as total_tickets FROM tickets) t3 ON t3.id = t1.id
INNER JOIN countries t2 ON t2.id = t1.country_id
GROUP BY t1.issued_date
HAVING (t2.region = '1')
Output is
issued_date, sum_tickets, region, total_tickets
2017-08-15, 3, 1, 8
2017-08-17, 1, 1, null
2017-08-18, 2, 1, null
You can add more conditions to HAVING in query.
I would just use conditional aggregation with a JOIN:
SELECT t.issued_date, COUNT(*) as total_tickets,
SUM(t.canceled = 0) as issued_tickets,
SUM(CASE WHEN t.canceled = 0 THEN t.fee END) as total_amount,
SUM(c.region = 1) as num_region_1
FROM tickets t JOIN
countries c
ON t.country_id = c.id
GROUP BY t.issued_date;

Different outputs for each condition.get together group by all values - single mysql query

Here how my tables look like:
CREATE TABLE my_table(id INT,project_id VARCHAR(6),order_id VARCHAR(6),user_id VARCHAR(6),owner_id VARCHAR(6));
INSERT INTO my_table
VALUES
(1, 211541, 8614, 1605, 0),
(2, 211541, 8614, 16079, 1605),
(3, 210446, 0, 12312, 0),
(4, 208216, 0, 16467, 14499),
(5, 208216, 0, 14499, 0),
(6, 208216, 0, 14499, 0),
(7, 208216, 0, 16467, 14499),
(8, 209377, 0, 7556, 0),
(9, 209324, 0, 7556, 0),
(10,201038, 8602, 9390, 101);
I have to check split Multiple condtion:
Query Execution this kind of way.
order_id != 0
Initially goes to project_id,
(i.e)
1.project_id - 211541 then first condition (owner_id = 0) , select user_id
note:
- if not get user_id(empty result) - goes to second condition.
- if get user_id - do not go to second condtion.
2.project_id - 211541 - second condtion (owner_id != 0), select owner_id.
i got
my_user_id
1605
101
order_id = 0
(i.e)
1.project_id - 208216 then first condition (owner_id = 0) , select group by user_id
note:
- if not get user_id(empty result) - goes to second condition.
- if get user_id - do not go to second condtion.
2.project_id - 208216 - second condtion (owner_id != 0), select group by owner_id.
i got
my_user_id
123121449975567556
Finally, i need this answer - group by my_user_id
my_user_id
160510112312144997556
Note:
I need single query.
why not just use an IF?
SELECT
IF (order_id = 0, user_id, owner_id) AS new_val
FROM my_table
GROUP BY new_val
when looking at it more it seems like you need a few more ifs.. something like this?
SELECT
if(order_id <> 0,
if(owner_id = 0, user_id, owner_id),
if(user_id = 0, owner_id, user_id)
) AS new_val
FROM my_table
group by new_val
this is what i understand from your conditions
ill number them and then put them with the if conditions i'll build in a second
if the order_id is not 0, -- 1
check to see if the owner_id is 0,
if owner_id = 0 -- 2
then pull in user_id -- 3
else owner_id is not 0
and you pull in owner_id -- 4
to write this more like code..
if(order_id <> 0, if(owner_id <> 0, owner_id, user_id), some condition for when order_id is 0)
other case
if the order_id is 0
pull in user_id (grouped)
if user_id = 0 -- 5
then pull in owner_id -- 6
else user_id is not 0
and pull in user_id -- 7
to put this with the other part replace the some other condition for when its 0.
-- 1 2 3 4 5 6 7
if(order_id <> 0, if(owner_id = 0, user_id, owner_id), if(user_id = 0, owner_id, user_id))
now to format it so its readable
if(order_id <> 0, -- if its not 0
if(owner_id = 0, user_id, owner_id), -- true condition
if(user_id = 0, owner_id, user_id) -- false condition
)
am I correct?
You can use 'if'.
Eg
#result=if(1!=2,'yes','no');
Which would give result a value of 'yes'.
These can be nested to create complex conditions:
SET #value_a='no';
set #value_b='';
set #value_c=’test’;
SET #value_d=’’;
set #result=
(
select if(#value_a!=’no’,(SELECT column1 FROM table1 WHERE id= #value_a),
if(#value_b!='',#value_b,
if(#value_c!='',(select column2 from table2 where id=#value_a),
if(#value_d!='',(select column3 from table3 where id=#value_a),
‘I am a default value’
)
)
)
)
);
Giving whatever select column2 from table2 where id=#value_a gives.

MySQL include zero rows when using COUNT with LEFT OUTER JOIN and GROUP BY

How can I avoid eliminating the users with zero meetings? I'm aware there are similar questions, but this code is quite a bit more complex.
SELECT user.userID, user.contactName, user.email, COUNT( * ) AS meetingsCount
FROM user
LEFT OUTER JOIN meeting ON user.userID = meeting.userID
WHERE user.userID NOT
IN ( 1, 2, 3, 4, 5, 59, 62, 63, 64, 66, 69, 71, 72, 73, 78, 107 )
AND SUBSTRING( meeting.meetingCode, 5, 2 )
BETWEEN 12
AND 22
AND SUBSTRING( meeting.meetingCode, 7, 2 )
BETWEEN 01
AND 12
AND SUBSTRING( meeting.meetingCode, 9, 2 )
BETWEEN 01
AND 31
GROUP BY user.userID, contactName, email
ORDER BY meetingsCount DESC
You need to put the logic for the meeting code table in your join. Otherwise users matching the records you are filtering out from the meeting table will be filtered out of your results. Making your JOIN essentially an INNER join. I think you also should put single quotes around the values in your BETWEEN clauses.
SELECT user.userID, user.contactName, user.email, COUNT( meeting.userID ) AS meetingsCount
FROM user
LEFT OUTER JOIN meeting ON user.userID = meeting.userID
AND SUBSTRING( meeting.meetingCode, 5, 2 ) BETWEEN '12' AND '22'
AND SUBSTRING( meeting.meetingCode, 7, 2 ) BETWEEN '01' AND '12'
AND SUBSTRING( meeting.meetingCode, 9, 2 ) BETWEEN '01' AND '31'
WHERE user.userID NOT IN ( 1, 2, 3, 4, 5, 59, 62, 63, 64, 66, 69, 71, 72, 73, 78, 107 )
GROUP BY user.userID, contactName, email
ORDER BY meetingsCount DESC