MYSQL get COUNT of highest column value for different phases - mysql

This is a pretty complicated database. So I have simplified the structure for easy understanding.
I have 2 tables
TABLE A : transactions
p_id rev_count
1 1
2 1
3 1
1 1
1 1
1 2 (Gets incremented as it has reached max entries)
2 1
2 1
2 2 (Gets incremented as it has reached max entries)
1 2
2 2
TABLE B : rev_countReference
p_id rev_count
1 2
2 2
3 1
So basically Table B stores the rev_count value as reference for TABLE A. Since p_id can accept only 3 transactions, whenever p_id goes exceeds 3 count the rev_count value is incremented and next p_id entry will have an incremented rev_count value.
QUESTION
I want to get number of transactions occupied in the maximum rev_count by each p_id.
Desired Output :
p_id used_rev
1 1
2 2
3 1
Tried so far :
Unfortunately i'm not able to write a code for the above DB example yet. But what I have tried so far with actual code.
public function getfreeslots($user_id) {
$this->db->select('count(t.id), t.reinvest_count, MAX(t.reinvest_count) AS max_reinvest, COUNT(t.id) AS outstandingSlots');
$this->db->from('transactions t');
$this->db->join('reinvestCount rc', "t.receiver_id=rc.user_id AND t.phase_id=rc.phase_id",'LEFT');
$this->db->where('t.receiver_id', $user_id);
$this->db->having('t.reinvest_count >= max_reinvest');
$this->db->group_by('t.phase_id');
$this->db->order_by('t.phase_id', 'ASC');
$query = $this->db->get();
$row = $query->result();
if (empty($row))
return FALSE;
return $row;
}
I'm using Codeigniter query builder. So answer using query builder will be much appreciated.

Related

SQL Order by a specific value of a one to many field

The context :
I have these three simple SQL tables : my database diagram
a Request has one or N Request_Field
Request_Field is a pivot table
Value inside the pivot table Request_Field can sometimes contain dates in string format
a Field has 0 or N Request_Field
These tables are part of a greater database and I cannot change them at the moment.
My question :
Is it possible to query multiple "Request" and order them by a specific value in "Request_Field" ?
Example :
Request table
id
1
2
3
Request_Field table
id
request_id
field_id
value
1
1
1
John
2
1
2
2021-01-01
3
2
1
Jane
4
2
2
2020-01-01
5
3
1
Jack
6
3
2
2022-01-01
Field table
id
label
1
name
2
desired_date
The SQL query I try to make : get all Request ordered by the desired_date record of the Field table (DESC).
Request desired results
id
3
1
2
Thank you

mysql if condition add value else add value1

I'am trying to get total full time and total half time by user, Timing is stored in single column, Integer value of timing should come in full time as sum(timing) and floating value in half time but in count
id user_id timing
1 2 1
2 2 2.5
3 1 1.5
4 1 1
5 3 3
6 2 2.5
I need the result as
user_id fulltime halftime
1 2 1
2 5 2
3 3 0
SELECT user_id
, SUM(FLOOR(timing)) AS fulltime
, SUM((timing % 1) * 2) AS halftime
FROM table
GROUP BY user_id;
This query should help you. please try it on your data
SELECT user_id,
sum(if(ceil(timing)>timing,0,timing)) as fulltime,
sum(if(ceil(timing)>timing,timing,0)) as halftime
FROM rest
GROUP BY user_id
Thanks
Amit

MYSQL Can WHERE IN default to ALL if no rows returned

