Sort by Amount of True Booleans? - mysql

I tried to find this online, but I can't seem to find anything. How would I check for example, for the amount of integers (which = 1) and then sort the rows from most to least?
For example, these three booleans.
INT_ONE, INT_TWO, INT_THRE
Thank you :)

Add the columns together, and sort on that:
ORDER BY (INT_ONE + INT_TWO + INT_THREE) DESC
If you also need to use the value:
SELECT
(INT_ONE + INT_TWO + INT_THREE) AS num_true
FROM tbl
ORDER BY num_true DESC
This works because booleans in MySQL are 0 or 1.

Related

MySQL ORDER BY CASE + operator

I am trying to do a search that would be sorted by relevance.
Let's say the search term contains 3 words: A, B and C. What I am trying to do is to check if the search term is present in the SELECT result and if yes that would increase its rank.
ORDER BY CASE
(
WHEN search_word_A_is_present THEN +1
WHEN search_word_B_is_present THEN +1
WHEN search_word_C_is_present THEN +1
ELSE 0
END
)
DESC
While there is no syntax error and the search runs and sorts by something (that seems different from what I want) but I am not sure what is being added up if anything. How would I go about seeing what the final rank (sum) is at the end for each result? Is this the correct way to do it?
Since in MySQL boolean conditions result in 1 and 0, you can simply add those up
ORDER BY search_word_A_is_present + search_word_B_is_present + search_word_C_is_present
DESC
A more practical example:
ORDER BY col1 = 1 + col2 = 'A' + col3 = 44 DESC

SQL How to sort by "Ranges"

I am currently using a database with different entries like dates, names, but also one column with time "ranges". This basically means that there can be a definite number like "10" in this cell, but also a value like "10-15" or "5-10".
So what I want to do here is to sort them by an "average" value ((Lowest+Highest)/2). So in case of the 3 mentioned values it should be
5-10
10
10-15
I am wondering if it is possible to embed this into the SQL statement in some way.
And if it is not possible, I'd like to know the easiest way to implement it otherwise.
Right now I am putting the $SQL_statement together via several conditions, then putting everything into $resultset which is then used with "while". Here are some snippets:
$resultset=mysql_query($SQL_statement);
while ($currententry=mysql_fetch_array($resultset))
{
echo $currententry['Platform'];
echo $currententry['PlaytimeInH']."h";
}
You can do this with substring_index() and arithmetic:
order by (substring_index(col, '-', 1) + 0 +
substring_index(col, '-', -1) + 0
) / 2
The division by 2 is unnecessary, but you do specify the average in your question.
Note that the above will work even if col has no hyphen in it.
You could use a select with a case when clause in order by
select col1, col2, cole
from your_table
order by case
when your_column = '5-10' then 1
when your_column = '10-15' then 2
when your_column = '15-20' then 3
else 4
end

MySQL sorting with alphanumeric prefix

I've got a database with a column that contains the following data:
aaa-1
aaa-2
aaa-3
...
aaa-10
aaa-11
...
aaa-100
aaa-101
...
aaa-1000
When I query and sort the data in ascending order, I get:
aaa-1
aaa-10
aaa-11
...
aaa-100
aaa-101
...
aaa-1000
...
aaa-2
...
aaa-3
Is this actually the correct (machine) way of sorting? Is the order being screwed up because of the aaa- prefix? How do I go about sorting this the way a human would (ie something that looks like the first snippet)?
P.S. If the problem does lie in the prefix, is there a way to remove it and sort with just the numeric component?
P.P.S. It's been suggested to me that I should just change my data and add leading zeroes like aaa-0001 and aaa-0002, etc. However, I'm loathe to go that method as each time the list goes up an order of 10, I'd have to reformat this column.
Thank you all in advance! :)
You can extract the number part, convert it to numeric data type and then do an ORDER BY:
SELECT mytable.*,
CAST(SUBSTRING_INDEX(mycolumn, '-', - 1) AS UNSIGNED) mycolumnintdata
FROM
mytable
ORDER BY mycolumnintdata;
If there are expressions which does not match number, the CAST function would return 0 and those records would be displayed first. You may handle this separately if needed.
I had a similar issue and the trick that did it for me was this one
*"ORDER BY LENGTH(column_name), column_name
As long as the non-numeric part of the value is the same length, this will sort 1 before 10, 10 before 100, etc."*
as given by Andreas Bergström on this question.
Hope that helps someone.
this is the alphabetical order,
you want numerical order,
for do this you must in the ORDER BY clause
trim the costant "aaa-" part
convert it in number
convert(SUBSTRING(val, 3), integer)
I will give you a sample sorting. Not based on your data sample, but this could help you out.
Say you have data like this :
id
----
1
2
6
10
13
when you do ORDER BY id ASC would return :
id
----
1
10
13
2
6
I suggest, use LPAD.
This query : SELECT LPAD('12',5,'0') return 00012
So when you have table data like I provide above, you can sort them like this :
SELECT * FROM TABLE
ORDER BY LPAD(ID,7,'0') ASC
Based on your data.
SELECT SUBSTR('aaa-100',5,LENGTH('aaa-100') - 3) return 100
So, SELECT LPAD( SUBSTR('aaa-100',5,LENGTH('aaa-100') - 3), 7, '0') return 00000100
So you can combine string function such as SUBSTR and LPAD. Do have any clue now?

