counting overlapping bitwise columns in mysql - mysql

I have a table in which there is a bitwise column representing a list of statuses that can be attached to an entry. Each entry can have multiple statuses selected (hence the use of the bitwise logic).
What I'd like to do is pull a query that will tell me how many entires there are for each status (i.e. how many times each bit is turned on). The difficulty I have is that there is of course overlap so a GROUP BY or a DISTINCT is not going to cut it (as far as I can see).
As an example let's just have two values, 1 and 2. and the following data
Id | Status
1 | 1
2 | 1
3 | 2
4 | 3
Now, I want to count how many entries there are for each bit so I'd like something that counts that 3 value into both the 1 and 2 totals, outputting something like this:
Bit | Count
1 | 3
2 | 2
The closest I can get so far seems to be pulling out the distinct values and then adding those with multiple entries into their corresponding counts using PHP. Obviously, I'd like to do something a bit more elegant.
Any ideas?

Expand the bits table as required
select `bit`, count(*) `count`
from bitt s
inner join (select 1 `bit` union all
select 2 union all
select 3 union all
select 4 union all
select 5) bits on s.status & Pow(2,bits.`bit`-1)
group by bits.`bit`

You could do:
SUM(IF(`Status`&1,1,0)) as `count1`,
SUM(IF(`Status`&2,1,0)) as `count2`,
SUM(IF(`Status`&4,1,0)) as `count4`
If you want to optimize it, you can still GROUP BY Status, but then you would need (a little) post-processing to sum the 8 rows you would get for a 3-bit situation.

One more variant -
SELECT 0, COUNT(IF(status >> 0 & 1 = 1, 1, NULL)) FROM table1
UNION
SELECT 1, COUNT(IF(status >> 1 & 1 = 1, 1, NULL)) FROM table1
UNION
SELECT 2, COUNT(IF(status >> 2 & 1 = 1, 1, NULL)) FROM table1
...

Related

MySQL query loads forever

I have a table with 1v1 matches like this:
match_number|winner_id|loser_id
------------+---------+--------
1 | 1 | 2
2 | 2 | 3
3 | 1 | 2
4 | 1 | 4
5 | 4 | 1
and I would like to get something like this:
player|matches_won|matches_lost
------+-----------+------------
1 | 3 | 1
2 | 1 | 2
3 | 0 | 1
4 | 1 | 1
My MySQL Query looks like this
SELECT win_matches."winner_id" player, COUNT(win_matches."winner_id") matches_won, COUNT(lost_matches."loser_id") matches_lost FROM `matches` win_matches
JOIN `matches` lost_matches ON win_matches."winner_id" = lost_matches."winner_id"
I don't know what I did wrong, but the query just loads forever and doesn't return anything
You want to unpivot and then aggregate:
select player_id, sum(is_win), sum(is_loss)
from ((select winner_id as player_id 1 as is_win, 0 as is_loss
from t
) union all
(select loser_id, 0, 1
from t
)
) wl
group by player_id;
Your query is simply not correct. The two counts will produce the same same value -- COUNT(<expression>) returns the number of non-NULL rows for that expression. Your two counts return the same thing.
The reason it is taking forever is because of the Cartesian product problem. If a player has 10 wins and 10 losses, then your query produces 100 rows -- and this gets worse for players who have played more often. Processing all those additional rows takes time.
If you have a separate players table, then correlated subqueries may be the fastest method:
select p.*,
(select count(*) from t where t.winner_id = p.player_id) as num_wins,
(select count(*) from t where t.loser_id = p.player_id) as num_loses
from players p;
However, this requires two indexes for performance on (winner_id) and (loser_id). Note these are separate indexes, not a single compound index.
You are joining the same table twice.
Both the alias win_matches and lost_matches are on the table matches, causing your loop.
You probably don't need separate tables for win and losses, and could do both in the same table by writing one or zero in a column for each.
I don't to change your model too much and make it difficult to understand, so here is a slight modification and what it could look like:
SELECT m."player_id" player,
SUM(m."win") matches_won,
SUM(m."loss") matches_lost
FROM `matches` m
GROUP BY player_id
Without a join, all in the same table with win and loss columns. It looked to me like you wanted to know the number of win and loss per player, which you can do with a group by player and a sum/count.

MySQL query to find ids that do not exist in table

