SELECTING with multiple WHERE range conditions on same column - mysql

I have tables like these:
A table B table
-------------------- -----------------------------------
item_ID | item_Name item_ID | option_ID | option_Value
-------------------- -----------------------------------
1 item_a 2 34 2000
2 item_b 2 45 3400
3 item_c 2 12 1200
4 item_d 3 34 500
5 item_e 3 13 500
6 item_f 4 45 700
I wrote a query to get items, for example which have option 34 = 2000 and option 12 = 1200 is:
SELECT A.item_ID, A.item_name
FROM A
LEFT JOIN B ON A.item_ID = B.item_ID
WHERE B.option_ID IN (34, 1200) AND
B.option_Value IN (1200, 2000) AND
GROUP BY A.item_ID
HAVING COUNT(A.item_ID) >= 2 /* count of option used for search, can be more*/
My problem is for some options I want to get range result , for example: where option id 34 is between 1000 and 2000 and option 12 is lower than 4000
Note : (option_id, option_value) pair is unique and i want to get items that match all of the conditions

How about this?
SELECT A.item_ID, A.item_name
FROM A
LEFT JOIN B ON A.item_ID = B.item_ID
WHERE (B.option_ID=34 AND B.option_value BETWEEN 1000 AND 2000)
OR (B.option_ID=12 AND B.option_value BETWEEN 0 AND 4000)
GROUP BY A.item_ID
HAVING COUNT(A.item_ID) >= 2
Maybe I didn't understand the question completely?

This should give you what you want:
WHERE
B.option_ID = 34 AND B.option_Value BETWEEN 1000 AND 2000
OR
B.option_ID = 12 AND B.option_Value < 4000
There might be a better way to do this if there was some rule according to which you want to filter... Otherwise, just use OR and AND to achieve what you want.

Related

MySQL - select avg of list but ignore max and min value

I have a list of values in my database.
k v
1 5000
1 100
1 120
1 3
2 5000
2 100
2 120
2 4
3 10000
3 120
3 100
3 4
4 10
4 120
4 110
4 5000
I want to calculate the average of each k but I need to ignore the highest and lowest value of v for each k. (to remove spikes)
select avg(v) from table where v > min(v) and v < max(v) group by k
results in an :
"Invalid use of group function"
I was thinking that this is a quite common task but I wasn't able to find any ideas from the docs.
Thanks for any advise.
One way to do this without worrying about whether there are duplicate min and max values of v (assuming you only want to ignore one of each) is to take the average as SUM(v)/COUNT(v), but subtracting the min and max values from the computation:
SELECT k, (SUM(v) - MAX(v) - MIN(v)) / (COUNT(v) - 2) AS average
FROM data
GROUP BY k
Output:
k average
1 110
2 110
3 110
4 115
Demo on dbfiddle
select avg(v) , k
from table
group by k
having k <> min (v) and k<> max (v)
First get the min and max v for each k and then left join the table to the results so to get the average of the non matching rows:
select
t.k, avg(t.v) average
from tablename t left join (
select k, min(v) minv, max(v) maxv
from tablename
group by k
) g on g.k = t.k and t.v in (g.minv, g.maxv)
where g.k is null
group by t.k
See the demo.
Results:
| k | average |
| --- | ------- |
| 1 | 110 |
| 2 | 110 |
| 3 | 110 |
| 4 | 115 |
Link: Demo
select t1.k, avg(t1.v) average
from numbers t1 left join (
select k, min(v) minv, max(v) maxv
from numbers
group by k
) t2 on t2.k = t1.k and t1.v in (t2.minv, t2.maxv)
where t2.k is null
group by t1.k

mysql- subtract values between rows where column values correspond

I have a table like:
Number | Event | Weight
1 4 150
1 4 160
2 5 200
2 4 200
3 6 190
3 6 195
For each row, I would like to subtract from its Weight, the Weight of another row where Number and Event matches (if exists). The desired output is:
Number | Event | Weight | DIFF
1 4 150 -10
1 4 160 10
2 5 200 NULL
2 4 200 NULL
3 6 190 -5
3 6 195 5
Is such an operation possible? Not sure if relevant, eventually I would need to turn this query into a view. Thanks in advance.
You need a left join:
select
t.*,
t.weight - tt.weight diff
from tablename t left join tablename tt
on tt.number = t.number and tt.event = t.event and tt.weight <> t.weight
It can be done by simply substracting the column in the joined table. When one of the operands is null, the result of the arithmetic operation is null:
select a.Number, a.Event, a.Weight, a.Weight - b.Weight as DIFF
from a
left join b on a.Number = b.Number and a.Event = b.Event

MySQL query find matching column based on another column?

Sorry I don't know how to word the question's title.
I have a table like this
Prod Part Number
0001 101 3
0001 102 2
0001 103 1
0002 101 3
0002 102 2
0002 103 4
0003 101 2
0003 102 3
0003 103 6
0004 101 3
0004 102 2
0004 103 1
I want to find the product that has the correct number per part for all parts.
So something like
SELECT Prod from table
WHERE (Number of Part(101)) = 3
AND (Number of Part(102)) = 2
AND (Number of Part(103)) = 1
Output would be:
Prod
0001
0004
How can I achieve that?
You could use some inner join
select t1.Prod from
( select Prod
from my_table
where part = 101
and Number = 3 ) t1
inner join
( select Prod
from my_table
where part = 102
and Number = 2 ) t2 on t1.Prod = t2.Prod
inner join
( select Prod
from my_table
where part = 103
and Number = 1 ) t3 on t1.Prod = t3.Prod
http://sqlfiddle.com/#!9/014fe4/8
Through conditional aggregation:
SELECT Prod,
SUM(CASE WHEN Part = 101 AND Number = 3 OR
Part = 102 AND Number = 2 OR
Part = 103 AND Number = 1
THEN 1 ELSE 0 END) sum
FROM tbl
GROUP BY Prod
HAVING sum = 3
This will give you an extra column. If it's a problem then wrap everything into another SELECT.