Unable to do sum and division in mysql

Here is my mysql query
SELECT IntervalStartTime,IFNULL(SUM(AbandonedCalls),0) AS AbandonedCallSum,SUM(QueueTime) AS QTS,SUM(RingTime) AS RTS,
IFNULL(SUM(AnsweredCalls),0) AS AnsweredCallSum
FROM intervalqueuestatistics
WHERE CallCenterId=17 AND DATE_FORMAT(IntervalStartTime,'%m')=10 AND DATE_FORMAT(IntervalStartTime,'%Y')=2012
GROUP BY DATE_FORMAT(IntervalStartTime,'%d');
Now i want to calculate a value (SUM(QueueTime)+SUM(RingTime))/SUM(AnsweredCalls)
So i modified my query accordingly as below
SELECT IntervalStartTime,IFNULL(SUM(AbandonedCalls),0) AS AbandonedCallSum,SUM(QueueTime) AS QTS,SUM(RingTime) AS RTS,
IFNULL(SUM(AnsweredCalls),0) AS AnsweredCallSum,IFNULL(SUM(QueueTime),0) + IFNULL(SUM(RingTime),0)/IFNULL(SUM(AnsweredCalls),0)
FROM intervalqueuestatistics
WHERE CallCenterId=17 AND DATE_FORMAT(IntervalStartTime,'%m')=10 AND DATE_FORMAT(IntervalStartTime,'%Y')=2012
GROUP BY DATE_FORMAT(IntervalStartTime,'%d');
But when executed it isn't giving me the correct answer.
For example one of the rows returned by this query
QTS RTS AnsweredCallSum CalculatedField
188000 41645 9 192627.222
But the CalculatedField is wrong it should be 25516.11 as per the calculation mentioned above
If SUM(AnsweredCalls) is 0, you are dividing by 0 and it should not work.
This would be my guess:
use IFNULL(..., 1) so you're never dividing by 0 should a null column exist.
Explicitly use parenthesis so order of operations doesn't fail. (You're currently performing sum1 + (sum2 / sum3) when I think you wanted (sum1 + sum2) / sum3). Remember multiplication & division come before addition & subtraction without the parenthesis.
( IFNULL(SUM(QueueTime), 0) + IFNULL(SUM(RingTime), 0) )
/ IFNULL(SUM(AnsweredCalls), 1)

MySQL: LIMIT by a percentage of the amount of records?

Let's say I have a list of values, like this:
id value
----------
A 53
B 23
C 12
D 72
E 21
F 16
..
I need the top 10 percent of this list - I tried:
SELECT id, value
FROM list
ORDER BY value DESC
LIMIT COUNT(*) / 10
But this doesn't work. The problem is that I don't know the amount of records before I do the query. Any idea's?
Best answer I found:
SELECT*
FROM (
SELECT list.*, #counter := #counter +1 AS counter
FROM (select #counter:=0) AS initvar, list
ORDER BY value DESC
) AS X
where counter <= (10/100 * #counter);
ORDER BY value DESC
Change the 10 to get a different percentage.
In case you are doing this for an out of order, or random situation - I've started using the following style:
SELECT id, value FROM list HAVING RAND() > 0.9
If you need it to be random but controllable you can use a seed (example with PHP):
SELECT id, value FROM list HAVING RAND($seed) > 0.9
Lastly - if this is a sort of thing that you need full control over you can actually add a column that holds a random value whenever a row is inserted, and then query using that
SELECT id, value FROM list HAVING `rand_column` BETWEEN 0.8 AND 0.9
Since this does not require sorting, or ORDER BY - it is O(n) rather than O(n lg n)
You can also try with that:
SET #amount =(SELECT COUNT(*) FROM page) /10;
PREPARE STMT FROM 'SELECT * FROM page LIMIT ?';
EXECUTE STMT USING #amount;
This is MySQL bug described in here: http://bugs.mysql.com/bug.php?id=19795
Hope it'll help.
I realize this is VERY old, but it still pops up as the top result when you google SQL limit by percent so I'll try to save you some time. This is pretty simple to do these days. The following would give the OP the results they need:
SELECT TOP 10 PERCENT
id,
value
FROM list
ORDER BY value DESC
To get a quick and dirty random 10 percent of your table, the following would suffice:
SELECT TOP 10 PERCENT
id,
value
FROM list
ORDER BY NEWID()
I have an alternative which hasn't been mentionned in the other answers: if you access from any language where you have full access to the MySQL API (i.e. not the MySQL CLI), you can launch the query, ask how many rows there will be and then break the loop if it is time.
E.g. in Python:
...
maxnum = cursor.execute(query)
for num, row in enumerate(query)
if num > .1 * maxnum: # Here I break the loop if I got 10% of the rows.
break
do_stuff...
This works only with mysql_store_result(), not with mysql_use_result(), as the latter requires that you always accept all needed rows.
OTOH, the traffic for my solution might be too high - all rows have to be transferred.