Combining two MySQL queries from the same table - mysql

I have a table set up currently as such:
issue|total | series
-----|--------|---------
1 | 1 | Series A
2 | 2 | Series A
1 | 3 | Series B
3 | 4 | Series A
2 | 5 | Series B
1 | 6 | Series C
It tracks series (series), along with the issue number (issue) within each series. As well as that, I have a running total (total) of the entire number of entries in this table.
What I would like to do is combine two queries, the first used to locate the last issue number within a given series, the second to always locate the last total number within the table. Presumably the two queries I need to use are as follows, I just don't see how would be best to combine them into one select query:
To locate last issue of a given series:
SELECT issue FROM seriesTable WHERE series = 'Series A|Series B|Series C' ORDER BY issue DESC LIMIT 1
To locate total number of entries:
SELECT total FROM seriesTable ORDER BY total DESC LIMIT 1
So, if I wanted to reference the last issue of Series A and the total number of entries (3, 6), what would be the best way to do this? I've tried INNER JOIN and UNION neither of which seem to work, however I think I'm on the right track, just using the syntax incorrectly.
I guess the other alternative is to run the queries separately, add to an array and have PHP give me the result, however I would rather avoid this if possible.

Original answer is below, after clarification I have have another answer
You only need to cross join a single row result set with the total you're fater.
for series A
SELECT series, MAX(issue), total.total FROM seriesTable
JOIN (SELECT total FROM seriesTable ORDER BY total DESC LIMIT 1) total
WHERE series = 'Series A';
or for every series
SELECT series, MAX(issue), total.total FROM seriesTable
JOIN (SELECT total FROM seriesTable ORDER BY total DESC LIMIT 1) total
GROUP BY series;
sql fiddle
Original answer
So, to get "total for each series as of the last issue for that series" you need to self join and group by series to find the max issue, then use that as you join to extract the total.
select issue as current_issue, total, series from seriesTable
natural join (SELECT series,
max(issue) as issue
FROM seriesTable
group by series) max_issues;
sql fiddle

You just need the max issue for a particular series, and you also need the max total for the table.
I think this is what your looking for:
SELECT max(issue),
(SELECT max(total)
FROM seriesTable)
FROM seriesTable
WHERE series = 'Series A'

Related

I need getting data from multiple tables, with COUNT included?

I need to create a query from 2 tables, where my company stores e-shop information.
Example of data from the first table:
currentDate: 5.5.2022 | eshopId: 1 | eshopName: test | active: true |
Table 2:
currentDate: 5.5.2022 | eshopId: 1 | orderId: 123 | attribution: direct |
From the first table, I want get how many days in a given period the eshop was active. From the second table, I would like to count all the orders that were attributed directly to our company in the same time period as in the first table.
SELECT i.id, count(*)
from table1 as i
FULL JOIN table1 as e ON e.id= i.id
WHERE i.active = TRUE
GROUP BY i.id
I tried merging the same table twice, because after I used count to get amount of inactive dates, I could not use another variable as it was not aggregated. This still does not work. I cannot imagine how I would do this for 3 tables. Can someone please point me in the right direction on how to do this? Thanks.
If there is one row for each day per eshopId and you want to count number of active days along with number of order per eshopId:
SELECT i.eshopId, count(*)
from table1 as i
left join (select eshopId, count(distinct orderId) from table2 group by eshopId) j on i.eshopId=j.eshopId
WHERE i.active = TRUE
GROUP BY i.eshopId

SQL Create variables from 2 different tables

I have 2 different tables called observations and intervals.
observations:
id,
type,
date
1 recess 03.05.2011 17:00
2 recess 03.06.2011 12:00
intervals:
id,
observation id,
value
1 1 5
2 1 8
3 2 4
4 2 4
I want a view that will display:
observation_id
percent_positive ((count where value = 5)/(total number of observations))
1 .5
2 0
I know
Select observation_id, Count(*) from intervals where value = 5 Group by
observation_id
will give me:
1 1
1 0
and
Select observation_id, Count(*) from intervals Group by
observation_id
will give me:
1 2
2 2
So how do I combine these to create a view with the percent_positive variable I'm looking for?
You can use joins to fetch data from two tables having a common column field .
For more ,please read it in detail Multiple values from multiple Tables
This gave me your desired result. Not proficient enough in SQL to determine if this is the optimal way of solving the issue though.
SELECT
observation_id as obs,
(SELECT COUNT(*) FROM intervals WHERE observation_id = obs AND value = 5)/(SELECT COUNT(*) FROM INTERVALS WHERE observation_id = obs) as percent
FROM observation
JOIN intervals ON observation.id = intervals.observation_id
GROUP BY observation_id;
SELECT
i.observation_id,
SUM(IF(i.value=5,1,0)) / counts.num as 'percent_positive'
FROM intervals i
inner join (
select observation_id, count(1) as num from intervals group by observation_id
) counts on counts.observation_id = i.observation_id
group by i.observation_id
order by i.observation_id
;
That oughta get you close, can't actually run to test at the moment. I'm not sure about the significance of the value 5 meaning positive, so the i.value=5 test might need to be modified to do what you want. This will only include observation IDs with intervals that refer to them; if you want ALL observations then you'll need to join that table (select from that table and left join the others, to be precise). Of course the percentage for those IDs will be 0 divided by 0 anyway...