Get intersection of 2 tables in MySQL

Id MedId ShipId AvailableQuant DefaultQuant MinQuant MedExpiry LastUsed
------ ------ ------ -------------- ------------ -------- ------------------- ---------------------
1 1 2918 20 30 15 2015-02-05 11:37:24 2014-12-01 11:37:32
4 2 2918 50 55 30 2015-03-26 11:57:14 2014-12-03 11:57:22
5 3 2918 15 40 20 2014-12-10 16:58:58 2014-12-10 16:59:02
6 4 2918 30 75 30 2015-03-31 11:58:26 2014-12-03 11:58:32
7 5 2918 22 50 20 2015-01-01 11:59:05 2014-12-03 11:59:09
9 6 3095 5 35 10 2014-12-03 11:59:51 2014-09-01 11:59:55
10 7 2918 30 60 35 2014-12-01 12:00:43 2014-10-22 12:00:57
11 8 3095 25 30 20 2014-12-31 17:48:58 2014-12-01 17:49:12
And there are 2 queries that i have written
1)To give me count of critical Items
SELECT SUM(IF(m.AvailableQuant <= m.MinQuant,1, 0)) AS criticalFROM tbl_vesselmaster vs
INNER JOIN comp_login cl ON vs.co_id = cl.id
INNER JOIN m_shipinv m ON vs.id = m.ShipId
WHERE vs.co_id=$co_id;
2)To give me medicine that have excedeed expiry date
SELECT COUNT(MedId) as count FROM `m_shipinv` WHERE DATE(MedExpiry) < DATE(NOW());
i want a query to get the count intersection of this both query. means if the item is critical and its medicine is expired then it should count only 1
this should be the output
count
-------
4
I think you can just add the where clause to the first query:
SELECT SUM(m.AvailableQuant <= m.MinQuant) AS critical
FROM tbl_vesselmaster vs INNER JOIN
comp_login cl
ON vs.co_id = cl.id INNER JOIN
m_shipinv m
ON vs.id = m.ShipId
WHERE vs.co_id = $co_id OR DATE(MedExpiry) < DATE(NOW());
Note that I simplified the SUM() calculation. The if is not needed because MySQL treats booleans as integers in a numeric context.
Can't you just put the two conditions in the where clause ?
I don't understand all you inner joins as you only describe one table. I just altered the select of Gordon Linoff:
SELECT count(*) AS critical
FROM tbl_vesselmaster vs
INNER JOIN comp_login cl ON vs.co_id = cl.id
INNER JOIN m_shipinv m ON vs.id = m.ShipId
WHERE vs.co_id = $co_id
AND DATE(MedExpiry) < DATE(NOW())
AND m.availableQuant <= m.minQuant;
I think the result should be 3: only records 5, 9 and 10 meet both criteria.

SQL: how to select a single id that meets multiple criteria from multiple rows

On a MySQL database, I have the table below
package_content :
id | package_id | content_number | content_name | content_quality
1 99 11 Yellow 1
2 99 22 Red 5
3 101 11 Yellow 5
4 101 33 Green 5
5 101 44 Black 5
6 120 11 Yellow 5
7 120 55 White 5
8 135 66 Pink 5
9 135 99 Orange 5
10 135 11 Yellow 5
and i am looking a possibility to make search queries on it:
I would like to select the package_id where content_number could be 11 AND 22 (In this case it should select only package_id 99
I really don't know if it's possible in SQL since the statement AND will always results as false. If i use the statement OR i also get the package_id 99, 101, 120, 135 and that's not what i want.
Maybe my table is not well designed too, but any suggestions would help!
Thanks in advance
Edit
I added the content_quality column
I used the sql query from juergen, works very well
select package_id
from package_content
where content_number in (11,22)
group by package_id
having count(distinct content_number) = 2
My last question is how could i now add another criteria : Select the package_id where content_number is 11 and 22 and content_number 11 has content_quality 1
Edit 2:
For the 2nd question i use now this query. Thanks to both of you who helped me! :)
SELECT *
FROM (
SELECT package_id
FROM package_content
WHERE
(content_number=11 AND content_quality > 1)
OR (content_number = 33 AND content_quality = 5)
OR (content_number = 44 AND content_quality =5 AND content_name like 'Black')
GROUP BY package_id
HAVING count( DISTINCT content_number) = 3
)t1
LEFT JOIN package_content ON package_content.package_id = t1.package_id
This will output
id | package_id | content_number | content_name | content_quality
3 101 11 Yellow 5
4 101 33 Green 5
5 101 44 Black 5
You need to group by the package_id and then use having to perform an aggregate function over the grouped data
select package_id
from package_content
where content_number = 22
or
(
content_number = 11 and content_quality = 1
)
group by package_id
having count(distinct content_number) = 2
You could query with a self join for that:
SELECT DISTINCT package_id
FROM package_content a, package_content b
WHERE a.package_id = b.package_id
AND a.content_number = 11 AND b.content_number = 22
Edit: For your second question: Just add that to the query. The package_content renamed to a is responsible for the content_number 11. Therefore you can ask, wether a has content_quality 1:
SELECT DISTINCT package_id
FROM package_content a, package_content b
WHERE a.package_id = b.package_id
AND a.content_number = 11 AND b.content_number = 22
AND a.content_quality = 1