MySQL nested Select statement with subquery - mysql

I am struggeling with a database query for 2 Hours now.
There is the following database structure:
article table
+---------------+-------------+
| id | ordernumber |
+---------------+-------------+
| 1 | 3243 |
| 2 | 3344 |
| 3 | 3423 |
| 4 | 7687 |
+---------------+-------------+
variant table
+----+-----------+-------+-------+
| id | articleId | stock | price |
+----+-----------+-------+-------+
| 1 | 1 | 3 | 10,99 |
| 2 | 1 | 0 | 10,99 |
| 3 | 1 | 1 | 10,99 |
| 4 | 2 | 0 | 11,99 |
| 5 | 2 | 0 | 11,99 |
| 6 | 2 | 1 | 11,99 |
+----+-----------+-------+-------+
I want to get all Articles where all but one variant have 0 stock.
Is this even possible with a plain sql statement? I tried with a subquery, but without success, since the subquery gets executed first and I would need to pass values from the current record of the resultset of the outer query.
Any help is much appreciated.
Edit:
Expected Result:
+----+-------------+
| id | ordernumber |
+----+-------------+
| 2 | 3344 |
+----+-------------+

If you want the full information for the variant:
select v.*
from variants v
where v.stock > 0 and
not exists (select 1
from variants v2
where v2.articleID = v.articleID and
v2.stock > 0 and
v2.id <> v.id
);
Note: this assumes that the duplicated "5" is a typo and that the ids really are unique in the table.

This can be done using group by and having.
select articleID
from variants
group by articleID
having count(*) - 1 = count(case when stock = 0 then 1 end)

Related

How can I get a purchase check in MySQL

I am not able to figure out how I can get the following result with one MySQL Query:
I have two tables:
shop_items
| id | description | price | active |
+----+-------------+-------+--------+
| 1 | product_1 | 5 | 1 |
+----+-------------+-------+--------+
| 2 | product_2 | 10 | 1 |
+----+-------------+-------+--------+
| 3 | product_3 | 15 | 0 |
+----+-------------+-------+--------+
inventory_items (the shop_items a user purchased)
| id | item_id | user_id | active |
+----+---------+---------+--------+
| 1 | 2 | 1 | 1 |
+----+---------+---------+--------+
| 2 | 1 | 1 | 0 |
+----+---------+---------+--------+
I want to see all shop_items where active = 1 including a row called purchased = 0 or 1 based on inventory_items -> matching user_id (where user_id = something) and active = 1
Example output based on the data from above tables -> where user_id = 1:
| item_id | price | description | purchased |
+---------+-------+-------------+-----------+
| 1 | 5 | product_1 | 0 |
+---------+-------+-------------+-----------+
| 2 | 10 | product_2 | 1 |
+---------+-------+-------------+-----------+
What query do I need for this output?
Please note: I only need the result from ONE user_id which I can change within the query :)
Test
SELECT shop_items.*, COALESCE(inventory_items.active, 0) purchased
FROM shop_items
LEFT JOIN inventory_items ON shop_items.id = inventory_items.item_id
AND user_id = 1
WHERE shop_items.active = 1

Finding average of the column generated from sql query having group by function

I want to find the average of the following data via mysql query (assume these are 719 rows).
| 1 |
| 3 |
| 1 |
| 2 |
| 2 |
| 1 |
| 1 |
| 2 |
| 1 |
| 1 |
| 2 |
| 1 |
| 2 |
| 1 |
| 2 |
| 1 |
| 1 |
+----------+
719 rows in set (2.43 sec)
SELECT COUNT(*) FROM osdial_agent_log WHERE DATE(event_time)='2015-11-01' GROUP BY lead_id;
I ran this query to get that data
Can someone help me to find the average for the above data.
Use
SELECT AVG(total)
FROM (SELECT COUNT(*) AS total
FROM osdial_agent_log
WHERE DATE(event_time)='2015-11-01'
GROUP BY lead_id) t

MySQL sum per IDs without subqueries

I'm working on a huge dataset, with a table that looks like this :
+----+---------+--------+--------+
| id | otherid | value1 | value2 |
+----+---------+--------+--------+
| 1 | 1 | 2 | 5 |
| 1 | 1 | 4 | 8 |
| 1 | 2 | 3 | 6 |
| 2 | 123 | 1 | 4 |
+----+---------+--------+--------+
I need to multiply value1 and value2 for each row, and sum values per id and otherid. A result table might be:
+----+---------+-----+
| id | otherid | sum |
+----+---------+-----+
| 1 | 1 | 42 | ((2*5)+(4*8))
| 1 | 2 | 18 | (3*6)
| 2 | 123 | 4 | (1*4)
+----+---------+-----+
My question is if it is possible to avoid subqueries to do this, I only found solutions that used them.
Thanks!
it's easy.
SELECT id,
otherid,
SUM(value1*value2) AS sum
FROM your_table
GROUP BY id, otherid;
Try Below Query
SELECT ID,otherid ,SUM(value1 * value2) sum
FROM TABLE1
GROUP BY ID,otherid

MySQL counting total number of zeros

I have a table that looks like
| id | day1 | day2 | day3 | day4 | day5 |
| 1 | 4 | 0 | 5 | 0 | 0 |
| 2 | 2 | 0 | 0 | 4 | 1 |
and I want to find to total number of zero entries for each id to give
| id | total_zeros |
| 1 | 3 |
| 2 | 2 |
SELECT id, (day1=0)+(day2=0)+(day3=0)+(day4=0)+(day5=0) total_zeroes
FROM table
Try this one:
select
id, if(day1=0,1,0)+if(day2=0,1,0)+ if(day3=0,1,0)+if(day4=0,1,0)+if(day5=0,1,0) as total
from test
DEMO HERE
Why do people insist on making such un-usable tables?
You will have to use a case statement, and evaluate each column individually, and then add up the results.

Select Distinct from multiple tables with order by

I'm stucked with a Mysql query, can you help me?
I have two tables:
user
id | name
1 | foo1
2 | foo2
3 | foo3
posts
id | id_user | created_at | kind
1 | 2 | 15-03-2011 | a
1 | 2 | 14-03-2011 | b
2 | 3 | 13-03-2011 | a
1 | 2 | 12-03-2011 | b
What I want is to retrieve the latest post of each user (the kind doesn't matter) ordered by de creation date.
How can I do that?
Thank you guys
One possible query is:
SELECT
u.id,
(SELECT MAX(p.created_at) FROM posts AS p WHERE u.id = p.id_user) AS latest
FROM
user AS u;
although the dependent subquery may not be the best solution to this. Example output:
users:
+------+------+
| id | name |
+------+------+
| 0 | test |
| 1 | one |
+------+------+
posts:
+------+---------+------------+------+
| id | id_user | created_at | kind |
+------+---------+------------+------+
| 0 | 0 | 2011-02-05 | a |
| 1 | 1 | 2011-02-06 | b |
| 2 | 0 | 2011-02-03 | a |
| 3 | 1 | 2011-02-02 | b |
+------+---------+------------+------+
output:
+------+------------+
| id | latest |
+------+------------+
| 0 | 2011-02-05 |
| 1 | 2011-02-06 |
+------+------------+
You can also add an ORDER BY latest DESC to the end of the query if you wish to get an ordered list of the latest posts across all user IDs.
Using a GROUP BY on id_user and the max post date ?
Something like that :
SELECT u.name, p.id_user, MAX( p.created_at )
FROM posts AS p
LEFT JOIN user AS u ON u.id
WHERE p.id_user = u.id
GROUP BY id_user