mysql subquery does not keep the order WHY? - mysql

good morning guys, I have this table named 'soccer_team'
id first_name surname player_number
1 Alexis Sanchez 7
2 Petr Cech 33
3 Hector Bellerin 24
4 Olivier Giroud 12
5 Theo Walcott 14
6 Santi Cazorla 19
if I launch this command,
SELECT CONCAT(first_name,' ',surname,' #',player_number) as 'i' from soccer_team order by player_number
it gives me
i
Alexis Sanchez #7
Olivier Giroud #12
Theo Walcott #14
Santi Cazorla #19
Hector Bellerin #24
Petr Cech #33
which is correct
however when I run
SELECT GROUP_CONCAT(i,' ') as 'players'
FROM (SELECT CONCAT(first_name,' ',surname,' #',player_number) as 'i'
from soccer_team
order by player_number
) as a;
it gives me
players
Alexis Sanchez #7 ;Petr Cech #33 ;Hector Bellerin #24 ;Olivier Giroud #12 ;Theo Walcott #14 ;Santi Cazorla #19
while it should be
players
Alexis Sanchez #7; Olivier Giroud #12; Theo Walcott #14; Santi Cazorla #19; Hector Bellerin #24; Petr Cech #33
**
how to solve the problem I know, I wanted to know why this happens
**
UPDATE
I know how to solve it, what interests me is because it works in this
way

You can specify the order inside GROUP_CONCAT provided that you include player_number in the subquery.
SELECT GROUP_CONCAT(i ORDER BY player_number ASC SEPARATOR ' ') as 'players'
FROM (
SELECT CONCAT(first_name,' ',surname,' #',player_number) as 'i' ,player_number
from soccer_team
) as a;
Here's a Demo.
Actually, you can simplify by removing the subquery which will still give you the same result
SELECT GROUP_CONCAT(CONCAT(first_name,' ',surname,' #',player_number)
ORDER BY player_number ASC SEPARATOR ' ') as 'players'
FROM soccerteam
Here's a Demo.

Related

Report Builder trouble with counting a sum of group values

I am trying to get the total field below to be the count of fields that are not calculated to equal 0. Instead of 1 I am looking for a total of 7 to display. The expression for the calculation of Column B is Sum(Fields!Hours.Value)/40. The expression for my Total field is: SUM(IIF(Sum(Fields!Hours.Value)/40 <>0,1 ,0))
-Column A Column B -BA #1 1.175 -BA #2 1.04375 -BA #3 1.375 -BA #4 0 -BA #5 1.1125 -BA #6 1.0375 -BA #7 1.05 -BA #8 0 -BA #9 1.40625 -BA #10 0 -Total 1
I have tried and just do not know what I am doing wrong here. Any help please? Much appreciated!

#1054 - Unknown column 'Brisbane' in 'where clause'

Write an SQL statement that uses a sub-query to retrieve all the information about the guests from Brisbane?
SELECT * FROM guest
WHERE guestCity = Brisbane IN (SELECT guestCity FROM guest
WHERE guestCity = Brisbane)
But it kept saying:
1054 - Unknown column 'Brisbane' in 'where clause'
I don't know what I'm doing wrong. Can someone shed some light please?
guestNo, guestName, guestAddress, guestCity, guestState, guestPostcode
1, Bill Watson, 56 Gee Street, Brisbane, QLD, 4000
2, Sharon Stone, 64 New Drive, Sydney, NSW, 2000
3, Mark Harris, 100 Regents Park Road, Brisbane, QLD ,4000
4, Silvia Smith, 312 West Road, Melbourne, VIC, 3000
Can you please try this query . It will return list of guest who are having
guestCity as 'Brisbane'
SELECT * FROM guest
WHERE guestCity = 'Brisbane'
You don't need the sub query to return what you need.
You also need to wrap strings in single quotes
SELECT g.*
FROM guest g
WHERE g.guestCity = 'Brisbane'
Sub Query
SELECT g.*
FROM guest g
WHERE g.guestCity IN (SELECT sg.guestCity
FROM guest sg
WHERE sg.guestCity = 'Brisbane')

