I currently have the code below and it works for getting me the 2 smallest number, but I want to get all of the 2nd smallest numbers and link them to their name as opposed to just one of them. lets say the numbers in the tables was made up of this:
Name| number
----|------
w 2
a 8
s 2
e 2
z 3
I would want to get
w 2
s 2
e 2
and now I am just getting w 2
SELECT MAX(col) FROM table WHERE col NOT IN (SELECT MAX(col) FROM table);
If this code gets you the second smallest number (what you want):
SELECT MAX(col) FROM table WHERE col NOT IN (SELECT MAX(col) FROM table);
Then simply do:
select *
from table
where col = (SELECT MAX(col) FROM table WHERE col NOT IN (SELECT MAX(col) FROM table));
I didn't understand well, but if you're using LIMIT 1,1, you will only get 1 row or none.
Just use what #491243 commented on your question.
SELECT * FROM tablename WHERE number = (SELECT MIN(number) FROM tableName);
Forget it, now I understood the question.
Try this:
SELECT * FROM tablename WHERE number =
(SELECT number FROM tablename WHERE number !=
(SELECT MIN(number) FROM tablename) ORDER BY number LIMIT 1);
Hope this helps.
EDIT: Using the SQLFiddle table:
SELECT * FROM ships WHERE gunsize = (
SELECT gunsize FROM ships WHERE gunsize !=
(SELECT MIN(gunsize) FROM ships) ORDER BY gunsize LIMIT 1);
http://sqlfiddle.com/#!2/9ca94/11
SELECT name,(SELECT MAX(gunsize) FROM ships s2
WHERE s2.Name=s.name and gunsize not in (select max(gunsize) from ships))as gunsizes
FROM ships s
Something like this?
Related
I have the following table:
SequenceNumber are always a multiple of 10. I would like to get, for a specific cpId, the smallest free sequence number (still in a multiple of 10). For example, for cpId = 1, the smallest available should be 20. For cpId = 2, it should be 10.
I have the following statement to get the smallest available sequenceNumber for all cpId, and I don't know how I can add a WHERE cpId = x inside the statement:
SELECT MIN(t1.sequenceNumber + 10) AS nextID
FROM LogicalConnection t1
LEFT JOIN LogicalConnection t2
ON t1.sequenceNumber + 10 = t2.sequenceNumber
WHERE t2.sequenceNumber IS NULL;
DB fiddle: https://www.db-fiddle.com/f/ag67AkFzfwPZEva8bTN7Q3/2#&togetherjs=L9nHb3Uu7O
Thank you for your help!
You can use lead() to get the next number and then some simple logic:
select cpid,
(case when min_sn > 10 then 10
else min(sequenceNumber) + 10
end)
from (select t.*,
min(sequenceNumber) over (partition by cpid) as min_sn,
lead(sequenceNumber) over (partition by cpid order by sequenceNumber) as next_sn
from t
) t
where next_sn is null or next_sn <> sequenceNumber + 10
group by cpid, min_sn;
You have to join both tables on cpId column and group the rows with similar cpId.
Where can be used to filter rows.
Below query gives you the cpId and their corresponding next available minimum sequence number.
SELECT t1.cpId, MIN(t1.sequenceNumber + 10) AS nextID
FROM LogicalConnection t1
LEFT JOIN LogicalConnection t2
ON t1.sequenceNumber + 10 = t2.sequenceNumber
and t1.cpId = t2.cpId
group by (t1.cpId)
I have a table with the following structure:
id name
1 X
1 X
1 Y
2 A
2 A
2 B
Basically what I am trying to do is to write a query that returns X for 1 because X has repeated more than Y (2 times) and returns A for 2. So if a value occurs more than the other one my query should return that. Sorry if the title is confusing but I could not find a better explanation. This is what I have tried so far:
SELECT MAX(counted) FROM(
SELECT COUNT(B) AS counted
FROM table
GROUP BY A
) AS counts;
The problem is that my query should return the actual value other than the count of it.
Thanks
This should work:
SELECT count(B) as occurrence, A, B
FROM table
GROUP BY B
ORDER BY occurrence DESC
LIMIT 1;
Please check: http://sqlfiddle.com/#!9/dfa09/3
You can try like this using a GROUP BY clause. See a Demo Here
select *, max(occurence) as Maximum_Occurence from
(
select B, count(B) as occurence
from table1
group by B
) xxx
This is how I finally handled my problem. Not the most efficient way but get the job done:
select A,B from
(select A,B, max(cnt) from
(select A ,B ,count(B) as cnt
from myTable
group by A,B
order by cnt desc
) as x group by A
) as xx
I have a table having id and no field, what I really want is the result raw will be repeated no filed times, if the no field is 2 then that raw must be repeated twice in result.
this is my sample table structure:
id no
1 3
2 2
3 1
now I need to get a result like:
1 3
1 3
1 3
2 2
2 2
3 1
I tried to write mysql query to get the result like above, but failed.
You need a table of numbers to accomplish this. For just three values, this is easy:
select t.id, t.no
from t join
(select 1 as n union all select 2 union all select 3
) n
on t.no <= n.no;
This query must do what you want to achieve:
select t.id, t.no from test t cross join test y where t.id>=y.id
not completely solve your problem, but this one can help
set #i=0;
select
test_table.*
from
test_table
join
(select
#i:=#i+1 as i
from
any_table_with_number_of_rows_greater_than_max_no_of_test_table
where
#i < (select max(no) from test_table)) tmp on no >= i
order by
id desc
EDIT :
This is on SQL Server. I checked online and see that CTEs work on MySQL too. Just couldn't get them to work on SQLFiddle
Try this, remove unwanted columns
create table #temp (id int, no int)
insert into #temp values (1, 2),(2, 3),(3, 5)
select * from #temp
;with cte as
(
select id, no, no-1 nom from #temp
union all
select c.id, c.no, c.nom-1 from cte c inner join #temp t on t.id = c.id and c.nom < t.no and c.nom > 0
)
select * from cte order by 1
drop table #temp
Pretty much as the title says, that was the simplest way I could explain it. To elaborate...
I first need to find the value of column c that has been duplicated the most times (mostDuplicated), and then SELECT * FROM t WHERE c=mostDuplicated
To go on about it further...
Here's my data:
SELECT * FROM t
a, b, c
- - -
1, 1, 1
2, 2, 1
3, 3, 1
4, 4, 2
5, 5, 3
So ignore the values in columns a & b completely, just concentrate on column c. I need to find the most duplicated value in column c (which is 1), and then SELECT only these records WHERE c=1. I want to do this in a single query if possible.
Do a "group by" query to count the number of unique values of c, order it descending and select only the top row. Then use the output as a subquery to select rows with that particular value of c:
SELECT * FROM t WHERE c = (SELECT c FROM t GROUP BY c ORDER BY COUNT(*) DESC LIMIT 1)
SELECT c FROM t GROUP BY c ORDER BY count(*) DESC LIMIT 1
Well it will be, like this:
SELECT * FROM t WHERE c =
(SELECT c FROM
(SELECT c, count(c) as co
FROM t ORDER BY co DESC LIMIT 1))
Hope this help
Here you go, it's a bit convoluted:
SELECT
*
FROM
t
WHERE
(
c IN
(
SELECT c
FROM (
SELECT
c,
COUNT(c) as freq
FROM
t
GROUP BY
c
ORDER BY
freq DESC,
c ASC
LIMIT 1
) AS t2
)
)
Basically, it's going this:
1. determine how often each value of C is repeated
2. select the value of the MAXimum repeats
3. use that value to determine what value of C to use when select * from the entire table.
I need to a MySQL query that returns the results of a table where the elements of a specific column are identical in at least 'n' records.
example pseudocode:
SELECT * FROM Table WHERE col has at least 3 identical
select * from table where col in (
select col from table group by col having count(*) > 3
)
SELECT col, COUNT(*) AS total FROM tbl GROUP BY col HAVING total >= 3
select * from table a
inner join (
select col,n=count(*)
from table
group by col
having count(*) >= 3 ) as b
on (a.col=b.col)
mysql allows you to write something like the following:
select *
from table
group by col
having count(*) >= 3
other dbmss force you to specify all columns explicitly and use MAX or MIN on them
SELECT DISTINCT * FROM TableName t WHERE 2 < (SELECT COUNT(*) FROM TableName p WHERE p.col = t.col)