I have a list of ids pre-generated that I need to check if exist in a table. My table has two columns, id, name, where id is an auto increment integer and name is a varchar(255).
I basically want to get a count of how many ids do not exist in table foo from my pre-generated list. So say my list has the numbers 5 and 10 in it, what's the best way to write something to the extent of:
select count(*) from foo where id does not exist in ( 5, 10 )
The idea here is that if 5 and 10 do not exist, I need the response 2, and not the number of rows in foo that do not have the id 5 or 10.
TL; DR sample data and queries at rextester
The idea here is that if 5 and 10 do not exist, I need the response 2, and not the number of rows in foo that do not have the id 5 or 10.
You should have provided a little more information to avoid confusion.
Example
id | name
1 | tom
2 | joe
3 | mae
4 | goku
5 | vegeta
If your list contains (1, 2, 3) then your answer should be 0 (since all three are in the table )
If your list contains (1, 2, 6) then your answer should be 1. ( since 1 and 2 are in the table but 6 is in't )
If your list contains (1, 6, 7) then your answer should be 2.
If your list contains (6, 7, 8) then your answer should be 3.
assuming this was your question
If you know the length of your list
select 2 - count(*) as my_count from foo where id in (5, 10)
The following query tells you how many are present in foo.
select count(*) from foo where id in (5,10)
So if you want to find those that do not exist, subtract this result from the length of your list.
select n - count(*) as my_count from foo where id in (5, 10,....)
You could use on fly table using union and the a left join
select count(*)
from my_table as m
left join (
select 5 as id from dual
union
select 10 from dual ) t on t.id = m.id
where t.id is null
otherwise you can populate a tempo table with the value you need and use left join
where the value is null

sql get couple of elements

I have the following sql table:
| ID | numbers |
|----|-----------------------------|
| 1 | 1,3,19,23,28,32,39,42,60,80 |
| 2 | 1,3,18,24,29,33,40,43,61,80 |
| 3 | 1,2,3,25,30,34,41,44,62,78 |
In Numbers I have a string with 10 numbers.
I want to get all couple of two elements (and if it is possible for three, four etc) in SQL Server or MySQL.
For example for two elements:
1,3 appers in all rows (3 times)
1, 80 appears in the first and second row (2 times)
etc
I tried to split numbers from every row and insert into a temporary table and after generate combinations of 10 choose k (where k is numbers of elements in a couple) but something doesn't work. I don't know if it's the best idea.
My code in this moment: http://pastebin.com/qRjPdfay
Thanks
Yes, splitting your numbers coulmns to rows would make things easier. If you are using MySQL you could use a query like this:
CREATE TABLE mytable2 AS
SELECT
ID, SUBSTRING_INDEX(SUBSTRING_INDEX(numbers, ',', n),',',-1) AS number
FROM
mytable CROSS JOIN (SELECT 1 AS n
UNION ALL SELECT 2 AS n
UNION ALL SELECT 3 AS n
UNION ALL SELECT 4 AS n
UNION ALL SELECT 5 AS n
UNION ALL SELECT 6 AS n
UNION ALL SELECT 7 AS n
UNION ALL SELECT 8 AS n
UNION ALL SELECT 9 AS n
UNION ALL SELECT 10 AS n) d;
(this will work if all numbers contains exactly 10 numbers an no less, if there are less this query needs some improvements). Then you can count the time each number appears:
SELECT number, COUNT(*) as appears
FROM mytable2
GROUP BY number
ORDER BY appears DESC
and you can group number by the number of times they appear:
SELECT
appears, GROUP_CONCAT(number) AS numbers
FROM (
SELECT number, COUNT(*) as appears
FROM mytable2
GROUP BY number
ORDER BY appears DESC
) g
GROUP BY
appears
ORDER BY
appears DESC
(MySQL only) and the result will be like this:
| appears | numbers |
|---------|---------------|
| 3 | 3,1 |
| 2 | 80 |
| 1 | 43,23,40..... |
Please see a fiddle here.

MySQL distinct count

abc table -
ID
---
1
2
3
xyz table -
abc_id | flag
--------------
1 | 0
1 | 1
2 | 0
3 | 0
3 | 0
I need the count of distinct abc_id where if flag = 1 for a particular abc_id, then that id shouldn't be counted.
In the above example, the count should be 2. Is there any way I can achieve that? Sorry if the question has been answered before or if it's something obvious. Thanks in advance ^^
EDIT: Basically I want the abc_id = 1 to be ignored in the count because one of it's flag = 1. I hope it's clear enough :|
The main issue was figuring out what you meant, but I'm pretty sure it's this. :o)
Count the distinct abc_ids, fully excluding the ids that have at least one row that was flagged. So id 1 is excluded, because it has a flag.
You can solve this with a not in condition, to exclude all ids that were flagged. After that, you can count the remaining rows, using count(distinct abc_id), to get the number of distinct ids.
select
count(distinct abc_id) as non_flagged_id_count
from
xyz x1
where
abd_id not in
(select x2.abc_id from xyz x2 where x2.flag = 1)
select count(distinct abc_id)
from xyz as x
left join (
select abc_id from xyz where flag = 1
) as y using (abc_id)
where y.abc_id is null
;

Add a column to result, which is another SELECT with dependency

This one is a little bit tricky, at least to me to explain, so please, don't get mad if you don't get the point - it's likely caused by my poor explanation.
I want to get one more column from my main SELECT, which will represent number of rows from another table, suiting id of main record.
So, imagine main table, which I am selecting from. I'll call it simply main.
What I want to select from main, basically is:
SELECT * FROM main ORDER BY c1 ASC LIMIT 5
Plus I need one extra column for each row returned, which says number of rows from side table, matching the id:
SELECT COUNT(*) FROM side WHERE m_id = main_id
Maybe an example will tell you a little bit more
id data1 data2 id m_id ...
main ----|-------|------- side -----|------|-----
1 aa ab 1 1
2 xx yy 2 2
3 az bz 3 1
4 1
5 3
6 2
7 1
8 1
9 2
expected result:
id data1 data2 num
----|-------|-------|------
1 aa ab 5
2 xx yy 3
3 az bz 1
A simple way to add the count is with a correlated subquery:
SELECT m.*,
(select count(*) from side s where s.m_id = m.main_id) as side_cnt
FROM main m
ORDER BY c1 ASC
LIMIT 5;
You can also do this by changing the from clause. However, this method only affects the select part of the query.
You should be able to do it as a subquery:
SELECT m.*, ( SELECT COUNT(*) FROM side WHERE m_id = m.main_id ) as num FROM main m ORDER BY c1 ASC LIMIT 5
This basically runs a special query for each result that counts the number of matching results and displays it in the "num" column.