MySQL column UPDATE data subsitution using IF (# 1054 - Unknown column in 'where clause')

I hope your day is going better then mine. I thought I had an easy task this morning to combine two tables in mysql (Distrib 5.5.43, for debian-linux-gnu) based on a common column. Basically the structure is as follows:
Practice1
Hip Description Purchaser Price
1 Bill Smith Sam Dillon $300,000
2 Justin Boyle Sarah Jones $75,000
3 Kevin Bains Anne Helan $120,000
4 Greg Demtri James Coon $250,000
Practice2
COL 1 COL 2 COL 3
2 James King $80,000
4 Bill Smell $300,000
What I'm trying to do is update Practice1 with values from Practice2. The end result would look like this:
Practice1
Hip Description Purchaser Price
1 Bill Smith Sam Dillon $300,000
2 Justin Boyle James King $80,000
3 Kevin Bains Anne Helan $120,000
4 Greg Demtri Bill Smell $300,000
What I've tried (and didn't work) so far has been:
UPDATE Practice1
SET Practice1.Purchaser=(SELECT `COL 2` FROM `Practice2` WHERE Practice1.Hip=`Practice2`.`COL 1`),
Practice1.Price=(SELECT `COL 3` FROM `Practice2` WHERE Practice1.Hip=`Practice2`.`COL 1`)
ORDER BY Practice1.Hip
What ended up happening was the information I wanted was updated, however the information that wasn't suppose to be touch became NULL! I.E.-
Practice1
Hip Description Purchaser Price
1 Bill Smith NULL NULL
2 Justin Boyle James King $80,000
3 Kevin Bains NULL NULL
4 Greg Demtri Bill Smell $300,000
So I tried an "IF" type of statement:
UPDATE Practice1
SET Practice1.Purchaser = if(Practice1.Purchaser=Practice2.`COL 2`, Practice1.Purchaser, Practice2.`COL 2`),
Practice1.Price = if(Practice1.Price=Practice2.`COL 3`, Practice1.Price, Practice2.`COL 3`)
WHERE Practice1.Hip=Practice2.`COL 1`
ORDER BY Practice1.Hip
which gave me the error "1054 - Unknown column 'Practice2.COL 1' in 'where clause'"
I think its something basic that I'm missing (I hope).
Try:
UPDATE practice1 p1, practice2 p2
SET p1.`Price` = p2.`COL3`, p1.`Purchaser` = p2.`COL2`
WHERE p1.`Hip` = p2.`COL1`
;
Demo: http://sqlfiddle.com/#!9/b3a26/1

Matching a duplicate entries in two columns and for those that have count >1 taking a third value for both and adding them

I have a table containing: ID, FEATURE_NAME (that would be a name of the city), STATE_ALPHA (two letter country identifier like 'AL' for Alabama), and POPULATION_DATA.
I need to:
find entries that have same FEATURE_NAME and STATE_ALPHA
take values for POPULATION_DATA in both(or more) appearances and add them
write down the sum in POPULATION_DATA where all addends are from.
Example:
- ID !FEATURE NAME ! STATE_ALPHA ! POPULATION DATA
- 1 Woodland WA 83
- 2 Woodland WA 5426
- 3 Vining KS 354
- 4 Vining KS 276
- 5 Vining KS 1450
What I need to get is:
- ID !FEATURE NAME ! STATE_ALPHA ! POPULATION DATA
- 1 Woodland WA 5509
- 2 Woodland WA 5509
- 3 Vining KS 2080
- 4 Vining KS 2080
- 5 Vining KS 2080
I googled for hours and dont even know where to start. Also I'll run that script on a view not on original table, I don't know does it changes anything. Please help.
You can do this with an aggregation and a join:
select t.id, t.feature_name, t.state_alpha, sumpop as Population_Data
from t join
(select feature_name, state_alpha, sum(population_data) as sumpop
from t
group by feature_name, state_alpha
) fs
on t.feature_name = fs.feature_name and
t.state_alpha = fs.state_alpha;
The aggregation sums the population (in the subquery fs). This result is joined back to the original data.
If I understand your question, a simple query should work where the table name is tablename
SELECT ID, FEATURE_NAME, STATE_ALPHA, POPULATION_DATA FROM tablename
FROM tablename
GROUP BY FEATURE_NAME, STATE_ALPHA

