Closed. This question does not meet Stack Overflow guidelines. It is not currently accepting answers.
Questions asking for code must demonstrate a minimal understanding of the problem being solved. Include attempted solutions, why they didn't work, and the expected results. See also: Stack Overflow question checklist
Closed 9 years ago.
Improve this question
I have a table like this. In a String like this 01-10-33 I'd like to know how many times which number appears in my table. For example number 01 I wanna check if it is in first row, count and check line two,three...EOF and get the count number. In the example link I tried to check if the number was in the string without success. I know I could probably get all my numbers and do this in java, but is it possible to get this with sql?
The expected results in the example would be
numbers-count
01 - 1
02 - 2
03 - 2
05 - 1
10 - 2
12 - 1
If the numbers are always in that format you could use SUBSTRING to get all the parts out and count them:
select number, count(*)
from (
select substring(n, 1, 2) as number
from l
union all
select substring(n, 4, 2)
from l
union all
select substring(n, 7, 2)
from l
) a
group by number;
sqlfiddle demo
If you want to know how many times "01" appears in the string "01-10-33", you can use:
select l.*,
(length(n) - length(replace(n, #n, '')))/length(#n)
from l cross join
(select #n := '01') const;
Note that this does not handle delimiters. So, '01' will match '001'. If that requirement is important:
select l.*,
(length(n)+2 - length(replace(concat('-', n, '-'), #n, '')))/length(#n)
from l cross join
(select #n := '-01-') const;
Related
Closed. This question needs details or clarity. It is not currently accepting answers.
Want to improve this question? Add details and clarify the problem by editing this post.
Closed 5 years ago.
Improve this question
Sample Table
I want to select 6 random records from this table based off 2 columns, the topic and the taxonomy.The selection of records must be balanced and repetition of topics and taxonomy should be kept a minimum.Is there an algorithm to do this?
Any help in either SQL or java would be appreciated.Thanks in advance
You can do this with an nth sample on an ordered set. It is something like:
select t.*
from (select t.*, (#rn := #rn + 1) as rn
from t cross join
(select #rn := 0) params
order by topics, taxonomy
) t cross join
(select count(*) as cnt from t) tt
where rn % floor(cnt / 6) = 1;
The idea is to use modulo arithmetic to take every nth value to get to 6. You may have to fiddle with the exact parameters in the where, depending on the size of your data.
Closed. This question does not meet Stack Overflow guidelines. It is not currently accepting answers.
This question appears to be off-topic because it lacks sufficient information to diagnose the problem. Describe your problem in more detail or include a minimal example in the question itself.
Closed 8 years ago.
Improve this question
I have a loop that checks the biggest number so far and adds one to it; but for example 1 2 3 4 5 I press button 6 is created, what if the list was 1 2 3 5 6 and I want it to print 4 so I can get a correct number line then proceed to increase it by one?
EDIT: I should mention that my list has a possiblity not being in order so 2 1 3 5
You should check incrementally that the numbers are good:
So say you have an array of int called numbers[]
you could do:
int placehodler = numbers[0] + 1;
for (int i = 1; i < numbers.length(); i++){
if (placeholder != numbers[i]) // checks if the next number is equal to the previous + 1
break; // breaks out of the loop if it isn't
placeholder++; // increment placeholder by 1
}
System.out.println(placeholder); // prints placeholder
So if the placeholder breaks out of the loop early, it is your 1,2,3,5,6 situation and it would have 4 (succeeds on the loop where placeholder = 3, fails when placeholder = 4) in the 1,2,3,4,5 situation, it would succeed on all and end the loop with a value of 6.
First, I would find the count of your current list. If your list is [1 2 3 5] the count is 4.
Then, I would create a new list that is a range of numbers from 1 to the count => [1 2 3 4]
comparing the two lists will let you see if any numbers are missing, if no numbers are missing then you can add 1 to the current count and append that to the end of the list.
sorting the list before doing any of this will take care of the out of order issue mentioned in the other comment.
Closed. This question does not meet Stack Overflow guidelines. It is not currently accepting answers.
Questions asking for code must demonstrate a minimal understanding of the problem being solved. Include attempted solutions, why they didn't work, and the expected results. See also: Stack Overflow question checklist
Closed 9 years ago.
Improve this question
I am writing something for my friend that will involve storing some data in a database with a primary key which is an integer which will increment. For example:
id name Age
1 James 15
2 Max 24
3 Jordan 61
How would I retrieve the integer under the id column in the last row? And this would be with Java. Thanks.
If the "last" row is the one with the highest ID, your SQL (to retrieve just the ID, like you asked) will look like:
SELECT id FROM User ORDER BY id DESC LIMIT 1;
In Java, you'll probably be using JDBC; best practice is to learn how to use PreparedStatements.
is it like
id name age
1 james 15
2 max 24
3 jordan 61
PRIMARY KEY(id)
SELECT id
FROM `tablename`
ORDER BY id DESC
LIMIT 0 , 1
this will return the last row id result set from ur table
Closed. This question does not meet Stack Overflow guidelines. It is not currently accepting answers.
Questions asking for code must demonstrate a minimal understanding of the problem being solved. Include attempted solutions, why they didn't work, and the expected results. See also: Stack Overflow question checklist
Closed 9 years ago.
Improve this question
I would like to split my strings in JAVA based on a regular interval, not on regex. This is what I have to split:
1 x3.1.105.41 1 -10
2 x4.1.105.41 0 -10
3 x12.1.105.41 0 -10
4 y3.1.105.41.19 1 0
5 y4.1.105.41.21 0 0
6 y1.1.105.41.23 0 0
7 y12.1.105.41.25 0 0
I would like to seperate each column. Currently, I use the strLine.spli function
Any help would be great!
You can use substring:
String myLine = "1 x3.1.105.41 1 -10";
String column1 = myLine.substring(0, 2).trim();
String column2 = myLine.substring(2, 20).trim();
...
Or just split the lines:
String myLine = "1 x3.1.105.41 1 -10";
String[] columns = myLine.trim().split("\\s+");
which gives you in columns[0] your first value, in `columns[1]ยด your second and so on.
The second solution looks smarter to me.
Closed. This question does not meet Stack Overflow guidelines. It is not currently accepting answers.
Questions asking for code must demonstrate a minimal understanding of the problem being solved. Include attempted solutions, why they didn't work, and the expected results. See also: Stack Overflow question checklist
Closed 9 years ago.
Improve this question
The expression is: n mod m = x
I want to know the value of n given m and x
Is possible to get this value ? Is there is a java function to get that number?
It isn't possible to get this value. There are in fact multiple possibilities for a given m and x. For example, take n mod 3 = 1. We know that m is 3 and x is 1, but just knowing that, we don't know whether n is 4 or 7 or 10 or 13 or any other number that is one more than a multiple of three.
I don't think that can be done - look at a few examples
10 mod 9 = 1
19 mod 9 = 1
n could be 10, 19, 28 etc...
or
9 mod 8 = 1
17 mod 8 = 1
n could be 9, 17, 25 etc...