Have a existing table of results like this;
race_id race_num racer_id place
1 0 32 2
1 1 32 3
1 2 32 1
1 3 32 6
1 0 44 2
1 1 44 2
1 2 44 2
1 3 44 2
etc...
Have lots of PHP scripts that access this table output the results in a nice format.
Now I have a case where I need to output the results for only certain race_nums.
So I have created this table races_included.
race_view race_id race_num
Day 1 1 0
Day 1 1 1
Day 2 1 2
Day 2 1 3
And can use this query to get the right results.
SELECT racer_id, place from results WHERE race_id=1
AND race_num IN
(SELECT race_num FROM races_included WHERE race_id='1' AND race_view='Day 1')
This is great but I only need this feature for a few races and to have it work in a compatible mode for the simple case show all races. I need to add alot of rows to the races_included table. Like
race_view race_id race_num
All 1 0
All 1 1
All 1 2
All 1 3
95% of my races don't use the daily feature.
So I am looking for a way to change the query so that if for race 1 there are no records in the races_included table it defaults to all races. In addition I need it to be close the same execution speed as the query without the IN clause, because this query Or variations of it are used a lot.
One way that does work is to redefine the table as races_excluded and use NOT IN. This works great but is a pain to manage the table when races are added or deleted.
Is there a simple way to use EXISTS and IN in tandem as a subquery to get the desired results? Or some other neat trick I am missing.
To clarify I have found a working but very slow solution.
SELECT * FROM race_results WHERE race_id=1
AND FIND_IN_SET(race_num, (SELECT IF((SELECT Count(*) FROM races_excluded
WHERE rid=1>0),(SELECT GROUP_CONCAT(rnum) FROM races_excluded
WHERE rid=1 AND race_view='Day 1' GROUP BY rid),race_num)))
It basically checks if any records exists for that race_id and if not return a set equal to the current race_num and if yes returns a list of included race nums.
You can do this by using or in the subquery:
SELECT racer_id, plac
from results
WHERE race_id = 1 AND
race_num IN (SELECT race_num
FROM races_included
WHERE race_id = '1' AND (race_view = 'Day 1' or raw_view = 'ANY')
);

MySQL: Find number of users with identical poll answers

I have a table, poll_response, with three columns: user_id, poll_id, option_id.
Give an arbitrary number of poll/response pairs, how can I determine the number of distinct user_ids match?
So, suppose the table's data looks like this:
user_id | poll_id | option_id
1 1 0
1 2 1
1 3 0
1 4 0
2 1 1
2 2 1
2 3 1
2 4 0
And suppose I want to know how many users have responded "1" to poll 2 and "0" to poll 3.
In this case, only user 1 matches, so the answer is: there is only one distinct user.
But suppose I want to know how many users have responded "1" to poll 2 and "0" to poll 4.
In this case, both user 1 and user 2 match, so the answer is: there are 2 distinct users.
I'm having trouble constructing the MySQL query to make this happen, especially given that there are an arbitrary number of poll/response pairs. Do I just try to chain a bunch of joins together?
To know how many users have responded "1" to poll 2 and "0" to poll 3.
select count(user_id) from(
select user_id from tblA
where (poll_id=2 and option_id=1) or (poll_id=3 and option_id=0)
group by user_id
having count(user_id)=2
)m
SQL FIDDLE HERE.

Changed MYSQL database table a little bit, need this query to work as before

So this seems like a simple little problem, but I haven't been successful with getting this to work properly. I have a comments table, a posts table, and a notifications table. Previously I stored the postID in a column called "uniqueID" for notifications and for comments. Now I have changed it so that the comments unique id is stored in that column instead. So all I need to do is for each row that is a comment in notifications, go to the comments table where uniqueID==id and then return the value in that table called postID.
Here is a photo showing my question better.
http://i41.tinypic.com/nyzolg.png
The information from the photo is:
Table: Notifications
id UserID FromID UniqueID Action State Read_Date Date
1 1 2 1 1 0 0 1325993600
2 1 6 2 1 0 0 1325993615
3 1 2 1 2 0 0 1325993622
4 1 6 2 2 0 0 1325993661
5 2 6 2 2 0 0 1325993661
Action = 1 means UniqueID identifies a row in Posts;
Action = 2 means UniqueID identifies a row in Comments.
Table: Posts
id ToID FromID Post State Date
1 1 2 Hey 0 1325993600
2 1 6 okay yeah 0 1325993615
Table: Comments
ID PostID FromID Comment State Date
1 1 2 lol 0 1325993622
2 1 6 ohh 0 1325993661
So, in the Notifications table where action is 2, the UniqueID's are for the 'id' in the Comments table.
What I want to return is the PostID, so in the query it would just be as if the UniqueID was this instead:
1
2
1
1
1
But the UniqueID would stay the same where Action is 1.
My current query is this, and it worked fine before I changed my database values around.
$notificationsq = mysql_query("
SELECT
N.*,
P.*,
MAX(N.date) AS newDate
FROM
notifications N,
posts
WHERE
N.userID='$session'
AND
(
(
N.action='1'
AND
(N.state = 0 OR N.state=1)
)
OR
N.action='2'
)
AND P.state='0'
AND
N.uniqueID=P.id
GROUP BY
N.uniqueID
ORDER BY
N.state ASC,
newDate DESC
") or die(mysql_error());
Try adding "C.*" to your select clause, "comments C" to your from clause, and change " N.action='2'" to
(N.action='2' and (N.uniqueId = C.id))