How to count rows in MySQL using an IF statement?

I have a relatively simple question but I'm stuck with writing a proper SQL query to display the results that I need. I have a table which stores results from matches with columns indicating the IDs of the players that took part in the match, the winner and another boolean column which let's say indicates whether I want to include that match in the result or not. So the columns are:
player1_id | player2_id | winner_id | use
So winner_id is the value from one of the first two columns depending on which player won. If I want to count how many times a certain player won a game just using the rows where the use flag is up, I can easily do so with:
SELECT COUNT(*) AS total, winner_id
FROM table
WHERE use = 1
GROUP BY winner_id
ORDER BY total DESC
However, I also want to do the same count but for the players that lost their matches. In other words, I want to group not by the winner_id but by the loser id, which would be the value of either player1_id or player2_id depending on which one of them is different from the winner_id. Any clues on how to do that with a simple query that works?
You can do like this to count the loosers:
SELECT
COUNT(*) AS total,
IF(player1_id = winner_id, player2_id, player1_id) AS looser_id
FROM table
WHERE use = 1
GROUP BY looser_id
ORDER BY total DESC

Single Query to fill in missing column by calculating percentage of group total

I have a mysql table with schema as follows:
group id amount fraction
1 1 3
1 2 5
2 3 2
2 3 1
Each of the rows belongs to a group. Each row also stores a specific value called amount. I want to find the fraction of the group total amount that each row has, so the table will look like this:
AFTER
group id amount fraction
1 1 3 .375
1 2 5 .625
2 3 2 66.67
2 3 1 33.33
To get the value for column 1, I sum up all the amount columns in group 1. That would be 3+5, which is 8. Then I divide the amount in row 1 by the group sum, which yields .375. I do this for all of them.
I could do this by writing query like so:
SELECT SUM(amount) GROUP BY group
Then loop through each group, select rows in that group, calculate fractions, and update the rows. Unfortunately this means that after the initial query I will be dealing with 2 nested for loops, and millions and millions of queries, which will take a long time given the size of the dataset.
I have a subtle feeling that there is a way to do this more efficiently with one mysql query. If anybody has any ideas how to do this with a single query, that's my question.
You can do this in a single query with a join and aggregation:
select t.*, t.amount / tt.sumamount
from t join
(select group, sum(amount) as sumamount
from t
group by group
) tt
on t.group = tt.group;
EDIT:
The update is quite similar:
update t join
(select group, sum(amount) as sumamount
from t
group by group
) tt
on t.group = tt.group
set fraction = t.amount / tt.sumamount;

Mysql Grouping and fetching row corresponding to a maximum field value within that group

I'm confused about how to fit together group and max in mysql. Here's my problem:
I need to group data and then based on a maximum value among that group, I need to fetch that row. Here's a sample:
Table
ID Player Score
1 1 5
2 1 7
3 2 5
4 2 8
5 2 9
After grouping on based of players and fetching all the fields corresponding to maximum score for each player
Result
ID Player Score
2 1 7
5 2 9
Please help me writing the query for this problem.
This is one way to achieve what you want but there could be some easier way:
SELECT s.id,
s.player,
s.score
FROM scores s
JOIN (SELECT id,
player,
MAX(score) AS total
FROM scores
GROUP BY player
) r
ON r.total = s.score AND
r.player = s.player;
Live DEMO
Basically you're comparing the score and the player to get the correct ones as listed in your example.
However if you have multiple entries of the same player with same score you might have a problem there, if you want the first or last it might work with ordering but aside from that it would not work.