MySQL Rank my total score - php

I have a table that looks something like below without the rank field. I want to put that rank field as it is in the table below. How can I achieve it in MySQL?
# name aa bb cc total | rank
1 name1 20 20 30 70 | 1
2 name2 10 20 30 60 | 2
3 name3 20 10 25 55 | 3
4 name4 20 20 30 70 | 1
5 name5 10 10 20 40 | 4

Here is a way to do it, the result will be sorted with respect to the total.
mysql> create table test (id int, name varchar(100),total int);
Query OK, 0 rows affected (0.13 sec)
mysql> insert into test values
-> (1,'name1',70),
-> (2,'name2',60),
-> (3,'name3',55),
-> (4,'name4',70),
-> (5,'name5',40);
Query OK, 5 rows affected (0.02 sec)
select
id,
name,
case
when #cur_rank = total then #rank
else #rank := #rank + 1
end as rank,
#cur_rank := total as total
from test ,(select #rank:=0, #cur_rank:=0)r
order by total desc ;
+------+-------+------+-------+
| id | name | rank | total |
+------+-------+------+-------+
| 1 | name1 | 1 | 70 |
| 4 | name4 | 1 | 70 |
| 2 | name2 | 2 | 60 |
| 3 | name3 | 3 | 55 |
| 5 | name5 | 4 | 40 |
+------+-------+------+-------+

Try this:-
SELECT #, name, aa, bb, cc, total, FIND_IN_SET( total, (
SELECT GROUP_CONCAT( total ORDER BY total DESC )
FROM your_tab
)
) AS rank
FROM your_tab;

Related

Selecting max value while group by two columns with order on another column

I have table with 4 rows
id season_id market elements I would like to select for each same season_id, market that hold max result and if max results are the same then max based on elements only where elements are higher than 9.
id | season_id | market | result | elements
1 | 20 | fh | 75 | 20
2 | 20 | fh | 75 | 22
3 | 20 | SH | 81 | 18
4 | 20 | SH | 75 | 20
5 | 21 | fh | 90 | 14
6 | 21 | fh | 86 | 16
7 | 21 | SH | 90 | 18
8 | 21 | SH | 91 | 2
I would like to get
id | season_id | market | result | elements
2 | 20 | fh | 75 | 22
3 | 20 | SH | 81 | 18
5 | 21 | fh | 90 | 14
7 | 21 | SH | 90 | 18
I've tried
SELECT a.* FROM results a INNER JOIN (SELECT id, market, MAX(result) as perc FROM
results where elements>9 group by market ) group ON a.market = group.market and
a.result = group.perc group by market
But it doesn't select all the markets and I'm not sure how to add selection by number of elements to that
You seem to want one result per season_id/market pair. I'm not 100% sure what the limit on elements > 9 is supposed to be doing, but I think it is an overall filter.
To get the rows with the maximum result and elements for each season and market, use row_number():
select t.*
from (select t.*,
row_number() over (partition by season_id, market order by result desc, elements desc) as seqnum
from t
where elements > 9
) t
where seqnum = 1;
Here is a db<>fiddle.
In older versions of MySQL (or even more recent versions), you can use a correlated subquery:
select t.*
from t
where t.id = (select t2.id
from t t2
where t2.season_id = t.season_id and t2.market = t.market and
t2.elements > 9
order by t2.result desc, t2.elements desc
limit 1
);

Find duplicated combination of rows in an array

I got a 2D array like this:
row | user_id | risk_id | measure_id | description
---------------------------------------------------
0 | 73 | 12 | 32 | 'foo'
1 | 73 | 12 | 32 | 'bar'
2 | 31 | 23 | 31 | 'foobar'
3 | 31 | 23 | 31 | 'foo'
I want to check if the combination between user_id, risk_id and measure_id is unique, and if not return another 2D array with the rows being the user_id and the columns the row, like this
user_id | row_0 | row_1 | ......
--------------------------------
73 | 0 | 1 | ......
31 | 2 | 3 | ......
This is for finding duplicates, but will return rows, not columns:
SELECT x.user_id, x.row
FROM YOUR_TABLE x
JOIN (SELECT t.user_id
FROM YOUR_TABLE t
GROUP BY t.user_id, risk_id, measure_id
HAVING COUNT(t.user_id) > 1) y ON y.user_id= x.user_id;
This one will return a table grouped by user_id, and duplicated rows stored comma separated in column rows:
user_id | rows
72 | 0, 1
31 | 2, 3
SELECT x.user_id, GROUP_CONCAT(x.row SEPARATOR ',') as rows
FROM YOUR_TABLE x
JOIN (SELECT t.user_id
FROM YOUR_TABLE t
GROUP BY t.user_id, risk_id, measure_id
HAVING COUNT(t.user_id) > 1) y ON y.user_id= x.user_id
GROUP BY x.user_id;

