Selecting random balanced records based on two columns [closed] - java

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.

Related

Count the number of existing values in a field in a record [closed]

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 4 months ago.
Improve this question
Such as I have a table of User---(id,name,age,hobby,sports)
Then it has a record that (1,"zhangsan",18,null,null);
Next you can see the total of record is 3, because hobby and sports are null.
use SQL sentences to count the number of existing values in a field in a record
Help and thanks
Use CASE expressions to count each column.
SELECT
CASE WHEN Column1 IS NOT NULL THEN 1 ELSE 0 END
+ CASE WHEN Column2 IS NOT NULL THEN 1 ELSE 0 END
-- repeat for all columns
AS NumberOfNonNullColumns
FROM MyTable

How to write a query for this two tables [closed]

Closed. This question needs to be more focused. It is not currently accepting answers.
Want to improve this question? Update the question so it focuses on one problem only by editing this post.
Closed 2 years ago.
Improve this question
EMPLOYEE(id,emp_name)
1, John
2, Michael
3, Philip
===================
RANK(id,emp_id,date,rank)
1,1,1583413078445,SE
2,1,1583413087085,SSE
3,1,1583413092169,PM
4,2,1583413131802,SE
5,3,1583413536655,SE
I want to show
employee id, employee name and latest Rank (whose id is maximum for emp_id in rank table or whose longdate is maximum for emp_id in rank table) from Rank table
id,name,Rank
1, John, PM
2, Michael, SE
3, Philip, SE
I am using java and my database is mysql and sqlite (in case offline)
I tried some way but could not aggregate the queries.
SELECT RK.emp_id, EP.emp_name, RK.rank
FROM RANK RK
LEFT JOIN employee EP ON RK.emp_id=EP.id
GROUP BY RK.emp_id
HAVING MAX(RK.id);
What you need is some techniques on analytic functions.
with RANK_EXPAND as (
select
id,emp_id,date,Rank,
max(date) over (partition by emp_id) as maxDate,
max(id) over (partition by emp_id, date) as maxId,
from RANK
),
select
r.id,
e.emp_name as name,
r.Rank
from
EMPLOYEE e inner join RANK_EXPAND r on e.id=r.emp_id
where
r.date=r.maxDate and r.id=r.maxId
You may also use purely group by + rejoining, but that will make the query much longer.
I agree with the solution of Aditya as the best solution (I would comment the answer, but I don't have necessary reputation).
I just want to point out another possible solution for this using a sub-query in the projection-clause.
SELECT EP.emp_id, EP.emp_name,
(SELECT RK.Rank FROM Rank RK WHERE RK.emp_id=EP.emp_id ORDER BY RK.id DESC LIMIT 1) as Rank
FROM employee EP
;

10 random words without repetition [closed]

Closed. This question needs to be more focused. It is not currently accepting answers.
Want to improve this question? Update the question so it focuses on one problem only by editing this post.
Closed 5 years ago.
Improve this question
I have 20 words in sql database in android
I want to choose 10 random words without repeating and put it in 10 textview if activity is starting
I find this code but it's for numbers only
ArrayList<Integer> number = new
ArrayList <Integer>();
for (int i = 1; i <= 10; ++i)
number.add(i);
Collections.shuffle(number);
If they are in a SQL database, you can use the query:
select word
from t
order by rand()
limit 10;
With 20 words, this should have quite reasonable performance. But if the number of words grows, performance could be an issue.
If you've got XQuery 3.1 you can do
random-number-generator()?permute($words)[position()=1 to 10]
Try this
SELECT TOP 20 WordField
FROM TableName
ORDER By rand()

How to find number of occurrence of a specific value in table column which contain pipe delimiter in MySQL query? [closed]

Closed. This question needs to be more focused. It is not currently accepting answers.
Want to improve this question? Update the question so it focuses on one problem only by editing this post.
Closed 5 years ago.
Improve this question
How to find number of occurrence of a specific value in table column which contain pipe delimiter in MySQL query?
Please refer the below string:
0|5573|73|5573|73
I want the count of 73 in above string it should give answer as 2.
-----------------------------
column -- count
------------------------------
0|5573|73|5573|73 -- 2
Please refer below mysql query which will return no of occurence of specific value as per your question.
In below query test_table is a table which contain the column named 'test_colum' which contains values like '0|5573|73|5573|73' and you want to find the no of occurrences of value '73' in it.
SELECT
test_colum,
(IF((POSITION('73|' IN test_colum))=1,1,0))
+
( ROUND (
(
(LENGTH(test_colum)
- LENGTH( REPLACE ( test_colum, "|73|", ""))
)
) / LENGTH("|73|")
)
+
IF((substring_index(test_colum,'|',-1) = '73'),1,0)) AS value_cnt
FROM test_table;
It will produce result as following :
test_colum value_cnt
0|5573|73|5573|73 2

what data type returns a join query like this in java? [closed]

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 8 years ago.
Improve this question
I do not know what kind of data it returns this query and how to retrieve/use them
Query q1 = em.createNativeQuery("SELECT t.idb, c.value FROM trans t join com c On t.id = c.idTrans where c.date >= '2014-05-01' AND c.date <= '2014-05-31' AND currency = 'euro' ORDER BY t.idb");
return q1.getResultList();
It depends on if you use getResultList or getSingleResult. If you expect multiple rows to match the query, you would use getResultList and have a List containing an Object[] representing each row, with values matching the type returned from the driver for t.idb and c.value.
If you use getSingleResult, you would get an Object[] representing the only row that should have been returned, or a NonUniqueResultException if more than one row was returned from the query.
If I understand your question, I think you want to call getResultList() -
final List<Object> data = (List<Object>) query.getResultList();

Categories

Resources