Query giving undesirable results

I have the following two tables:
lab_assistant
la_id - la_firstname - la_lastname
1 - Dennis - Rodman
2 - Michael - Jordan
3 - Horace - Grant
hours_worked
hw_semester - hw_date - hw_hours - la_id
Fall 2012 - 2012-11-01 - 4 - 2
Fall 2012 - 2012-11-04 - 5 - 3
Spring 2012 - 2012-02-12 - 4 - 1
Spring 2012 -2012-03-10 - 4 - 1
The result of my query is supposed to look like the following:
and I'm trying to run the following query in order to list the Lab Assistant and the number of hours they worked each semester
SELECT DISTINCT(CONCAT(la.la_firstname, ' ', la.la_lastname)) AS 'Lab Assistant',
hw.hw_semester AS 'Semester Worked', hw.hw_hours AS 'Hours Worked'
FROM lab_assistant la
JOIN hours_worked hw
ON la.la_id = hw.la_id;
The results of my quere are SUPPOSED to look like this:
Michael Jordan - Fall 2012 - 4
Horace Grant - Spring 2012 - 5
Dennis Rodman - Fall 2012 - 8
BUT the results I'm getting are as follows:
Michael Jordan - Fall 2012 - 4
Horace Grant - Fall 2012 - 5
Dennis Rodman - Spring 2012 - 4
So basically, it just isn't counting the proper hours for Dennis Rodman. It should be 8 and not 4.
And I probably shouldn't use DISTINCT here because it's possible the same person could work different semesters.
DISTINCT needs to go at the beginning of the SELECT:
SELECT DISTINCT CONCAT(la.la_firstname, ' ', la.la_lastname) AS 'Lab Assistant',
hw.hw_semester, COUNT(w.hw_hours)
FROM lab_assistant la
JOIN hours_worked hw
ON la.la_id = hw.la_id;
But since you are using aggregate functions, you will want to use GROUP BY:
SELECT CONCAT(la.la_firstname, ' ', la.la_lastname) AS 'Lab Assistant',
hw.hw_semester, COUNT(w.hw_hours)
FROM lab_assistant la
JOIN hours_worked hw
ON la.la_id = hw.la_id
GROUP BY hw.hw_semester;
Based on your edit, you need something like this:
SELECT CONCAT(la.la_firstname, ' ', la.la_lastname) AS 'Lab Assistant',
hw.hw_semester AS 'Semester Worked',
sum(hw.hw_hours) AS 'Hours Worked'
FROM lab_assistant la
JOIN hours_worked hw
ON la.la_id = hw.la_id
GROUP BY la.la_id, hw.hw_semester;
See SQL Fiddle with Demo
Results:
| LAB ASSISTANT | SEMESTER WORKED | HOURS WORKED |
---------------------------------------------------
| Dennis Rodman | Spring 2012 | 8 |
| Michael Jordan | Fall 2012 | 4 |
| Horace Grant | Fall 2012 | 5 |
This is because DISTINCT is a modifier to SELECT and cannot be used like that. What's best in your case is to apply a GROUP BY on hw.hw_semester to fix it.
The MySQL syntax for SELECT shows that DISTINCT needs to come before the column names:
SELECT
[ALL | DISTINCT | DISTINCTROW ]
[ .. etc .. ]
select_expr [, select_expr ...]
Each select_expr indicates a column that you want to retrieve.
Basically, DISTINCT can only apply to the entire query, not a single column.