I have a column fruits and it has rows like
banana
pineapple
orange
grapes
apple
mango
pomegranate
Kiwi
grapefruit
peach
or maybe like this
pineapple
grapefruit
orange
grapes
apple
mango
pomegranate
Kiwi
banana
peach
I want to retrieve all that with grapefruit in the middle all the time like following no matter whether it has even or odd number of rows
banana
pineapple
orange
grapes
grapefruit
apple
pomegranate
Kiwi
mango
I know basic SQL query SELECT fruit FROM FRUITTABLE
but dont know further
You can order by is_even/is_odd on some enumeration. (here: the difference in row_number() over nothing)
\i tmp.sql
CREATE TABLE fruits(fruit text);
INSERT INTO fruits(fruit) VALUES
('banana') ,('pineapple') ,('orange') ,('grapes') ,('apple') ,('mango') ,('pomegranate') ,('Kiwi') ,('grapefruit') ,('peach')
;
with numbered AS (
select fruit, row_number() OVER () rn
FROM fruits
)
, gnum AS (
SELECT rn FROM numbered
WHERE fruit = 'grapefruit'
)
SELECT n.fruit, n.rn
FROM numbered n
JOIN gnum g ON true
ORDER BY ((n.rn - g.rn) %2), (n.rn <> g.rn)
;
Result:
CREATE TABLE
INSERT 0 10
fruit | rn
-------------+----
mango | 6
grapes | 4
Kiwi | 8
pineapple | 2
grapefruit | 9
banana | 1
orange | 3
apple | 5
pomegranate | 7
peach | 10
(10 rows)
Edit: the tie-breaker is not always correct (caused by modulo on negative numbers) A better order would be
ORDER BY (ABS(n.rn - g.rn) %2) , (n.rn <> g.rn) DESC
I think you should explain why you want to do this, maybe there is a better way to do obtain the result you want.
But I think that you could add a weight col in your table
and order by that value in the select.
Just take into consideration that whenever you add a new row you have to update that weights in order to maintain grapefruit in the middle.
And you have to define how to manage the pair numbers of rows.
Table with examples
SELECT *
FROM Fruits
ORDER BY Weight;
Could you do:
(select * from fruit where id < (select max(id)/2 from fruit))
union select * from fruit where id = grapefruit
union
(select * from fruit where id > (select max(id)/2 from fruit))
Or something like that. If you don't have an id you might have to use rownumber, but it should be doable.
Related
I have data like this
id otherid name
1 123 banana
2 123 banana
3 123 banana
4 456 grape
5 456 grape
6 789 orange
7 111 banana
How can I get output like this: (with MySQL query)
name count
banana 2
grape 1
orange 1
Try this:
SELECT
f.`name`,
COUNT(DISTINCT (f.`otherid`))
FROM
`fruits` f
GROUP BY f.`name`
You can use COUNT with GROUP BY:
SELECT
`name`, COUNT(*)
FROM
write_you_table_name
GROUP BY
`name`,`otherid`
Using distinct in the count function is recommended. But if you prefer not to using it, try this:
select name,count(*) from
(select name from fruit group by name,otherid) t
group by name;
SELECT name , count(*) as count FROM tb_stock GROUP BY other_id
for example this is my data
info
----
id name eating andSaying
--- ---- ------ ---------
1 aman mango with sister amanzing mango
2 ramesh mango via mouth wow its really good
3 aman guava with jimmy yoyo
4 ramesh pineapple yummy
Now i want a single query where i can fetch those rows who is having common eating value (table name eating) for example where name is aman and ramesh who is having same eating then show the rows
info
-----
id name eating andSaying
--- ---- ------ ---------
1 aman mango with sister amanzing mango
2 ramesh mango via mouth wow its really good
i have tried this query but it is NOT Working
SELECT *
FROM info a1
JOIN info a2
ON a1.name!= a2.name
AND a1.eating (get first array before "space"mango ) = a2.eating(get first array before "space" mango )
How to get the two rows if eating on first letter is same?
it should reflect two rows like this
info
-----
id name eating andSaying
--- ---- ------ ---------
1 aman mango with sister amanzing mango
2 ramesh mango via mouth wow its really good
To get the eating values related to more than one name values you can use:
SELECT eating
FROM info
GROUP BY eating
HAVING COUNT(DISTINCT name) > 1
Output:
eating
------
mango
Use the above query as a derived table and join back to the info table to get the expected result set:
SELECT i1.*
FROM info AS i1
JOIN (
SELECT eating
FROM info
GROUP BY eating
HAVING COUNT(DISTINCT name) > 1
) AS i2 ON i1.eating = i2.eating
Edit:
To get matches depending on the first word of eating field, instead of:
GROUP BY eating
use:
GROUP BY SUBSTRING_INDEX(`eating`, ' ', 1)
So the whole query could look like this:
SELECT i1.*
FROM info AS i1
JOIN (
SELECT SUBSTRING_INDEX(`eating`, ' ', 1) AS eating
FROM info
GROUP BY SUBSTRING_INDEX(`eating`, ' ', 1)
HAVING COUNT(DISTINCT `name`) > 1
) AS i2 ON i1.eating LIKE CONCAT(i2.eating, '%');
Demo here
I've stripped out the irrelevant parts of the problem...
DROP TABLE IF EXISTS my_table;
CREATE TABLE my_table
(name VARCHAR(12) NOT NULL
,eating VARCHAR(12) NOT NULL
,PRIMARY KEY(name,eating)
);
INSERT INTO my_table VALUES
('aman','mango'),
('ramesh','mango'),
('aman','guava'),
('ramesh','pineapple');
SELECT DISTINCT x.*
FROM my_table x
JOIN my_table y
ON y.name <> x.name
AND y.eating = x.eating;
+--------+--------+
| name | eating |
+--------+--------+
| ramesh | mango |
| aman | mango |
+--------+--------+
I need to create a new view using MySQL that concatenates all names if they have the same id;
Ex: Table: sample_table
profile_id food
---------- ----
1 Apple
2 Banana
2 Orange
3 Lemon
3 Potato
3 Grapes
Should be
vw_sample_view
profile_id food
---------- ----
1 Apple
2 Banana, Orange
3 Lemon, Potato, Grapes
You can use mysql's group_concat() function to achive the desired outcome:
select profile_id, group_concat(food)
from yourtable
group by profile_id
All you need to do is to include the above query into a create view command.
I have been searching in Google and SO for an answer to this but having a hard time. Not even sure if I am asking it correctly, but I am going to give it my best shot here:
My data:
Fruit | Attributes
-------------------
Apple | Dark Red
Apple | Green
Apple | Yellow
Apple | Light Red
Apple | Greenish Yellow
Apple | Dark Yellow
Apple | Brown
Banana | Yellow
Banana | Greenish Yellow
Banana | Dark Yellow
Banana | Yellow
Banana | Brown
Banana | Red
Banana | Black
What I would like to do is run a query that outputs how many attributes for all fruits (in this case just apples and bananas) that overlap.
Hope I've been clear... please let me know if I need to clarify.
Something like this?
SELECT DISTINCT ATTRIBUTE, FRUIT
FROM FRUIT_TABLE
WHERE ATTRIBUTE IN (
SELECT ATTRIBUTE
FROM FRUIT_TABLE
GROUP BY ATTRIBUTE
HAVING COUNT(DISTINCT FRUIT_NAME) > 1 )
ORDER BY ATTRIBUTE, FRUIT_NAME
I tried it with City and State in a similar way and it worked:
SELECT DISTINCT CITY, STATE
FROM ADDRESS
WHERE CITY IN (
SELECT CITY
FROM ADDRESS
GROUP BY CITY
HAVING COUNT(DISTINCT STATE) > 1)
ORDER BY CITY, STATE
BATH NB
BATH ON
BEDFORD NS
BEDFORD QC
BRANDON MB
BRANDON MT
Brampton NB
Brampton ON
... and so on
SELECT ATTRIBUTE, COUNT(*) FROM (
SELECT DISTINCT ATTRIBUTE, FRUIT
FROM FRUIT_TABLE
WHERE ATTRIBUTE IN (
SELECT ATTRIBUTE
FROM FRUIT_TABLE
GROUP BY ATTRIBUTE
HAVING COUNT(DISTINCT FRUIT_NAME) > 1 )
ORDER BY ATTRIBUTE, FRUIT_NAME ) AS RESULTS
GROUP BY ATTRIBUTE
I have tried to implement the ideas from this post to order my output by the occurrences of each pair -
MySQL: Count occurrences of distinct values
What I need is to be able to take into consideration two different columns, not just "name". So what would be the most efficient way to order the output of the following table -
id name food
----- ------ ------
1 Mark apple
2 Mike pear
3 Paul apple
4 Mike pear
5 Mike banana
6 John apple
7 Mark pear
The expected order should be:
name food count
----- ------ -----
Mike pear 2
Mike banana 1
Mark apple 1
Mark pear 1
Paul apple 1
John apple 1
You've got to group by both columns if you want to count the same combinations in those columns:
SELECT name, food, COUNT(*) count
FROM yourtable
GROUP BY name, food
ORDER BY COUNT(*) DESC