Get Highest amount , count of products / users

id | user_id | prd_id | amnt | dis
1 | 1 | 10 | 200 | 23
2 | 2 | 10 | 300 | 11
3 | 3 | 20 | 100 | 26
4 | 2 | 20 | 50 | 12
5 | 4 | 30 | 100 | 22
6 | 2 | 40 | 600 | 18
7 | 2 | 30 | 100 | 16
I want 2 result from above table :
First by prod_id as below
prd_id | user_id | cont | highestamt | disc
10 | 2 | 2 | 300 | 11
20 | 3 | 2 | 100 | 26
30 | 4 | 2 | 100 | 22
40 | 2 | 1 | 600 | 18
Second by user_id as below:
user_id | cont | bid on prd_id | winner on bid prod_id |
1 | 1 | 10 | - | -
2 | 4 | 10,20,30,40 | 10,40 |
3 | 1 | 20 | 20 |
4 | 1 | 30 | 30 |
UPDATE : ex: above : user_id = 2 has bid on product 10,20,30,40 ( bid on prd_id ) hence his bidding cont = 4 ...and out of which he is winner in 10,40 ( winner on bid prod_id ) ..WHY ONLY 10,40 and not 30 ...bcz user_id =4 has bid on prd=30 with amt =100 and user_id =2 with amt=100 ..but first bid was made by user=4 on prd=30 hence he is winner for prd=30 ( for same amt )
Tried below query for by prd_id but it giving me some wrong result.
SELECT `prd_id`, `user_id` , count('prd_id') as cont , max(`amnt`) as highestamt,disc
FROM `proddtails`
group by `prd_id` order by `prd_id`
above query result as below : ( user_id,disc not coming proper )
prd_id | user_id | cont | highestamt | disc
10 | 2 | 2 | 300 | 11
20 | 2 | 2 | 100 | 11
30 | 2 | 1 | 100 | 11
40 | 2 | 1 | 600 | 11
For second by user_id I am not getting what will be query.
Thanks
UPDATE :
THANKS FOR HARSHIL : http://www.sqlfiddle.com/#!9/5325a6/5/1
but after some more entry i found this bug : http://www.sqlfiddle.com/#!9/e04063/1 for second : for user_id but works well for prd_id (first query )
user_id cont bid_on_prd_id winner_on_bid_prod_id
1 1 10 (null)
2 4 10,20,40,30 10,40,30
3 1 20 20
4 1 30 30
but i want as below :
without null user_id
user_id cont bid_on_prd_id winner_on_bid_prod_id
2 4 10,20,30,40 10,40
3 1 20 20
4 1 30 30
with null user_id ( but in my wamp server i don't see null in winner_on_bid_prd_id for user_id =1 , i get value 10 instead of null )
user_id cont bid_on_prd_id winner_on_bid_prod_id
1 1 10 (null)
2 4 10,20,30,40 10,40
3 1 20 20
4 1 30 30
For prd_id:
select t1.prd_id,t1.user_id,
(select count(*) from tablename where prd_id = t1.prd_id)as cont,
t1.amnt as highststatment,
t1.dis as disc
from tablename t1
where (t1.prd_id,t1.amnt) in
(select prd_id, max(amnt) from tablename group by prd_id)
group by t1.prd_id;
For usr_id:
select t1.user_id,
count(*) as cont,
Group_concat(t1.prd_id separator ',') as bid_on_prd_id,
(select Group_concat(distinct t2.prd_id separator ',')
from tablename t2
where t2.user_id = t1.user_id
and (t2.id) in
(select min(id) from tablename
where (prd_id,amnt) in
(select prd_id,max(amnt) from tablename group by prd_id)
group by prd_id
)
) as winner_on_bid_prod_id
from tablename t1
group by t1.user_id
having winner_on_bid_prod_id IS NOT NULL;
Click here for UPDATED DEMO

How to fetch data from table as per column value using PHP and MySQL

I need to filter value from table as per column value using PHP and MySQL. Here is my data:
db_images:
image_id member_id subcat_id from_day to_day
1 220 56 1 3
2 220 56 1 3
3 220 56 1 1
4 120 22 1 5
5 120 22 2 4
Here is my query:
$qry=mysqli_query($connect,"select * from db_iamges group by member_id,subcat_id order by image_id desc");
Using my query I am getting a record like below:
image_id member_id subcat_id from_day to_day
3 220 56 1 1
I need the to_day should always be higher value. If same member_id and subcat_id is present the the to_day will be always higher value and the from_day will be always smaller value. The expected output should like below.
image_id member_id subcat_id from_day to_day
1 220 56 1 3
4 120 22 1 5
It seems you have syntax problems, because if you copy-paste, you put "db_iamges". I made a table:
mysql> select * from prueba1;
+----------+-----------+-----------+----------+--------+
| image_id | member_id | subcat_id | from_day | to_day |
+----------+-----------+-----------+----------+--------+
| 1 | 220 | 56 | 1 | 3 |
| 2 | 220 | 56 | 1 | 3 |
| 3 | 220 | 56 | 1 | 1 |
| 4 | 120 | 22 | 1 | 5 |
| 5 | 120 | 22 | 2 | 4 |
| 6 | 120 | 22 | 2 | 9 |
| 7 | 120 | 22 | 2 | 2 |
+----------+-----------+-----------+----------+--------+
7 rows in set (0.00 sec)
And:
mysql> select image_id, member_id, subcat_id, min(from_day), max(to_day) from prueba1 group by member_id, subcat_id order by image_id asc;
+----------+-----------+-----------+----------+-------------+
| image_id | member_id | subcat_id | from_day | max(to_day) |
+----------+-----------+-----------+----------+-------------+
| 1 | 220 | 56 | 1 | 3 |
| 4 | 120 | 22 | 1 | 9 |
+----------+-----------+-----------+----------+-------------+
2 rows in set (0.00 sec)
It is working
EDIT: Updated, as I didn't understand your main problem.
Select
T.*
From db_images t
Inner join (
Select member_id, subcat_id, max(to_day) to_day
db_images group by member_id,subcat_id
) t2 on t.member_id = t2.member_id
And t.Subcat_id = t2.subcat_id
And t.to_day = t2.to_day;
use max(column name) to get higher value .
select image_id, member_id,subcat_id,from_day, max(to_day) as to_day from from db_iamges group by member_id,subcat_id order by image_id desc
try this
select t1.* from db_images t1 inner join(select max(member_id) as
mid,max(subcat_id) as cid,min(from_day) as fdy from db_images group
by member_id) t2 on t1.member_id = t2.mid and t2.cid = t1.subcat_id
and t2.fdy = t1.from_day group by member_id order by image_id asc;
check here

What query do I run to select the HIGHEST value for EACH group of values in an SQL table?

I have a table of bids (like an eBay bids, for example):
bid_id | bid_from_user | bid_for_auction | bid_price
1 | 150 | 1 | 20
2 | 453 | 1 | 30
3 | 42 | 1 | 50
4 | 12 | 2 | 12
5 | 60 | 2 | 20
I need to select each bid_for_auction only once with its highest current bid, so my result should look like this:
bid_for_auction | bid_price
1 | 50
2 | 20
How would I go about doing this?
Use GROUP BY
SELECT bid_for_auction, MAX(bid_price) AS bid_price
FROM bids GROUP BY bid_for_auction;
SELECT bid_for_auction, MAX(bid_price)
FROM bids
GROUP BY bid_for_auction
This should do the trick:
SELECT
`bid_for_auction`,
MAX(`bid_price`)
FROM `bids`
GROUP BY `bid_for_auction`
SELECT bid_for_auction,MAX(bid_price) FROM bids GROUP BY bid_for_auction

Categories