I have a table like the following
table_fruit:
account_id fruit
1 apple
1 banana
2 apple
2 orange
2 banana
3 apple
I'm trying to make a list of everyone that has an apple and no other fruit
This seems like an easy idea but its passing me right now...
You can use not exists as
select * from table_fruit t1
where t1.fruit = 'apple'
and not exists(
select 1 from table_fruit t2
where t2.account_id = t1.account_id
and t2.fruit <> 'apple'
);
try this:
Select * From table_fruit Where fruit = 'apple'
or
Select * From table_fruit Where fruit like '%apple%'
Related
I have a table with values look like this.
Id Name Fruit
------------
1 Jon Apple
------------
2 Jon Orange
------------
3 Jon Grape
------------
4 Mike Apple
------------
5 Mike Orange
-------------
How to distinct the column into something like this in mysql?
Name Fruit
----------
Jon Apple
Orange
Grape
-----------
Mike Apple
Orange
-----------
This should do
SELECT name, GROUP_CONCAT(fruit SEPARATOR '\n') FROM your_table GROUP BY name
Demo in db<>fiddle
Update to add numbering:
SELECT name ,
GROUP_CONCAT(CONCAT (rn,')',fruit) SEPARATOR '\n')
FROM (
SELECT *
,ROW_NUMBER() OVER (PARTITION BY name) AS rn
FROM your_table
) SQ
GROUP BY name
Demo with numbering in db<>fiddle
Another option is also using joins to achieve this requirement.
select
case when t1.id = t2.id then t1.name else '' end
, t3.fruits
from
(select min(id) id, name from testA
group by name) t1
left join
testA t2 on t2.name = t1.name
left join
testA t3 on t3.id = t2.id
order by t2.id asc
see dbfiddle.
FX., Given tables for baskets and fruits, looking for a query that returns the basket_id that contains ONLY the two specific fruit.
EX
table: fruits
basket_id | fruit
_________________
1 | apple
1 | orange
1 | pear
2 | apple
2 | orange
3 | apple
3 | pear
Given the above table, I'm looking for a query that returns the basket_id that ONLY contains the fruits "apple" and "orange", which would be row 2 only. Basket id 1 should be excluded because it also contains "pear".
Help with this would be most appreciated.
Please note for the example table, I added one more basket which is basket_id = 4, that has two apples only, and it will be a strong test sample for the query.
WITH ABC --sample table
AS
(
SELECT 1 as basket_id, 'Apple' as fruit
UNION ALL
SELECT 1 as basket_id, 'Orange' as fruit
UNION ALL
SELECT 1 as basket_id, 'Pear' as fruit
UNION ALL
SELECT 2 as basket_id, 'Apple' as fruit
UNION ALL
SELECT 2 as basket_id, 'Orange' as fruit
UNION ALL
SELECT 3 as basket_id, 'Apple' as fruit
UNION ALL
SELECT 3 as basket_id, 'Pear' as fruit
UNION ALL
SELECT 4 as basket_id, 'Apple' as fruit
UNION ALL
SELECT 4 as basket_id, 'Apple' as fruit
)
--main query:
SELECT basket_id FROM
(
SELECT *,CASE WHEN fruit in ('Apple','Orange') THEN 1 ELSE 0 END AS row_check
FROM ABC
) as A
GROUP BY basket_id
HAVING COUNT(DISTINCT row_check) =1 -- make sure there is no other fruit
AND COUNT(DISTINCT fruit) >1 -- make sure there are at least one apple and one orange
output: basket_id 2
select
f.basket_id,
(select count(1) from fruits where basket_id = f.basket_id) as fruits_in_basket,
(select count(1) from fruits where basket_id = f.basket_id and fruit in ("apple", "orange")) as specific_fruits_in_basket
from
fruits as f
group by f.basket_id
having fruits_in_basket = specific_fruits_in_basket
Here is a fiddle: http://sqlfiddle.com/#!9/f4ae3c/37/0
SELECT DISTINCT basket_id
FROM fruits f
WHERE
NOT EXISTS (SELECT 1 FROM fruits f1 WHERE f.basket_id=f1.basket_id AND f1.fruit NOT IN ('apple', 'orange'))
AND
(SELECT count(*) FROM fruits f2 WHERE f.basket_id=f2.basket_id AND f2.fruit='apple')=1
AND
(SELECT count(*) FROM fruits f3 WHERE f.basket_id=f3.basket_id AND f3.fruit='orange')=1;
I have tested the query on your example. Demo
If it is possible to use the list of items as an ordered list you want to select like 'apple,orange', then you can use following.
SELECT f.basket_id, f.fruit
FROM fruits f
INNER JOIN (
SELECT basket_id, GROUP_CONCAT(fruit order by fruit) as a
FROM fruits
GROUP BY basket_id
HAVING a = 'apple,orange'
) f1 ON f.basket_id = f1.basket_id
Here is the fiddle.
SELECT DISTINCT(basket_id)
FROM basket
where fruit IN ('apple', 'orange');
I have a table with this data in it:
ID Name Color
1 Kyle Blue
1 Susan Orange
1 Steven Orange
2 Susan Blue
I want to use a query like this:
Select * from table group by ID, Top1(Name), Top1(Color)
So I get these results:
ID Name Color
1 Kyle Blue
2 Susan Blue
I don't care if it's Kyle Blue, or Steven Orange as long as the color matches the name.
Eg
SELECT x.*
FROM my_table x
JOIN (SELECT id, MIN(name) min_name FROM my_table GROUP BY id) y
ON y.id = x.id
AND y.min_name = x.name;
select *
from (
select *,
row_number() over (partition by Name order by Name) as row_number
from table
) as rows
where row_number = 1
I have this table:
id item_name_1 quantity 1 item_name_2 quantity_2 item_name_3 quantity_3
-----------------------------------------------------------------------------------
1 Apple 2 Pear 3 Orange 5
2 Pear 1 Apple 4
3 Orange 6
4 Apple 1 Pear 2 Orange 3
I want this result:
item total
--------------
Apple 7
Pear 6
Orange 14
I tried this:
SELECT
(SELECT item_name_1, SUM(quantity_1) AS count FROM table1
GROUP BY item_name_1) AS item,
(SELECT item_name_2, SUM(quantity_2) AS count FROM table1
GROUP BY item_name_2) AS item,
(SELECT item_name_3, SUM(quantity_3) AS count FROM table1
GROUP BY item_name_3) AS item,
SUM(count) AS total
FROM table1
GROUP BY item;
Error Code: 1241. Operand should contain 1 column(s)
Any suggestions?
You're selecting two columns in your sub-queries when there can only be one column.
Something like this will get you what you are looking for:
SELECT item_name, SUM(quantity) AS quantity
FROM
(SELECT item_name_1 AS item_name, SUM(quantity_1) AS quantity
FROM table1
GROUP BY item_name_1
UNION ALL
SELECT item_name_2 AS item_name, SUM(quantity_2) AS quantity
FROM table1
GROUP BY item_name_2
UNION ALL
SELECT item_name_3 AS item_name, SUM(quantity_3) AS quantity
FROM table1
GROUP BY item_name_3) AS ABB1
GROUP BY item_name;
I would try it like this.
select t.item_name_1, s1+s2+s3
from
(
select * From
(
select sum(quantity_1) as s1, item_name_1
from
table1
group by item_name_1
) t1
full outer join
(
select sum(quantity_2) as s2, item_name_2
from
table1
group by item_name_2
) t2 on t1.item_name_1 = t2.item_name_2
full outer join
(
select sum(quantity_3) as s3, item_name_3
from
table1
group by item_name_3
) t3 on t2.item_name_2 = t3.item_name_3
) t
I am trying to develope a query to fetch the rows having duplicate values, i need to fetch both records i.e duplicating record and the real one, for example
table
id keyword
-------------------
1 Apple
2 Orange
3 Apple
4 Grape
5 Banana
6 Grape
The query result should be:
id keyword
-------------------
1 Apple
3 Apple
4 Grape
6 Grape
Please anyone help me!
Query:
select * from
table where keyword in
(select keyword
from table
group by keyword
having count(keyword)>1)
One way to do it:
SELECT *
FROM `table` t1
WHERE
(SELECT COUNT(*) FROM `table` t2 WHERE t2.keyword = t1.keyword) > 1
And another way:
SELECT t1.*
FROM `table` t1
JOIN `table` t2 ON t1.keyword = t2.keyword
WHERE t1.id != t2.id
This might help:
SELECT t1.id, t1.keyword
FROM table t1
INNER JOIN table t2
ON t1.id != t2.id
AND t1.keyword=t2.keyword
Tested on SQL Fiddle
http://sqlfiddle.com/#!2/44dbb/1/0