What I want to do is select everything from the database for a specific dancer group that by date and then look for the latest date for each dance and only show those.
See attached image of what results (big red arrows) I would want to return from the database. I this example the single jig was not danced on the 11th of Sept but I need the last time this was danced (10th) so I can then calculate the current level a dancer is at for all dances.
Try
SELECT t.*
FROM table1 t JOIN (
SELECT dancer_id, dance, MAX(date) date
FROM table1
GROUP BY dancer_id, dance
) q ON t.dancer_id = q.dancer_id
AND t.dance = q.dance
AND t.date = q.date
Here is a SQLFiddle
Sample Output:
| id | dancer_id | date | dance | level | placed | venue |
|-----|-----------|-----------------------------|------------|-------|--------|--------|
| 143 | 130 | September, 10 2016 00:00:00 | Single Jig | 1 | 4 | (null) |
| 146 | 130 | September, 11 2016 00:00:00 | Reel | 2 | 5 | (null) |
| 147 | 130 | September, 11 2016 00:00:00 | Light Jig | 2 | 4 | (null) |
| 148 | 130 | September, 11 2016 00:00:00 | Slip Jig | 2 | 4 | (null) |
Related
I have input table:
+-----+------------+---------------------+---------+-------------+
| id | client_id | subscription_date | program |how_long_subs|
+-----+------------+---------------------+---------+-------------+
| 1 | 1 | 2016-05-20 00:00:00 | W90 | 12 |
| 2 | 4 | 2017-01-01 00:00:00 | W90 | 6 |
| 314 | 7 | 2017-03-21 00:00:00 | AB | 12 |
| 3 | 8 | 2016-12-19 00:00:00 | W90 | 12 |
| 4 | 9 | 2018-06-08 00:00:00 | W90 | 12 |
| 5 | 10 | 2017-06-12 00:00:00 | W90 | 12 |
| 313 | 10 | 2017-01-18 00:00:00 | AB | 12 |
| 377 | 11 | 2018-12-22 00:00:00 | AB | 12 |
| 308 | 11 | 2017-12-22 00:00:00 | AB | 12 |
| 6 | 11 | 2017-12-22 00:00:00 | W90 | 12 |
| 7 | 13 | 2017-01-05 00:00:00 | W90 | 6 |
| 8 | 21 | 2017-03-10 00:00:00 | W90 | 12 |
| 325 | 22 | 2017-03-02 00:00:00 | AB | 1 |
+-----+------------+---------------------+---------+-------------+
id is unique autoincremented column for this table client_id is to connect with clients table. I am looking for a result in form:
+------------+----------+-----------------------------------------+--------+
| client_id | programs | dates |how_long|
+------------+----------+-----------------------------------------+--------+
| 1 | W90 | 2016-05-20 00:00:00 | 12 |
| ... | | | |
| 11 | W90|AB | 2017-12-22 00:00:00|2018-12-22 00:00:00 | 12|12 |
+------------+----------+-----------------------------------------+--------+
so every client_id has 3 columns: programs, dates, how_long
so far I have below for columns programy and na_ile
SELECT
client_id,
GROUP_CONCAT(DISTINCT program SEPARATOR '|') AS programs
FROM subscriptions GROUP BY client_id
but I have no idea how to do this for dates, so everything is in good order.
in example result I chose some client that has multiple rows for different programs, to best illustrate, as you can see dates in result are MAX date for certain program.
To give you better picture what I'm trying to do is process my input table, to be able to calculate remaining subscription for different programs.
Formula is:
if client_id has no records in subscriptions then use date from clients table
and return difference between that date +1 year and today.
if client_id has records in subscriptions then
for every unique program client has in subs table:
get max subscription_date and corresponding how_long_subs for that max date/program. add how_long_subs months to subscription date and return difference from now in days.
My plan is to get result table as described above and use php to process rows to get something like this:
example result for client_id: 11
W90: 2017-12-22 00:00:00 +12 months - now()
AB: 2018-12-22 00:00:00 +12 months - now()
Can you please help me with mySQL query and advice if approach im taking is ok or maybe there is some much easier way of doing it.
Thank you
SELECT A.client_id,
GROUP_CONCAT(DISTINCT A.program SEPARATOR '|') programs,
GROUP_CONCAT(DISTINCT A.subscription_date SEPARATOR '|') dates,
GROUP_CONCAT(A.how_long_subs SEPARATOR '|') how_long
FROM
(SELECT client_id, subscription_date, MAX(program) program, how_long_subs
FROM subscriptions
GROUP BY client_id, subscription_date,how_long_subs) A
GROUP BY A.client_id;
See DEMO on SQL Fiddle.
You could use a subquery for get the max date
SELECT client_id, GROUP_CONCAT( program SEPARATOR '|') programs,
GROUP_CONCAT(max_date SEPARATOR '|') max_date
FROM (
SELECT
client_id,
programs,
max(subscription_date) max_date
FROM subscriptions
GROUP BY client_id, program )
I'm using MySQL and PHP.
I have 2 tables.
The first one has dates in it.
This table is called dates
+-------+-------------+----------+---------+
| id | unixTime | month | year |
+------------------------------------------+
| 1 | 1443657600 | 10 | 2015 |
| | | | |
| 2 | 1443657600 | 11 | 2015 |
| | | | |
| 3 | 1443657600 | 12 | 2015 |
| | | | |
| 4 | 1443657600 | 01 | 2016 |
| | | | |
+-------+-------------+----------+---------+
The table below will show if you query SELECT * FROM analytics WHERE clientfk=36. There's only 3 entries because client #36 stopped being a client in January.
This table is called analytics
+------------------------------------------------+
| id datefk Value clientfk |
+------------------------------------------------+
| 156 1 "some value" 36 |
| |
| 157 2 "another value" 36 |
| |
| 157 3 "thrid value" 36 |
| |
+------------------------------------------------+
I want to join(?) union(?) these tables and get an output that looks like this: (NB, the null value)
+------------------------------------------------+
| month year client value |
+------------------------------------------------+
| 10 2015 36 "Some value" |
| |
| 11 2015 36 "Another value" |
| |
| 12 2015 36 "third value" |
| |
| 1 2016 36 NULL |
+------------------------------------------------+
I tried the query below, among a myriad of other things but always either returns nothing or just 3 rows
SELECT *
FROM analytics a
WHERE a.clientfk = 36
AND NOT EXISTS (SELECT null FROM dates d
WHERE d.id = a.datefk)
you are looking for LEFT JOIN
SELECT `dates`.`month`, `dates`.`year`, `analytics`.`clientfk` AS 'client', `analytics`.`value`
FROM `dates`
LEFT JOIN `analytics` ON `analytics`.`datefk` = `dates`.`id` AND `analytics`.`clientfk` = '36'
WHERE 1
You will get result by LEFT JOIN.
SELECT
d.month as month, d.year as year, a.clientfk as client, a.value as value
from dates d
LEFT JOIN analytics a on d.id = a.datefk
WHERE a.clientfk = 36
By this you will get Client also null. Because you don't have data in analytics for date id 4.
+------------------------------------------------+
| month year client value |
+------------------------------------------------+
| 10 2015 36 "Some value" |
| |
| 11 2015 36 "Another value" |
| |
| 12 2015 36 "third value" |
| |
| 1 2016 NULL NULL |
+------------------------------------------------+
But if you still want client id to be present in that column you can use case.
SELECT d.month as month, d.year as year, CASE
WHEN a.clientfk = null THEN 36
ELSE a.clientfk
END as client, a.value as value
from
dates d
LEFT JOIN analytics a on d.id = a.datefk
WHERE a.clientfk = 36
Currently I am developing a system to support a competition. In this competition there are 11 judges that give a grade to the participant. This grade is stored in a table per judge. With the following Select statement I retrieve the total score from the MySQL database (and also some inner joins to select other information from other tables).
Now I want to have ranking in this system, where also ex aequo is taken into account. I have tried several mysql solutions from this forum. But somehow the statement is always incorrect. Hopefully, someone can help me.
SELECT optreden.id, optreden.wedstrijd, optreden.jaartal,
liedje.titel, club.volledige_clubnaam,
(SELECT SUM(score.score_lied) FROM score WHERE score.optreden=optreden.id) AS score
FROM optreden
INNER JOIN liedje ON optreden.liedje=liedje.id
INNER JOIN club ON liedje.uitvoerende_club=club.id
WHERE optreden.wedstrijd="voorselectie" AND optreden.jaartal=2014
GROUP BY optreden.id
ORDER BY score_lied DESC
With this query, I get the following result:
+----+--------------+------+----------+--------------+------+
| id | Competition | Year | Title | Artist (club)| Score|
+----+--------------+------+----------+--------------+------+
| 12 | voorselectie | 2014 | Song 1 | Club 1 | 792 |
| 16 | voorselectie | 2014 | Song 2 | Club 2 | 600 |
| 10 | voorselectie | 2014 | Song 3 | Club 3 | 600 |
| 11 | voorselectie | 2014 | Song 3 | Club 3 | 500 |
| 12 | voorselectie | 2014 | Song 3 | Club 3 | 400 |
The query fetches information from the following tables:
Score: in this table the scores given by the 11 judges is stored as
line-items (result is given below);
Optreden: in this table the information from the club and the song is linked;
Club: information from the club,
such as name;
Liedje: information about the song, performed by the
club.
All the tables are linked by id's. In the result, there should come a column, with the ranking of the club.
The result from the table score is as follows:
+----+----+--------+----+
| ID | Pid| Userid | 85 |
+----+----+--------+----+
| 1 | 12 | 444 | 85 |
| 12 | 12 | 454 | 92 |
| 13 | 12 | 445 | 87 |
| 14 | 12 | 446 | 56 |
| 15 | 12 | 447 | 81 |
| 16 | 12 | 448 | 78 |
| 17 | 12 | 449 | 55 |
| 18 | 12 | 450 | 69 |
| 19 | 12 | 451 | 88 |
| 20 | 12 | 452 | 69 |
| 21 | 12 | 453 | 32 |
+----+----+--------+----+
Where the columns represent the following:
- ID: the id of the score in the table;
- Pid: the id of the performance (with this performing club and song are linked);
- Userid: the userid of the 11 judges;
- Score: the score given by each judge.
The end result should be as follows:
+----+--------------+------+----------+--------------+------+------+
| id | Competition | Year | Title | Artist (club)| Score| Rank |
+----+--------------+------+----------+--------------+------+------+
| 12 | voorselectie | 2014 | Song 1 | Club 1 | 792 | 1 |
| 16 | voorselectie | 2014 | Song 2 | Club 2 | 600 | 2 |
| 10 | voorselectie | 2014 | Song 3 | Club 3 | 600 | 2 |
| 11 | voorselectie | 2014 | Song 3 | Club 3 | 500 | 4 |
| 12 | voorselectie | 2014 | Song 3 | Club 3 | 400 | 5 |
I have tried the solution from Neville before and now again. I have rewritten the SQL statement as follows:
SELECT optreden.id, optreden.wedstrijd, optreden.jaartal,
liedje.titel, club.volledige_clubnaam,
(SELECT SUM(score.score_lied) FROM score WHERE score.optreden=optreden.id) AS score, CASE
WHEN #prev_value = score THEN #rank_count
WHEN #prev_value := score THEN #rank_count := #rank_count + 1
END AS rank,
FROM optreden
INNER JOIN liedje ON optreden.liedje=liedje.id
INNER JOIN club ON liedje.uitvoerende_club=club.id
WHERE optreden.wedstrijd="voorselectie" AND optreden.jaartal=2014
GROUP BY optreden.id
ORDER BY score_lied DESC
Here I get the error: Unknown column 'score' in 'field list'..
It seems that you are getting this error simply because there is no field score... I have extracted your inner SELECT into a JOIN so the real field can be used directly. (Not tested though, sorry)
SELECT optreden.id, optreden.wedstrijd, optreden.jaartal,
liedje.titel, club.volledige_clubnaam,
SUM(score.score_lied) AS score, CASE
WHEN #prev_value = SUM(score.score_lied) THEN #rank_count
WHEN #prev_value := SUM(score.score_lied) THEN #rank_count := #rank_count + 1
END AS rank,
FROM optreden
INNER JOIN liedje ON optreden.liedje=liedje.id
INNER JOIN club ON liedje.uitvoerende_club=club.id
LEFT JOIN score ON score.optreden=optreden.id
WHERE optreden.wedstrijd="voorselectie" AND optreden.jaartal=2014
GROUP BY optreden.id
ORDER BY score_lied DESC
Please note that you might face another little problem with this kind of ranking: Normally, when two entities end up on the same rank, the next rank is not given to the next person. Here's what I mean with an example, with two persons ranking in 3:
1, 2, 3, 3, 5
But in your code, it will give:
1, 2, 3, 3, 4
I hope this helps.
I have tried several things. However, I had forgotten to tell you one important feature I was gonna use. The ranking of the clubs should be presented through a presentation on a big screen. Therefor the MySQL code changed a little.
I have added a support table to my database. In this support table the sum of the scores is recorded with a INSERT INTO...SELECT statement.
When this insert is done. An update script is launched which makes the ranking in the table and also makes a correct calculation for ex aequo.
Here's the update statement:
SET #rank=0, #last_score = null, #tie_build_up = 0;
UPDATE ranking_voorselectie_lied
SET rank= #rank:= if(#last_score = totaal_score_lied, #rank, #rank+#tie_build_up+1),
tie_build_up= #tie_build_up:= if(#last_score = totaal_score_lied, #tie_build_up+1, 0),
last_score= #last_score:= totaal_score_lied
WHERE wedstrijd="voorselectie" AND jaar=2014
ORDER BY totaal_score_lied DESC;
This gives me the following result:
+--------------+------+----------+-------------------+------+-----+------------+
| Wedstrijd | Jaar | Optreden | totaal_score_lied | Rank | Tie | last_score |
+--------------+------+----------+-------------------+------+-----+------------+
| voorselectie | 2014 | 12 | 792 | 1 | 0 | 792 |
| voorselectie | 2014 | 16 | 82 | 2 | 0 | 82 |
| voorselectie | 2014 | 10 | 73 | 3 | 0 | 73 |
| voorselectie | 2014 | 15 | 51 | 4 | 0 | 51 |
| voorselectie | 2014 | 3 | 50 | 5 | 0 | 50 |
| voorselectie | 2014 | 11 | 42 | 6 | 0 | 42 |
| voorselectie | 2014 | 13 | 38 | 7 | 0 | 38 |
| voorselectie | 2014 | 8 | 38 | 7 | 1 | 38 |
| voorselectie | 2014 | 14 | 37 | 9 | 0 | 37 |
| voorselectie | 2014 | 5 | 35 | 10 | 0 | 35 |
| voorselectie | 2014 | 4 | 33 | 11 | 0 | 33 |
| voorselectie | 2014 | 7 | 25 | 12 | 0 | 25 |
| voorselectie | 2014 | 9 | 23 | 13 | 0 | 23 |
+--------------+------+----------+-------------------+------+-----+------------+
That title may not make sense, I'm not sure how to ask what I'm trying to do in a single sentence...
I have a MySQL table like this:
| id | user | team | month | result |
|----|-------|--------|-------|--------|
| 1 | Joe | red | sept | 100 |
| 2 | Joe | red | oct | 40 |
| 3 | Jim | red | sept | 70 |
| 4 | Jim | red | oct | 50 |
| 5 | Susy | red | sept | 40 |
| 6 | Tim | blue | sept | 60 |
| 7 | Tim | blue | oct | 100 |
| 8 | Betty | blue | sept | 70 |
| 9 | Dave | blue | sept | 20 |
| 10 | Stan | green | oct | 40 |
| 11 | Alan | green | sept | 80 |
| 12 | Tina | green | oct | 100 |
| 13 | Tina | green | sept | 30 |
| 14 | Rick | yellow | oct | 50 |
| 15 | Ellen | yellow | oct | 60 |
Ultimately I'm trying to output an HTML table that shows a total number of users who have a result greater than 50 organized by team and month.
Here's the query I'm running right now:
SELECT team, month, count(*)
FROM example
WHERE result >= 50
GROUP BY team, month
Which returns this:
| team | month | count(*) |
|--------|-------|----------|
| blue | oct | 1 |
| blue | sept | 2 |
| green | oct | 1 |
| green | sept | 1 |
| red | oct | 1 |
| red | sept | 2 |
| yellow | oct | 2 |
But in my HTML table I want to list out months as columns (and add a total column). So an HTML table that renders like this (ignore the sorting, it's arbitrary):
| Team | sept | oct | Total |
|--------|------|-----|-------|
| red | 2 | 1 | 3 |
| blue | 2 | 1 | 3 |
| green | 1 | 1 | 2 |
| yellow | 0 | 2 | 2 |
Can I work with the query result I have and somehow manipulate it into the end format in the PHP/HTML? Or do I need to change the way I'm querying in the first place?
*Editing to change the HTML table output example - I want this to be dynamic so that as more months are added to original table the resulting HTML table can expand horizontally.
The basic SQL you need is:
SELECT Team,
COUNT(CASE WHEN Month = 'Sept' THEN 1 END) AS Sept,
COUNT(CASE WHEN Month = 'Oct' THEN 1 END) AS oct,
COUNT(*) AS Total
FROM T
GROUP BY Team;
Example on SQL Fiddle
If you don't know the number of columns you need you will need to do it dynamically with a prepared statement'
SET #sql = NULL;
SELECT GROUP_CONCAT(DISTINCT
CONCAT(
'COUNT(CASE WHEN Month = ''',
Month,
''' THEN 1 END) AS ',
Month
)
)
INTO #sql
FROM T;
SET #sql = CONCAT('SELECT Team, ', #sql, ', COUNT(*) AS Total FROM T GROUP BY Team');
PREPARE stmt FROM #sql;
EXECUTE stmt;
DEALLOCATE PREPARE stmt;
Example on SQL Fiddle
This question already has an answer here:
Closed 10 years ago.
Possible Duplicate:
Calculating total quantity of equipments for a date range
Project: I am working on a project which is about some rooms and equipments using in the rooms. The software is about scheduling the equipments in the rooms. In other words, it is a reservation software that reserves selected equipments in separate rooms for needed dates and times ranges. I have many tables in MYsSQL database working with Php but I will mention the tables my question is about. The tables I will relate my questions are equipment table (Table A), schedule table (Table B) and equipments using in the related schedule (Table C).
Table A: equipment list table
+------+----------+-----------+
| eqid | eqName | available |
+------+----------+-----------+
| 1 | book | 90 |
| 2 | pen | 82 |
| 3 | computer | 25 |
+------+----------+-----------+
In table A; eqid represents unique id of an equipment, eqName represents name of an equipment, available represents total available equipments existing.
Table B: schedule table
+------------+------------+------------+-----------+----------+--------+
| scheduleid | startDate | endDate | startTime | endTime | office |
+------------+------------+------------+-----------+----------+--------+
| 1 | 2012-08-27 | 2012-08-27 | 08:30:00 | 10:00:00 | room1 |
| 2 | 2012-08-27 | 2012-08-27 | 09:30:00 | 11:00:00 | room3 |
| 3 | 2012-08-28 | 2012-08-30 | 08:30:00 | 12:00:00 | room2 |
| 4 | 2012-08-29 | 2012-08-31 | 11:30:00 | 14:00:00 | room1 |
| 5 | 2012-08-28 | 2012-08-28 | 10:30:00 | 14:00:00 | room3 |
| 6 | 2012-08-27 | 2012-08-30 | 08:30:00 | 10:00:00 | room4 |
| 7 | 2012-08-27 | 2012-08-27 | 10:30:00 | 12:00:00 | room4 |
| 8 | 2012-08-27 | 2012-08-30 | 08:30:00 | 11:00:00 | room6 |
| 9 | 2012-08-27 | 2012-08-27 | 10:30:00 | 12:00:00 | room5 |
+------------+------------+------------+-----------+----------+--------+
In table B; scheduleid represents unique id for a schedule, startDate and endDate are date range for a schedule, startTime and endTime time range for a schedule, office means that where the schedule will take place. Let me give an example here. Scheduleid 1 means there is a reservation on 27th of august 2012, Monday and it is from 08.30 to 10:00. As it start and end on same day this is just one day reservation in room1. However, Scheduleid 3 means there is a reservation starts on 28th of august 2012, Tuesday and goes on until 30th of august 2012, Thursday at 08:30-12:00... in other words, it lasts for 3 days and everyday from 08:30 to 12:00... So there is a reservation from Tuesday to Thursday at 08:30 to 12:00 in room2... I hope this is clear.
Table C: equipments using in the related schedule
+--------+------------+------+-------------+
| Autoid | scheduleid | eqid | amountInSch |
+--------+------------+------+-------------+
| 1 | 1 | 1 | 2 |
| 2 | 1 | 2 | 3 |
| 3 | 1 | 3 | 1 |
| 4 | 2 | 1 | 1 |
| 5 | 2 | 2 | 1 |
| 6 | 2 | 3 | 2 |
| 7 | 3 | 2 | 1 |
| 8 | 3 | 3 | 3 |
| 9 | 4 | 2 | 1 |
| 10 | 4 | 3 | 1 |
| 11 | 5 | 1 | 1 |
| 12 | 6 | 1 | 1 |
| 13 | 6 | 3 | 2 |
| 14 | 6 | 2 | 4 |
| 15 | 7 | 1 | 5 |
| 16 | 7 | 2 | 6 |
| 17 | 8 | 2 | 1 |
| 18 | 9 | 1 | 8 |
| 19 | 9 | 2 | 5 |
| 20 | 9 | 3 | 6 |
+--------+------------+------+-------------+
In table C: Autoid represents unique automatic id generated by auto-increment, scheduleid comes from Table B, eqid comes from Table A, amountInSch represents how many (amount) equipment will use in the related schedule. I want to give an example here. Scheduleid 1 in Table C, there are 3 rows. This means that scheduleid 1 related in TAble B will use 2 books (eqid 1), 3 pens (eqid 2) and 1 computer (eqid 3) in room1 specified dates and times in table B . Another example is that scheduleid 3 in Table C is related 2 rows. It means that 1 pen (eqId 2) and 3 computers (eqId 3) will be using in room2 from 27th to 30th of august 2012 everyday from 08:30 to 12:00.
The above is the explanation and give some information about the project. The table rows are not permanent. When you make a reservation, there will be a new row in Table B and if it is selected an equipment, there will be new rows in table C...
The Question:
I want to calculate left amount of a specific equipment when I supply eqId, startDate, endDate, startTime and endTime...
An example:
eqId: 1 (book)
startDate: 2012-08-27
endDate: 2012-08-27
startTime: 08:30:00
endTime: 12:00:00
Result should be: 14 books used in schedule and 76 left available books
Because: if you look scheduleIds and related eqIds, you will only see 1, 2, 6, 7, 9 scheduleIds related to my query(dates and eqId). If you sum the all amount of related in Table C, you will get the wrong result. In other words, related amounts for eqId(1-book) and for 1, 2, 6, 7, 9 scheduleIds are 2, 1, 1, 5, 8 respectively. So if you sum them you will get 17 which is wrong. Because, 1 and 9 schedule don't intersect each other in terms of start and end Times, and 6 and 7 don't intersect each other either. as a result of them 2 stays lonely and can be count separately. We must consider 1 and 9 as summed 8 because 8 is bigger than 2. it is same for 6 and 7, considered as 5 because of 5 is bigger than 1...
So folks! I am not sure how I can sum/ this in programming algorithm. Is there a way to do in SQL or do I have to use PHP and Mysql together? and How?
SQLFiddle Records
There are different ways of achieving this - I will point you at something that can be used with most DB-engines.
In mysql, it is possible to merge tables in a single query. What you need is something along the line of this:
$eqid = 1;
$startDate = "2012-08-27";
$endDate = "2012-08-27";
$startTime = "08:30:00";
$endTime = "12:00:00";
$sql = "SELECT SUM(`amountInSch`) FROM `table_c`,`table_b` WHERE `eqid` = $eqid
AND `startDate` >= '$startDate' AND `startDate` <= '$endDate' AND `endDate`
<= '$endDate' AND `endDate` >= '$startDate' AND `startTime` >= '$startTime'
AND `startTime` <= '$endTime' AND `endTime` <= '$endTime' AND `endTime` >=
'$startTime' AND `table_b`.`scheduleid` = `table_c`.`scheduleid`";
$r = mysql_query($sql);
$n = mysql_result($r,0,0);
The trick is to use the table_b`.`scheduleid` =
`table_c`.`scheduleid (some quotes are missing due to markup here) to merge the two tables.