I have one table with the following fields:
battle_id, winner, looser
1 200 44
2 55 366
3 44 200
4 123 200
5 200 44
6 55 366
7 177 205
8 188 211
9 366 55
10 55 366
right now it has about 1300 records (its small), and there are about 400 players, in each battle there can only be a winner and a looser (there are no draws)
how can i find all the repeated battles? i do not want to find all the repeated battles of one player, i do want to know all the repeated battles of all the players...i know that i cam make a recursive function in php that iterates over all the battles and assign them to a matrix, but just for fun...is there a way to do it only on mysql?
And how can i optimize the table to find the repeated battles more quickly?
regards
EDIT:
For example i want the query to show:
battle_id, winner, looser
1 200 44
2 55 366
3 44 200
5 200 44
6 55 366
9 366 55
10 55 366
This should work, using a self-join could result in many duplicated entries
SQLFIDDLE
SELECT
t1.battle_id,
t1.winner,
t1.loser
FROM
your_table t1
WHERE
EXISTS (
SELECT
1
FROM
your_table t2
WHERE
( ( t1.winner = t2.winner
AND t1.loser = t2.loser )
OR ( t1.loser = t2.winner
AND t1.winner = t2.loser ) )
AND t1.battle_id <> t2.battle_id
)
try this:
SELECT b1.battle_id,
b1.winner,
b1.looser
FROM battles as b1
group by b1.battle_id, b1.winner, b1.looser
having count(*)>=2
Related
I have the following columns:
2022-05-25T17:31:34+0000 92 7 1
2022-05-25T16:06:46+0000 50 5 9
2022-05-25T13:05:27+0000 91 10 106
2022-05-25T09:17:01+0000 48 4 4
2022-05-25T08:43:05+0000 60 4 2
2022-05-25T06:26:38+0000 24 3 6
2022-05-24T15:14:49+0000 55 12 6
2022-05-24T12:25:35+0000 43 8 2
2022-05-24T11:15:24+0000 66 7 2
2022-05-24T10:45:56+0000 37 15 2
2022-05-23T17:51:09+0000 59 7 1
2022-05-23T17:50:44+0000 47 6 3
2022-05-23T15:48:02+0000 126 7 13
2022-05-23T11:42:26+0000 64 9 9
2022-05-27T06:00:29+0000 3 0 1
2022-05-25T17:31:34+0000 96 7 1
2022-05-25T16:06:46+0000 55 5 9
2022-05-25T13:05:27+0000 99 11 116
2022-05-25T09:17:01+0000 52 4 15
2022-05-25T08:43:05+0000 61 4 2
2022-05-25T06:26:38+0000 26 3 6
2022-05-24T15:14:49+0000 57 13 7
2022-05-24T12:25:35+0000 43 8 2
2022-05-24T11:15:24+0000 66 7 2
2022-05-24T10:45:56+0000 38 15 2
2022-05-23T17:51:09+0000 59 7 1
2022-05-23T17:50:44+0000 47 6 3
2022-05-23T15:48:02+0000 127 7 13
If you look at the 16th row it has the same date as the first row but the number of PostLikes are different (92 and 96) PostComments and PostShares also change. I want to create a stored procedure where when I input the date it will return the difference. For example, if I select the third row with date 2022-05-25T13:05:27+0000 it should give the result:
DateT PostLikes PostComments PostShares
2022-05-25T13:05:27+0000 7 1 10
Now one method of doing this is:
select max(PostLikes) - min(PostLikes) as LikeDifference, max(PostComments) - min(PostComments) as CommentDifference, max(PostShares) - min(PostShares) as ShareDifference
from kpitb.userLikes
where DateT = "2022-05-25T17:31:34+0000";
But this method is only good if there are two values, the database is going to have several values with the same dates. (Note DateT is VARCHAR)
For e.g.
Row 1 2022-05-25T13:05:27+0000 24
Row 2 2022-05-25T13:05:27+0000 34
Row 3 2022-05-25T13:05:27+0000 67
How to find the difference.
Any help would be appreciated.
Using window functions you can find the max id and the previous values and the main query becomes trivial.
with cte as
(select t.*,
lag(postlikes) over(partition by datet order by id) prevlikes,
lag(postcomments) over(partition by datet order by id) prevcomments,
lag(postshares) over(partition by datet order by id) prevshares,
t1.maxid
from t
join (select max(id) maxid,datet from t group by datet) t1 on t1.datet = t.datet
order by datet,id
)
select datet,
case when prevlikes is null then postlikes else postlikes - prevlikes end likes,
case when prevcomments is null then postcomments else postcomments - prevcomments end comments,
case when prevshares is null then postshares else postshares - prevshares end shares
from cte
where id = maxid;
https://dev.mysql.com/doc/refman/8.0/en/window-functions.html
Sql Table
Sales Date order Id customer_id medium
07-07-2018 WP241530914666620 26 4
21-07-2018 WP241532133344497 26 4
29-07-2018 WP241532821589511 26 4
01-07-2018 1100070191 61 3
05-07-2018 1100071337 61 3
11-07-2018 1100073077 61 3
15-07-2018 1100074754 61 3
21-07-2018 1100075959 61 3
25-07-2018 1100077484 61 3
01-07-2018 100207654 64 3
07-07-2018 100210077 64 3
08-07-2018 WP241531004708220 64 4
Result:-
Sales Date order Id customer_id medium
07-07-2018 WP241530914666620 26 4
21-07-2018 WP241532133344497 26 4
29-07-2018 WP241532821589511 26 4
01-07-2018 1100070191 61 3
05-07-2018 1100071337 61 3
11-07-2018 1100073077 61 3
i need mysql query for above result .
last 3 orders for each customers in above table . i am trying some codes but unable to get results for the above
You could using correlated subqueries like:
select Sales_Date,order_Id,customer_id,medium
from
(select y1.Sales_Date,y1.order_Id,y1.customer_id,y1.medium,
(select count(*)
from 'yourtable' y2
where y2.customer_id=y1.customer_id
and (y2.Sales_Date < y1.Sales_Date or y2.Sales_Date = y1.Sales_Date)
) rn
from 'yourtable' y1
) finalresult
where rn<=3
Reference:
correlated subqueries
Example
id district icnum
1 10 111
2 10 112
3 10 113
4 10 114
5 10 111
6 20 115
7 20 116
8 20 117
9 20 111
10 20 111
11 30 118
12 30 119
13 30 111
14 30 111
15 30 120
I have the above sample table. I want to create a mysql query to check duplication of 'icnum', a summary/count and also a list of the duplication. What
I want is:
How many 'icnum' within district '10' is found in district '20'.
How many 'icnum' within district '10' is found in district '30'.
How many 'icnum' within district '20' is found in district '30'.
I've tried several queries found in stackoverflow but it doesn't give me the result I want. I'm a newbie in complex sql query.
So should I execute the query separately for every district to get the result. Please masters of MySQL in stackoverflow, help me with this. Tq.
Below is the sample output that i want:
district district count(*)
10 20 2
10 30 2
20 30 2
You can do this using a self-join:
select t1.district, t2.district, count(distinct t1.icnum)
from t t1 join
t t2
on t1.icnum = t2.icnum and t1.district < t2.district
group by t1.district, t2.district;
Notes:
If you don't have duplicates, then use count(*) instead of count(*).
This will not return pairs that have nothing in common (although that could be fixed).
I am working in access 2010 I have a table which has the following format:
ID BOORP BEGIN_DIEPTE EIND_DIEPTE TEXTUUR1
1 148000 0 10 ZK
2 148000 20 60 ZK
3 148000 60 80 MK
4 148000 80 110 MK
5 148000 110 130 ZK
6 148000 130 160 -
7 148000 160 220 ZZL
8 148000 220 250 -
9 148000 250 300 MK
10 148001 0 20 ZK
11 148001 20 40 -
12 148001 40 210 ZZL
13 148001 210 310 ZZL
What i want is to join certain rows which have the same texture under the following conditions:
1) rows must have the same BOORP in order to be merged
2) only consecutive rows may be merged
The result should look like this:
ID BOORP BEGIN_DIEPTE EIND_DIEPTE TEXTUUR1
1 148000 0 60 ZK
3 148000 60 110 MK
5 148000 110 130 ZK
6 148000 130 160 -
7 148000 160 220 ZZL
8 148000 220 250 -
9 148000 250 300 MK
10 148001 0 20 MK
11 148001 20 40 -
12 148001 40 310 ZZL
It is especially the 2nd condition i'm having problem with.
Any suggestions?
That's quite a complex task as an SQL-only solution.
If you want to do it in one query, you can use the following solution (tested on your sample data):
SELECT e.ID, e.BOORP, e.BEGIN_DIEPTE, e.EINDDIEPTE As EIND_DIEPTE, e.TEXTUUR1
FROM
(
SELECT a.ID, a.BOORP, a.BEGIN_DIEPTE, IIF(b.ID Is Null, a.EIND_DIEPTE, b.EIND_DIEPTE) AS EINDDIEPTE, a.TEXTUUR1, b.ID As IDJoined
FROM TestTable a
LEFT JOIN
(
SELECT c.*, (SELECT Max(d.ID) FROM TestTable d WHERE d.ID < c.ID) As PreviousID
FROM TestTable c
) As b
ON b.PreviousID = a.ID AND b.BOORP = a.BOORP AND a.TEXTUUR1 = b.TEXTUUR1
) AS e
WHERE e.ID NOT IN (
SELECT b.ID
FROM TestTable a
INNER JOIN
(
SELECT c.*, (SELECT Max(d.ID) FROM TestTable d WHERE d.ID < c.ID) As PreviousID
FROM TestTable c
) As b
ON b.PreviousID = a.ID AND b.BOORP = a.BOORP AND a.TEXTUUR1 = b.TEXTUUR1
)
It's quite a lot of subqueries, and a bit too much to explain in depth. Simply put, the most inner subquery (query b) LEFT JOINs on equal BOORP and TEXTUUR1 and where the ID is the previous ID, then the outer subquery (query e) takes the result, and removes all the entries that have been joined with another entry.
Note that this joins 2 entries together, not more than that. If you want to join more than 2 entries together, you can just run a similar query on the result of this one.
I have two different queries. One for "plus" and one for "minus". I want to find the difference in the count value for each player.
I tried union all and got some very weird numbers.
Here are the results of each query which I ned to find the difference of:
fk_plus_player1_id cnt
90 71
65 68
79 66
45 59
64 57
27 56
55 56
93 55
37 55
40 44
1 36
84 33
20 31
24 28
8 23
fk_minus_player1_id cnt
93 44
64 42
79 40
37 35
90 33
20 31
84 31
27 30
65 30
40 26
1 26
24 25
45 25
55 22
8 10
How would I accomplish this? Thanks in advance for your help. I am a noob...
UGH...Trying to do the join method. Having issues, getting no results, just 4 empty columns. This is what I am trying
SELECT
*
FROM
(
SELECT
statement for plus results here
) AS tp
JOIN (
SELECT
statement for minus results here
) AS tm ON tp.fk_plus_player1_id = tm.fk_minus_player1_id
GROUP BY
fk_plus_player1_id
suggestions??
You have two tables.
You want for each player, the difference of the counts.
So :
SELECT t1.fk_minus_player1_id AS player, ABS(t1.cnt - t2.cnt) AS difference
FROM table1 t1, table2 t2
WHERE t1.fk_minus_player1_id = t2.fk_plus_player1_id
GROUP BY t1.fk_minus_player1_id;
Maybe this is what you're looking for ?
WITH query1 AS
(SELECT t1.fk_minus_player1_id AS player, (t1.cnt - IFNULL(t2.cnt,0)) AS difference
FROM table1 t1 LEFT OUTER JOIN table2 t2 ON t1.fk_minus_player1_id = t2.fk_plus_player1_id
GROUP BY t1.fk_minus_player1_id),
query2 AS (SELECT t2.fk_plus_player1_id AS player, (IFNULL(t1.cnt,0) - t2.cnt) AS difference
FROM table2 t2 LEFT OUTER JOIN table1 t1 ON t1.fk_minus_player1_id = t2.fk_plus_player1_id
GROUP BY t2.fk_plus_player1_id)
(SELECT player, difference
FROM query1)
UNION
(SELECT player, difference
FROM query2 WHERE player NOT IN (SELECT player FROM query1))
You run the risk that the same players are not in both lists. The solution is union all with group by:
select player1id, sum(pluscnt) as pluscnt, sum(minuscnt) as minuscnt,
(sum(pluscnt) - sum(minuscnt)) as diff
from ((select player1id, cnt as pluscnt, 0 as minuscnt
from plustable
) union all
(select player1id, 0, cnt
from minustable
)
) t
group by player1id;